Wednesday, February 17, 2010

Shell Scripting Continued...

Hi Folks

Along with my previous commitment about posting further more on Shell Scripting and on request from few about same, here I am again with few more details on it.

In this blog, I would try to answer basic Why, How and Where about Shell Scripting.

I received ample of comments with basic yet very valid question that

Why Should I need to write Shell Scripts ?

Here are some usage I have tried to quote :

1. Operating Systems likes Linux and other Unix-like needs automated tasks. Shell scripts are extremely useful for achieving this.
2. Shell scripts are also employed extensively in the default installations of Unix-like operating systems.
3. In daily life, programmers might need to execute series of commands most often. Executing those several commands manually at a command line interface, Shell scripts allow it to be executed automatically. And that too without having to wait for a user to trigger each stage of the sequence.

For example, in a directory with three C source code files, rather than manually running the four commands required to build the final program from them, one could instead create a C shell script, and keeping in the directory with them, which would compile them automatically.

These types of programs are also called as Batch jobs.

4. A shell script can be used to provide a sequencing and decision-making linkage around existing programs, and for moderately-sized scripts the absence of a compilation step is an advantage.
5. Shell scripts can also be useful at a need where scripts requires executed :
  1. Interactively
  2. Once, at a future date and time
  3. Repeatedly on a fixed schedule
  4. Using an HTML form

Next, I would like to shed light on How to create shell Script ?

What all I would like to mention here to answer this question is that all that is necessary to create this script is to open a plain text editor (but not a word processor), such as vim, emacs, gedit, dtpad et cetera are all valid, any text editor will do. And type the series of commands to execute them. Thats it !! Its that simple !! For adding commands to be executed, you can refer my previous post on same topic. It would help you understand the syntaxes, variables, arithmatics to be used etc. in script.

Consider a very basic example of printing Date of the System :

Filename : myDate.sh

#!/bin/bash
set `date`
echo $1 $2 $3 $6

Alternatively, the above code could be copied from this page and pasted to a blank page opened by the text editor page using the standard keyboard or mouse copy and paste functions.

After saving this plain text file, with a file name such as date (or anything else desired), the script is complete and almost ready to run. Scripts are typically run by typing a dot, a forward slash and the file name (with no spaces in between) and then pressing the ENTER key. You would come to know further ways to execute the script from command prompt down below.

Thus, for example, if the above script were saved with the name morning, an attempt could be made to execute it by issuing the following command:

./myDate

Script will print the output as : sat Aug 5 2006

However, the script probably will not run, in which case an error message will appear on the screen such as bash: ./myDate: Permission denied.

This is because the permissions for the file first have to be set to executable. (By default, the permissions for new files are set to read and write only.) The problem can easily be solved by using the chmod command with its 755 option (which will allow the file creator to read, write and execute the file) while in the same directory as that in which the file is located as follows:

chmod 755 morning

Now the script is totally ready to run..


Finally Where would I execute the Script ? is the question remained..

Command Prompt !! This is what all I can answer to this. Ha ha. Yes, serious I am. Consider you have named your script as foo. (Well, I dont know why foo. :) Its just like that !! LOL ). So once you are done writing your script, you would simply go to the command prompt and type any of following command to execute your script..
  • $ bash foo
  • $ sh foo
  • $ ./foo


Finally, I would like to share one Shell Scripting example with Menu Program ::

fnMainMenu()
{
clear
echo "======= Menu ==========="
echo "Menu :"
echo " (1) Addition"
echo " (2) Quit this Menu"
echo "Enter your option : "
read response
case $response in
1) echo "Enter First Number : "
read num1
echo "Enter Second Number : "
read num2
sum=`expr $num1 + $num2`
echo "Addition : $sum "
;;
2) exit
;;
*) echo "I dont know what you are trying?"
sleep 2
;;
esac
}
#===== Start =====
#!/bin/sh
fnMainMenu


Try this program !!

Hope I have been successful in explaining the more basics about the Shell Script and was able to answer most of your doubts on this, so feel free to share your views on this.

No comments:

Post a Comment