What is a Shell Script?
A shell script is a Linux based script that has commands written into it and when the user executes the script, all those commands that are in the script are then executed one by one. Now think of it like this, you have a task that you need to do, and it requires a certain number of commands that the user needs to write so it’s difficult to always write those commands individually and then execute them, that’s where the shell script comes in.
Now to do that task you just write all those commands in a single script and save that script file somewhere. Then whenever the user needs to do that certain task, he just needs to execute the saved script and it will be done without individually writing all the commands again. The shell is an interpreter of the commands that the user writes.
How to write a Shell Script?
We will be using one of the built-in editors in Ubuntu 18.04 LTS to write a simple script. The editor is called ‘Nano’ and the shell scripts have a “.sh” extension. File extensions on Linux are optional, but it is a good practice to name shell scripts with .sh extension.
Now in the very beginning, you have to type “Nano” in the terminal window to open the text editor. It is usually already installed in Ubuntu.
The above command will open the Nano editor that will look something like this:
The script usually starts with #!/bin/bash so you first need to write this. So, the list of commands goes as follows:
#!/bin/bash echo "welcome" ls echo "this is the whole list of dir"
When you are done with the commands that are mentioned above then you press CTRL + X to save the script and exit. after that the system will ask you for confirmation and then it will ask you to write a name for the file. I will be saving the file as “directories.sh”. It will look like this:
Press “y” to confirm.
After you do this the editor will exit and save your script.
Till here you will have successfully created a simple script, now the script has default permission of rw – -r- -r (the first flag is for the current user, the second one is for user groups and the third one is for others). The permission format consists of binary numbers that represent permissions. The basic permissions are 4 2 1.
- Read- read permission is assigned to 4
- Write- write permission is assigned to 2
- Execute- execute permission is assigned to 1
So, to execute the script you saved you need to change its permission to 7 7 4. the concept of this is that if you want to give an rwx (read, write, execute) you will have to add 4(read)+2(write)+1(execute) that will sum up to 7. so the permission of that script needs to be 774 for the user to execute it.
You can check the permission of a specific file by the following command:
ls - l directories.sh
Now, to change the permission of the script you saved, you will have to write the following command in the terminal:
sudo chmod 774 directories.sh
After you press enter it will ask you for the current user’s password, after entering the password it will change the permissions for the file.
Now, to run the script you just have to type “./nameofscript”.
You will get something like this as a result of executing the script. Now, if you followed all the steps carefully and as mentioned above then you will have created a script and executed it successfully.
To make it easier to automate the scripts, you create an alias for the script you made.
First of all, you need to make a file called .bash_aliases in your home folder. You will have to enter the following command in the terminal:
Once it is created, open the file by using the command:
After you enter the above command Nano will open and look like this.
Then you will have to type:
alias dir= ".directories.sh"
In this way, an alias for the script you made will be saved and this alias saves the user the trouble of writing “./script.sh” every time you need to execute the script.
So now after creating the alias you just need to write “dir” in the terminal and the script will be executed.
So, this concludes the article, we have covered almost everything about shell scripts. If you follow all the steps carefully and as they are in the guide you will successfully have created your first simple script. Now you know what shell script is, you know how to make a script, you know about permission system of files and you know how to make aliases. So good luck making more scripts that will help you further.