A shell script is a file containing a series of commands in order of their execution.
When the shell script is executed, it runs all the commands one by one in order.
A shell script can be executed multiple times hence it saves the time of executing every single command one by one.
Some of the reasons why you need shell scripts are as follows:
i. To avoid repetitive work
ii. To automate frequently performed operations
iii. Combine complex and repetitive sequences of commands into a single, simple command.
In an earlier post, we have explained creating and running the shell script in Ubuntu .
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Linux related queries with bash shell which is the default shell in Debian.
In this context, we shall look into how to create and run a shell script in Debian system.
To do this, follow the steps given below.
To create a shell script, you only need to create a plain text file using any text editor like Nano, Vim, Gedit, etc.
Then in the text file, you will need to add the commands in the same sequence you want them to be executed.
Remember following points when creating a shell script:
a. Begin the shell script with “#!/bin/bash”
b. Each line is treated as a command
c. Start a comment line with #
We will be creating the script named “defaultscript” using the Nano editor; however, you can choose any editor of your choice.
Before creating a script file, make sure its name does not conflict with the commands.
You can check this by typing “which” followed by the script name:
$ which script_name
In our case, it would be:
$ which defaultscript
The output will show that there is no such command or program installed with the name “defaultscript”.
Therefore, we can proceed with this name.
In order to create the script named “defaultscript”, run the following command in Terminal:
$ sudo nano defaultscript
After running the above command, an empty file will be opened in the Nano editor. First, add “#!/bin/bash” at the beginning of the file.
Now add the commands you like to run in the script with each command in a separate line.
Here we want to create a script that can do the following tasks:
a. Print the IP address of our system.
b. List files in the Downloads directory.
c. Backup files from Downloads directory to the Documents directory.
So, we have written the following lines of commands in our script:
#!/bin/sh
echo "Now we are going to print IP address"
ip addr | grep ens37
echo "Now we are going to ping Google website"
ping -c 2 google.com
echo "Now we are going to backup our Downloads directory to Documents directory"
cp -rv ~/Downloads ~/Documents
After entering the commands in the script, save and close it.
Now you will need to make the script file executable in order to run it as a program.
You can so by running the following command in Terminal:
$ chmod +x script_name
In our example, it would be:
$ chmod +x defaultscript
After the execute permissions have been applied to the script, you can run the script as follows:
$ ./script_name
Note: If your script has been placed in some directory other than the current working directory, make sure to navigate to that directory first and then execute the script.
For instance, to run our script named “defaultscript.sh”, the command would be:
$ ./defaultscript.sh
After running the script, the shell will execute each command contained in the script in a sequence and will display the output.
Other than running the script using the ./script_name command, there are two more ways you can run the script, which is as follows:
$ bash script_name
$ sh script_name
This is how you can create and run a shell script in Debian 10 Buster system.
This article covers how to easily create a shell script and automate repetitive jobs in #Linux. Shell scripts are just a series of commands that you add in a file and run them together.
To write and execute a #script:
1. Open the #terminal. Go to the directory where you want to create your script.
2. Create a file with . sh extension.
3. Write the script in the file using an editor.
4. Make the script executable with command chmod +x
5. Run the script using ./
#Shell is a #UNIX term for an interface between a user and an operating system service.
Shell provides users with an interface and accepts human-readable commands into the system and executes those commands which can run automatically and give the program's output in a shell script.