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.