Graham
Graham

Reputation: 581

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

Upvotes: 7

Views: 12887

Answers (2)

rjmoggach
rjmoggach

Reputation: 1470

This will return the OS as requested - note that uname is not necessarily available on all OSes so it's not part of this answer.

case "$OSTYPE" in
  linux*)   echo "linux" ;;
  darwin*)  echo "mac" ;; 
  msys*)    echo "windows" ;;
  solaris*) echo "solaris" ;;
  bsd*)     echo "bsd" ;;
  *)        echo "unknown" ;;
esac

Upvotes: 2

William Pursell
William Pursell

Reputation: 212148

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

Upvotes: 13

Related Questions