Home > Mobile >  How to detect OS system using BASH?
How to detect OS system using BASH?

Time:01-17

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?

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

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

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