Issue
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?
Solution
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
Answered By - larsks Answer Checked By - Pedro (WPSolving Volunteer)