Pergunta

I am trying to make my script check for x32 or x64 OS and select files from the script package based on the results.

For example: If the OS is x32-bit I want the script to copy file-x32 to the install directory If the OS is x64-bit I want the script to copy file-x64 to the install directory

I came across this code which prints the result:

    if [ `getconf LONG_BIT` = "64" ]
    then
        echo "I'm 64-bit"
    else
        echo "I'm 32-bit"
    fi

Would it be as simple as to modify this script like this to do the job:

    if [ `getconf LONG_BIT` = "64" ]
    then
        mv /tmp/autoscript/file-x64 /var/destination
    else
        mv /tmp/autoscript/file-x32 /var/destination
    fi

If possible I would also like to echo to the user either: x32-bit OS Detected or x64-bit OS Detected

Foi útil?

Solução

The if-else block can contain multiple statements. Thus, if you want an echo for each branch, you could do something like so.

if [ `getconf LONG_BIT` = "64" ]
then
    echo "I'm 64-bit"
    mv /tmp/autoscript/file-x64 /var/destination
else
    echo "I'm 32-bit"
    mv /tmp/autoscript/file-x32 /var/destination
fi

If you would like to use the architecture information elsewhere, you can store it in a variable and do something like this:

arch=$(getconf LONG_BIT)
echo "I'm $arch-bit"
if [ "$arch" = "64" ]
then
    mv /tmp/autoscript/file-x64 /var/destination
else
    mv /tmp/autoscript/file-x32 /var/destination
fi

Even better, with the variable in place you can do this:

arch=$(getconf LONG_BIT)
echo "I'm $arch-bit"
mv /tmp/autoscript/file-x$arch /var/destination
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top