Breaking News

Understand the basic concept of writing a decent Script in Linux OS

The Shell Script is a text file that contains a list of commands listed in the order of execution. From this definition we can say that anything that can run/execute a bunch (group) of commands can be considered as a shell script.
There are some rules that has been defined to guide you on how to write a decent scripts, not only do the task that you have written for them but also be easy readable by others. This will make you happy when you write a long script that obey all the basic requirements of readability. The following are the rules that make the decent to your script. So when you write a script, make sure you meet the following requirements.
  • Give it a unique name.
  • Start/Include the shebang (#!).
  • Include comments.
  • Use the exit code.
  • Make your script executable.
From the above description, we can write a decent script by following those rules. For example, I want to write a shell script that will create 10 directories. The following steps will guide us to write that script...!!
I'm using a Kali Linux but don't worry, any version apart from this Kali the command can work.
  • Open your Linux Machine
  • Now Open terminal (This will be used to write a script)
  • I want to create those directories on my Desktop, so I will type the following command to navigate to Desktop.
  •  cd /root/Desktop (This will change the current directory to Desktop. Note: It is not necessary to navigate to Desktop to create a script on Desktop, any where you can access Desktop as long as you know the Path to Desktop)
  • Now we are at Desktop, let us create a script, name it as Concept. To do so, type the following command and press Enter. (As we said earlier, script is a text file, so we are using Vi Editor to write the content of shell script)
  • vi Concept (Vi is a command that will start a Text Editor of Vi and Concept is a script name that will be written with Vi Editor)

  • After pressing Enter, new shell will be opened. This is a Vi Editor where we will type all contents of Script. It has two mode of writing, by default it open with Command Mode but you can Change by pressing ( i ) on your keyboard to enter to Insert Mode (Input Mode). Here we are going to use Insert Mode, so after open Vi, press ( i ) to change mode to Insert Mode.

  • After pressing ( i ), type the following codes.
#!/bin/sh

#This script will create 10 directories
#These directories will be differentiated by numbers
#Example, Directory1, Directory2,....... Directory10

mkdir Directory{1..10}
exit 0
  • Now let us discuss the meaning of each line from the script.
  • #!/bin/sh (This line has two parts, the first is ( #! ) this is called shebang, it tell the current shell which subshell should be executed when this script is running. The second part is/bin/sh ) this is the name of the subshell which will execute the script when it run, but you can use any other shell if you'd want)
  • #This script will create 10 directories..... (Those 3 lines that start with (#) sign and has an orange color means comment. Comment are line(s) that tell a reader the meaning of a certain part in your script
  • mkdir Directory{1..10} (This line has three parts, first part ismkdir ) this is a command that is used to create a directory, second part isDirectory ) This is the name of directory that is going to be created and the last part is {1..10} ) this is like a loop, it is readable like 1 up to 10, so it will create the first directory with 1 value, because it is a loop, it will come again to execute value 2, again 3, and again until the range from 1 to 10 has complete. The 1 value represent the initial value and 10 represent final value)
  • exit 0 (This line has two parts, first part isexit ) this command is used to exit and tells the parent shell that the script has been executed, and the second part is0 ) this is a value that represent successfully so when a parent shell read exit 0 it know the script executed successfully. If it reads anything other than exit 0 it know there was a problem)
  • After that, you have to press (Esc) button to back to Command Mode and type the following command to save and exit
  • :wq (This (:w) means saving the content of the editor and (:q) means quit, so we combine together to get the meaning of 'Save and Quit'. Note: You must save first then you may quit otherwise your script will not be saved)

  • Now, let us make this script be executable and run as a normal command. To do so, type the following command.
  • chmod +x Concept (chmod command for change permission, +x means add an executable permission, Concept  means the name of File. So, now we translate the command by saying, add the permission of execution to Concept file)
  • Now the Script is ready to run. Note if your Script is in executable format, in order to run it you have to write the full path to where your script is. From this example, our Script is in Desktop, so we have to write a full path to Desktop. You can use pwd command to see the path if you are at the same location with the Script.
  • Above, I used ls command to see it and it looked in white color. Now let us see it after converted to executable file.
  • As you can see, the color change to Green. Now, this is an executable Script
  • Finally, type the following command and press Enter to run the Script
  • bash /root/Desktop/Concept
  • Let us confirm if 10 Directories has been created on the Desktop. Type the following command to see them.
  • ls
  • As you can see, 10 directories has been created on Desktop.
  • You can run Script on other way but we will use them on other posts when we deal with Scripts.
Please comment and share with your friends.
Thank you!!

2 comments: