Introduction to Shell Scripting in Linux
Computers in general understand the language of 0’s and 1’s which is known as a binary language. Decades ago, people used to write programs and applications like even calculators in binary languages. But that wasn’t pretty. Just imagine! You want to calculate 73+96, how would you write? You would have to rather calculate the binary integer for 73 and 96 and then add them. A person have to be pretty stupid to do that. One would rather calculate 73 and 96 on a paper and move on. But that is for the smaller part. What if you have to calculate something like (19273/23*[2967^3]). I would say now that this is pretty intense.
So, an indeal man learning to encode everything in binary language doesn’t make sense. That is the main reason why the computers weren’t popular in the 70s and the 80s. But later, even before the Windows was developed, people started developing kernels and similar stuff which were the core part of operating system. The kernels used to understand human readable format of a specific program, and then convert it into machine code. And this is where the programming languages emerged and changed the whole era of computer world. Applications were build in C and C++, and later on with Java, Python, Ruby and Perl.
As of now there are more than 50 programming languages all around. And that is atleast those which I know of. Let alone the fact that there are other programming languages which are even harder to understand than machine code. My point being, examples like Chicken programming and Malbolge. They are some serious nasty stuff to deal with.
Shell Programming….errr…Scripting?
Now you might be wondering why I haven’t mentioned Shell programming above along with the other programming languages. The main reason being Shell itself is not a programming language. It is a scripting language. Shell is just a user end interactive program which interprets any input from the user and gives the result provided that the command is valid. It then passes the program to the kernel to execute it. The Shell is not a part of Kernel, but instead it uses the kernel to do its job of executing and creating files.
Types of Shell Scripting Program
Now, unlike C, C++ or Java, there are several other types of shells that can be used for Shell Scripting. Following are the four types of shell available till date:
No. | Shell Type | Description |
1. | Bash aka Bourne Again Shell | This is the most common shell available on all Linux and debian based systems. It is open source and freeware. |
2. | CSH or C Shell | This Shell scripting program uses the C programming’s shell syntax and its almost similar to C. |
3. | KSH or Korn Shell | Korn is a Unix based Shell scripting program, initially based on the Bash Shell Scripting. This shell is quite advanced and its a high level programming language. |
4. | TCSH | There is no specific fullform of TCSH. It is as it is. TCSH is an advanced version of Berkeley Unix C shell. It again supports C style syntax |
As for today, we are not interested in all the above shells here. We are only interested in the Bash shell, since it is the most commonly used shell in the world today.
The Bash Shell
The Bash Shell scripting program is a high-level scripting language, similar to that of python. But if you have any experience with Python before, then you will actually find that Bash Shell Scripting is far more easy than any other scripting language. One can write scripts in bash much faster than in C, C++, Java or Python. The default terminal in today’s most Linux environment, say for eg:- Ubuntu, Linux Mint Cinnamon/ Rafaela, Fedora, Arch Linux or any other debian environment consists of Bash as the default Shell. You can however check all the shell environments present in your system by typing the following in the default Terminal:-
$ cat /etc/shells |
The ‘cat’ syntax in used to read text files. The types of shell scripting programs in a Linux, Unix or Debian environment is stored in the shell file within the etc directory. Each shell has its own unique set of syntaxes and their own set of built in functions. The DOS or windows also have their own shell known as the infamous ‘command prompt’ or the ‘powershell’ from windows 8.1 onwards, but it is still not as powerful as the bash. The cat syntax will give you the list of shells available on the system, however to find the default shell or to check the current using shell, one can use the following syntax in the Shell Scripting Terminal:-
$ echo $SHELL |
The ‘echo’ syntax is used to print any statement. Here the ‘SHELL’ is a variable assigned to print the current shell. The dollar sign after ‘echo’ is used to represent a variable in Shell Scripting.
Shell Scripting
So, now you know what a shell is, lets take a look at Shell Scripting and why it is known as that. Shell Scripting is nothing but a series of command in a simple text file ending with an extension ‘.sh’. The ‘.sh’ determines that it is a shell executable file. After writing a shell script, one would need to change the executable permission using the ‘chmod function’. By default, any newly created shell is not executable in the UNIX or the Linux environment unless the permissions are changed specifically. Shell Scripting is almost similar to that of Batch programming from Windows, but the difference being that Shell Scripting is rather more powerful and provides much more high-end interactive environment along with tab completions. One needs to make use of a good text editor to write a shell script. Few better ones that I know of are Vim, Gedit and Leafpad.
Unlike other programming languages, Shell scripts are quicker to write and execute. You don’t need to download any specific bundles or dependencies to execute. They can be used not only by a System administrator, but also by just a normal person for executing automated scripts on a daily basis. Following is a simple example of a script written here.
#!/bin/bash echo -e “Welcome to the first shell program.\n”echo -e “Write something here and this program will print back the same.\n” #asking the user for inputread input #reading the input from userecho “You entered: $input”exit 0 |
Now let me explain all the parameters in the above code. First of all, write the above code in a text file in a ‘as is’ format and save it with an extension ending with ‘.sh’. For example, name the file as ‘main_program.sh’. Make sure that it is not ‘main_program.sh.txt’. Most beginners tend to do the same and then it is not executable. Once the above code is copied, change its permissions using the following syntax in the terminal:-
$ chmod +x main_program.sh OR $ chmod 755 main_program.sh |
Feel free to use either of the above commands since both of them do the same thing. In the modern bash shell scripting programs, the ‘755’ is changed to ‘+x’; so it doesn’t matter which one you use actually. After you have changed the permissions, type the following to execute it.
$./main_program.sh |
The period and slash are used to identify and execute the shell scripts. Once you execute the above script, it will print the first echo statement and then will ask you to enter something. Once you enter it, it will print out the same thing back to you and exit the interpreter. So let me explain all the syntaxes above. The ‘#!/bin/bash’is used to tell the interpreter that this is a bash script and the Bash shell scripting program is located in the ‘bin’ directory. The ‘echo’ command is used to print any statement and the ‘-e’ flag is used to indicate the shell that there will be either a new line identified as a ‘slash n or \n’ or a tab ‘slash t or \t’ or the likes of it inside the echoed statement. The hash part on the second line is used to identify a comment in a bash script. Whatever you write after hash gets commented out and won’t be executed. But this does not apply to the ‘#!/bin/bash’ on the first line. This is how the script is meant to work. The ‘read’ syntax is used to read any input from the user. And the ‘input’ I have mentioned after the ‘read’ syntax, is the variable in which the input from the user is stored. One can use any variable instead of the input. On the second last line, the $input is used to identify itself as a variable and output the stored input inside it. Finally the exit command is used to exit the interpreter with a clean exit zero.
UnInteractive Scripting
Now the above example is just quite easy. But there are built in modules which you can use instead of giving all the inputs by yourself. Following is a piece of code, which prints out the user name which is currently logged in, the current time, the calendar date and the number of logins as well:-
#!/bin/bash # This script will print the current logged in user info, the number of users logged in, the date,the time and the calendar echo “Hello, $USER”echo “Today’s date and time is `date`”echo “Number of Users logged in : `who | wc -l`”
echo “Calendar” cal exit 0 |
Execute this script after changing the permissions of the file above. Now, you might be wondering that in the previous script, we actually entered the input and then it returned the input using th variable. But here, we have not provided any input inside the ‘$USER’ variable, then how come it prints the exact user. The main reason being these things are already configured in the system. Remember I told you about $SHELL previously at the start, as to how it is defined by default? Yes, similar is the case with the $USER here. As for the date, if you just type in ‘date’ without any quotes in the terminal, it will print out the exact date and time. I just used single quote ( ` ) to indentify itself as a syntax rather than a part of echoing statement. The single quote ( ` ) will execute any syntax printed inside it, be it in a statement or anywhere else. As for the number of users part, the ‘who’ command is used to check the current users logged in along with the logged in processes and the date and time as to when it was started. But as for the above command, I actually truncated it to just output the number of users logged it using th ‘wc -l’ command. The bar ( | ) or what it is popularly known as pipe is used to combine two commands. The pipe actually stands for much more than that, but I won’t be going into much detail here. And finally I printed the calendar using the ‘cal’ built in command for the printing out the current date in the calendar.
The Exit Zero
The Exit Zero is something which I haven’t explained in any of the above written programs. The main reason being ‘Exit Zero’ has much more gravity to it than any of the syntaxes above in Shell Scripting. By default, whenever you execute a script in a Linux or Unix distribution, it returns two types of values or more specifically the exit status to check whether the execution of the shell script is successful or not. If it returns the value as Zero, then the execution was successful, else it either did not get executed or there was some error during execution. Thus, typing in ‘exit 0’ at the end of any script checks the execution status of the script.
All the things I have covered here are just the least basic part of the Shell scripting in the linux distribution. The exit zero part and the pipe plays an important role when executing multiple shell scripting in linux commands and using it along with the conditional if and else statements.
Related Articles
Here are some articles that will help you to get more detail about the Shell Script Types For Linux Newbies so just go through the link.
16 Online Courses | 3 Hands-on Projects | 160+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses