Software & AppsOperating SystemLinux

How To Check if Running Platform is Ubuntu or CentOS with Bash Script?

Ubuntu 10

In this article, we will guide you on how to determine if your running platform is Ubuntu or CentOS, using a Bash script. Bash scripting is a powerful tool that can automate tasks and provide system insights.

Quick Answer

To check if the running platform is Ubuntu or CentOS with a Bash script, you can use any of the three methods mentioned in the article: checking the /etc/os-release file, using the lsb_release command, or checking the output of the uname -a command. These methods will help you determine the operating system quickly and easily.

Understanding Bash Scripting

Bash (Bourne Again Shell) scripting is a Unix shell and command language. It is widely used for its ability to control job control and shell functions. Bash scripts are plain text files containing a series of commands that are run in sequence.

Checking the Operating System

There are several methods to check the operating system using a bash script. We will discuss three of the most common ones.

Method 1: Checking /etc/os-release File

The /etc/os-release file contains operating system identification data. We can use the grep command to search for the strings “Ubuntu” or “CentOS” in this file.

if grep -q "Ubuntu" /etc/os-release; then
 echo "The system is running Ubuntu."
elif grep -q "CentOS" /etc/os-release; then
 echo "The system is running CentOS."
else
 echo "Unknown distribution"
fi

In this script, -q is a parameter for the grep command that enables quiet mode. The grep command will not output anything, it will only return an exit status. If the exit status is 0, it means the string was found.

Method 2: Using lsb_release Command

The lsb_release command displays certain LSB (Linux Standard Base) and distribution-specific information. The -si option outputs the distributor ID.

if lsb_release -si | grep -q "Ubuntu"; then
 echo "The system is running Ubuntu."
elif lsb_release -si | grep -q "CentOS"; then
 echo "The system is running CentOS."
else
 echo "Unknown distribution"
fi

Here, we pipe (|) the output of lsb_release -si into grep, which searches for “Ubuntu” or “CentOS”.

Method 3: Checking the Output of uname -a Command

The uname -a command outputs all system information, including the operating system.

if uname -a | grep -q "Ubuntu"; then
 echo "The system is running Ubuntu."
elif uname -a | grep -q "CentOS"; then
 echo "The system is running CentOS."
else
 echo "Unknown distribution"
fi

In this script, we pipe the output of uname -a into grep to search for “Ubuntu” or “CentOS”.

Conclusion

Bash scripting is a powerful tool for system administrators. It allows you to automate tasks and gain insights into your system. By checking the /etc/os-release file, using the lsb_release command, or the uname -a command, you can easily determine if your running platform is Ubuntu or CentOS.

Remember to replace the echo statements with the actual commands you want to run based on the operating system. Always test your scripts in a controlled environment before deploying them. Happy scripting!

What is the purpose of a Bash script?

The purpose of a Bash script is to automate tasks and provide system insights in Unix-like operating systems. It is a plain text file containing a series of commands that are run in sequence.

What does the `-q` parameter do in the `grep` command?

The -q parameter in the grep command enables quiet mode. It suppresses the output of the grep command and only returns an exit status. If the exit status is 0, it means the string was found.

How can I run commands based on the operating system in a Bash script?

You can use conditional statements (if, elif, else) in your Bash script to run different commands based on the operating system. For example, you can use the if statement with the appropriate condition to execute specific commands for Ubuntu or CentOS.

Can I use these methods to check other Linux distributions?

Yes, you can modify the methods mentioned in this article to check for other Linux distributions. You would need to replace the string "Ubuntu" or "CentOS" with the appropriate string for the distribution you want to check.

Leave a Comment

Your email address will not be published. Required fields are marked *