Shell Scripting Delving deeper: Tutorial 1
Shell Scripting Delving deeper: Tutorial 1

CALL NOW FOR FREE CONSULTATION

+91 9983340133, +91 9001997175

grras

Shell Scripting Delving deeper: Tutorial 1

In this series of blog we will try to delve deeper into shell scripting, starting from the basics. Shell scripting helps to automate tasks of repetitive nature and is a boon for system administrators.
Shell Scripting is the combination of Linux/Unix commands with the various control structures (like if condition, for loop & while loop) in a logical way to achieve a particular task e.g. file/data manipulation, program execution etc.
I will be using Red Hat Linux version 7 to execute the shell scripts and shell will be bash shell. According to Wikipedia “Bash is a command processor that typically runs in a text window, where the user types commands that cause actions. Bash can also read commands from a file, called a script. Like all Unix shells, it supports filename globing (wildcard matching),piping, command substitution, variables and control structures for condition-testing and iteration. The keywords, syntax and other basic features of the language were all copied from sh. ”
Always remember Linux commands are case-sensitive. To be good at shell scripting you need to be well versed with the various commands. The better you know the commands ,the control structures, and the logic, it is directly proportional to your acumen in shell scripting.
You can use any editor like vim or gedit to write shell script. I generally prefer vim editor as it is easily available on all the platforms.
The extension of file should be .sh . We will be writing a shell script to print the user logged in, date & time and No. of users logged in. To create a shell script file on the terminal type
#vim Myfirstscript.sh

Now let’s bifurcate the above program step by step.
The first line in the script file is always #!/bin/bash It states that the script will be passed to the bash shell for execution. All comments start with the # symbol, however ! sign and bash path after # bypasses the comment rule. Commenting is a good practice and it helps others reading your script, to understand the logic better.
Clear command will clear the terminal of any commands and their outputs.
echo command prints a string. The USER is a global variable that stores the name of the currently login user. The $ symbol fetches the value stored in a variable. So $USER will display the value stored in USER variable.
All the commands are written in a parentheses (). The date command will display the current date and time. The who command shows the users logged in , it is further passed to wc command to count the number of users logged in.
Make sure that the script file owner should have execute permission. You can do so with the chmod command on the terminal:
#chmod 755 Myfirstscript.sh
To run the script the command is sh followed by the script file name having .sh extension.
#sh Myfirstscript.sh
The output will look like this:

Feel free to post your queries or suggestions. Starting from the basics we will move ahead into deeper aspects of shell scripting.
By-
Dhiraj Bharara, RHCSA, RHCE,
Trainer, Grras solutions pvt. ltd., Jaipur

Leave a Reply

Your email address will not be published. Required fields are marked *