Basic Unix Shell Script: A Tutorial

Shell Scripting is underlying core of Unix Operating System and if you haven't experienced the beauty of command line or shell interface of Unix while using that OS then you haven't seen Unix at all. Unix Shell Script can make your task much easier and fun at the same time as you can modify the way you want it to and you don't have to depend upon particular software do that work for you.
Why do we need a Shell Script for. You have a set of commands which you want your computer to perform and since you don't want to type one by one you combine all the commands in a Single file and you call it a script file and when you run it, Unix shell will run those commands in Sequence. Well it is just a basic to understand the Shell Script in Simple terms. But There are great things that Shell scripting can do and my aim is to make it fun while understanding it so that we will picture it easily in our mind. 

I'll try to write in a way so that any Layman can get it. It feels bad to see people stuck to old Windows system though I don't deny the User friendliness of Windows(because we have been using them from long), but one should try working on Linux or Unix System once and then you'll see the real beauty and power of using Open Source Technologies. If you are quite new to Unix System and just trying learn the Shell Scripting and you have Windows system installed, don't worry, you don't have to format your Windows to install Unix. Just install Virtual Box in your system  which you can get it from here, and install the Guest operating system of your choice.

You need to do the Following:

1. Install Virtual Box Software ( Its Free and Pretty Good)
2.Download Unix or Linux Flavor like Ubuntu,Fedora or Suse etc.. as per your choice(For a starter I would suggest installing Ubuntu.)You can get it from here
3.Install the Guest Operating System in your Virtual Box.

You also have a Choice to use the Windows based Unix Shell Script software to run the commands but i would suggest to install the Linux OS and enjoy it.

A basic introduction to Unix Shell before starting with Shell scripting is that a Unix shell is a command-line interpreter or in easier way its like a Language interpreter which you need when you don't understand any Language. So Shell helps interpreting our language to a Language which Computer understand and work as we command. There are many Shells available in Unix and if you want to go in depth about them you can get it here

We will use the basic terminal to run our shell script and if you want to know a bit about installing some software's in Ubuntu you can check it Here

Ok, lets start discussing scripting right away and we can talk about the commands used in the script on the way. Let's say you login into your Unix Machine and you want to check that how many people are logged in today. We will use different commands for that like who, date:
who: This Shell command tells us how many people are currently logged in. (Just For your Information, Unix is a Multi User Operating system so multiple users can be there at a time and if you are an administrator you may want to check who is currently logged in). The Output of this command is something like this   
pc       pts/0        2012-07-07 23:04 (:0)
pc:User name of the person Logged in.
pts/0:The Terminal Name of the User from where he is logged into the Shell.
2012-07-07 23:04Date of Login and the Time When User Logged in.
(:0)Terminal from where he is logged(Could be a Remote Machine Also)

Unix uses a system of Pipe where and Output of one Shell command can be used as an Input for other Shell command. You can compare it with multiple Garden Hose pipes tied to each other in a way lie water coming out from one end is entering the other hose pipe. How you want to use that output depends upon you. Like in the above case we need a total count of people logged in and so we apply pipe to our who command like this :
                                                  who | wc -l
Where :
 wc: Word Count and since we want to count the lines as each line tells us the user logged in so we use and option of word count i.e. -l . 

Unix shell give us another beautiful feature of using the options to further tweak the output we want to see. The point is, we need a direct answer to our question rather than all the options which a Unix command is giving us and so we use options and filters etc. And when you run the above command in terminal you'll see the output as number( Number of Users logged into you system) So in above case it will be 1.

Other Shell Command we will use is:
date: It's quite clear from the name itself what it will display and its output without any filter looks like this:
Sun Jul  8 09:06:19 IST 2012
Now lets combine the above two shell command and put them in a script file so that you don't have to run them individually. We will create the file first and you can create that through Unix Shell itself using cat command. Its executed as:
 cat > <file name>   

Name the file whatever you want and run the command like:
                cat >
I have put .sh in the end for my understanding that its a shell scrpit file. You can choose not to put that extension but i suggest you should do it for future easy reference. After running the above command you will see that terminal is just showing a blinking cursor which means its waiting for you to add the contents to the file you just created. So we will enter the commands we discussed earlier and its like this :
cat >
who | wc - l
 ^d is used in Unix Shell to tell that its the end of file and please go back to the prompt. And Bingo your first shell script file is saved and ready to run. But before running a shell script we need to make it executable also like we have and .exe or .bat file in windows. So type in Terminal
chmod +x
Where : chmod is a Unix command which tells to change the mode of the file. There are three modes of a file which are read, write and Execute written as r, w, x and + means that I want to add this mode to the file( so - means remove that mode). So the above file will make it executable. We can run our shell script now. And we run it as:
 And the output should be:
Sun Jul8 09:06:19 IST 2012
 Congratulations. You have just run your first shell script. So every time you feel like checking how many users are currently logged in, just run the script and bingo, count of people with date will be there on your screen. 

Lets do one thing more to make this script more useful. You want to track the total number of logins on that day for a particular month so its better to save it in a file also. So we do something like this:
./ > data.txt
 (Where > operator will send the output of the shell script to the file data.txt and if that file doesn't exist , it will create it automatically)
You just need to be a bit cautious with it because if the above file exist already it will overwrite all the contents without giving any warning.
So how to keep adding the data everyday. For that we use the shell script as:

./ >> data.txt
Where >> means that append my output to the previous output which is there. So check the file data.txt after a some days you will see something like this:

Sun Jul  8 09:06:15 IST 2012
Sun Jul  9 09:07:19 IST 2012
Sun Jul  10 09:07:19 IST 2012
The output will vary as per number of users logged in at the time you checked. So its like you want to track how many people were in office at around 9 a.m. logged in, this basic command will do the purpose.

This is just the basic and you could see the beauty of simple shell script.

No comments: