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 to correctly set a string to what uname returns?

thanks

+6
source share
2 answers

Instead of single quotes, you probably wanted to use backreferences:

 OS=`uname -s` 

but you really want

 OS=$(uname -s) 

Also, instead of stat if, which will eventually become the if / else series, you can consider the case:

 case $( uname -s ) in Linux) echo Linux;; *) echo other;; esac 
+12
source

This will cause the OS to return on demand - note that uname not always available for all OSs, so it is 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 
+1
source

Source: https://habr.com/ru/post/916553/


All Articles