Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect OS from Bash script and notify user

Using bash, I want to find the operating system and notify the user. I tried:

OS='uname -s'
echo "$OS"
if [ "$OS" == 'Linux' ]; then
    echo "Linux"
else 
    echo "Not Linux"
fi

I just get

uname -s 
Not Linux

on the terminal, which is wrong. How do I correctly set the string to what uname returns?

Thanks

like image 787
Graham Avatar asked May 24 '12 11:05

Graham


1 Answers

Rather than single quotes, you probably meant to use backticks:

OS=`uname -s`

but you really want

OS=$(uname -s)

Also, rather than an if statment, which will eventually become an if/else series, you might consider using case:

case $( uname -s ) in
Linux) echo Linux;;
*)     echo other;;
esac
like image 133
William Pursell Avatar answered Oct 14 '22 14:10

William Pursell