2

The script should be able to detect the operating system that is running. The alternatives OS is Arch Linux, Centos and Ubuntu.

os=$(uname) 
if [ "$os" == "Arch" ]; then   
  echo "Arch Linux detected" 
elif [ "$os" == "CentOS" ]; then   
  echo "CentOS detected" 
elif [ "$os" == "Ubuntu" ]; then   
  echo "Ubuntu detected" 
else   
  echo "Unknown OS detected"
fi```

Output: Unknown OS detected


I tried doing this:

\`del1()

{
os=$(cat /etc/os-release | grep "PRETTY_NAME")
}
del1

echo "The operating system is: $os"\`

The output: PRETTY_NAME="Ubuntu 20.04.2 LTS"

But I want to check between Centos, Arch Linux and Ubuntu.

Any suggestions?
3

3 Answers 3

7

The uname command will always return Linux when running on Linux, so of course that's never going to work.

Using /etc/os-release is probably the best solution, but don't grep it for information; the file is a collection of shell variables that you can source with the . command, so you can write something like this:

#!/bin/sh

. /etc/os-release

case $ID in
  ubuntu) echo "This is Ubuntu!"
    ;;

  arch) echo "This is Arch Linux!"
    ;;

  centos) echo "This is CentOS!"
    ;;

  *) echo "This is an unknown distribution."
      ;;
esac
Sign up to request clarification or add additional context in comments.

2 Comments

This is the cleanest! man os-release for details on the variables. The variable $ID is the best choise to use. The file /etc/os-release is adopted by all linux distributions that use systemd. a bit more details here: 0pointer.de/blog/projects/os-release
Thanks for help, that made much more sense :D
0

You can use the grep command to filter the output of the cat /etc/os-release command for specific strings that indicate the operating system. For example, you could use the following command to check for Ubuntu:

os=$(cat /etc/os-release | grep -o "Ubuntu")

You could then use an if statement to check if the variable os equals to Ubuntu:

if [ "$os" == "Ubuntu" ]; then   
  echo "Ubuntu detected" 
else   
  echo "Not Ubuntu detected"
fi

You can do the same to check for Arch Linux:

os=$(cat /etc/os-release | grep -o "Arch")

And for Centos:

os=$(cat /etc/os-release | grep -o "CentOS")

You can also use cat /etc/*-release instead of cat /etc/os-release for more general detection of the OS.

You can also use lsb_release -a command to get more details about the distribution and version of the OS.

os=$(lsb_release -a | grep -o "Ubuntu")

You can then create a function that check for each os one by one and print the output accordingly.

check_os(){
os=$(cat /etc/os-release | grep -o "Ubuntu")
if [ "$os" == "Ubuntu" ]; then   
  echo "Ubuntu detected" 
else
  os=$(cat /etc/os-release | grep -o "Arch")
  if [ "$os" == "Arch" ]; then   
    echo "Arch Linux detected" 
  else
    os=$(cat /etc/os-release | grep -o "CentOS")
    if [ "$os" == "CentOS" ]; then   
      echo "CentOS detected" 
    else   
      echo "Unknown OS detected"
    fi
  fi
fi
}
check_os

Please note that this approach might not be 100% accurate and it is better to use the appropriate package manager commands to check the OS version and distribution.

2 Comments

(a) you shouldn't be using grep, and (b) if you do decide to use grep, you don't need to use cat: grep -o Ubuntu /etc/os-release
Don't make use of nested if-statements. Just use case.
0

If you just need to check the name of the OS, you can try;

source /etc/os-release
echo "The operating system is: $NAME"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.