While working in Linux OS, there are some commands that you need to run quite frequently.
Instead of typing and executing those lengthy and repetitive commands one by one in a shell, a shell script allows you to run all those commands automatically.
All you require is to put all those commands in a file and then run that file.
You can execute a shell script multiple times which helps to saves time in executing every single command one by one.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform CentOS related queries.
In this context, we shall look into how to create and run a shell script in the CentOS system.
Importance of using shell scripts:
1. Monitoring various tasks like disk usage, uptime, ans so on.
2. Automating backups, user account creation, etc.
3. Launch multiple programs with a single command.
Steps to Create and Run a Shell Script:
Here are the steps you need to follow to create and run a shell script in CentOS.
1. Launch terminal and move to the directory where you need to create the shell script.
2. Use any text editor like Nano, vi, etc to create a new file with a .sh extension.
3. Now enter commands you want the script to run.
4. Make the file executable with command chmod +x <script_name>.
5. Run the script using either ./<script_name>, bash <script_name>, or sh <script_name>.
Now let's explain the steps briefly.
1. Launch Terminal
Firstly, you will need to launch the Terminal in your CentOS system.
To do so, hit the super key on the keyboard, and then from the left panel that appears, click the Terminal icon.
2. Create the Shell Script
i. First, move to the directory where you need to create your script. You can do so with the cd command followed by the directory name as follows:
$ cd directory_name
We will create the script in the Home directory which is our current working directory.
Now, create a new blank file with .sh extension. You can do so with the use of any text editor.
ii. To create the script file using the Nano editor, use the below command:
$ sudo nano script_name.sh
For instance, the command to create the script file named "linuxapt.sh" would be:
$ sudo nano linuxapt.sh
This will open a new file named "linuxapt.sh" in Nano editor.
3. Enter commands in your script
Now in the new file you have created above, enter the commands in the same order you want your script to run.
Also keep in mind the points below, when you write a shell script:
a. Start writing the shell script with "#!/bin/bash".
b. Enter each command in a new line.
c. To add a comment, start it with #.
Now in the script file, add "#!/bin/bash" in the first line and then add commands (each in a separate line) you want to run by the script.
Here we are going to create a simple script that will print a message on the shell using the "echo" command.
For this purpose, we have added the following command in the script:
echo "This is a simple script to print a message"
You can add any commands of your choice. Once done, save and close the script file. To save the file, press Ctrl+O and then press Enter.
Then to close the script, press Ctrl+X.
4. Set Execute Permissions
Now you should make your script file executable, which can be done using the below command:
$ sudo chmod +x script_name.sh
For instance, to make "linuxapt.sh" executable, the command would be:
$ sudo chmod +x linuxapt.sh
5. Run the Shell Script
Now your shell script is ready to run. There are three different commands to run a script which are described below. You can use either of these commands for running your shell script.
Make sure to replace the script_name.sh with your script name:
$ bash script_name.sh
$ sh script_name.sh
[Need urgent assistance to install missing packages on CentOS system? We can help you. ]