Saturday, July 25, 2009

Starting with Shell Script

What is a Shell Script?

A shell script is a text file that is interpreted as a series of commands executed one after the other to achieve the desired result. It can cater for interactivity and complexity where decisions are required on certain conditions.

Here is an example of what a shell script looks like

#!/bin/bash
#
# Prog: Sample Shell Script, outputs Hello World
# Author : Jayant C Varma
#
echo "Hello World"

What does it all mean?
The first line indicates to the kernel that this is a script. So every script should start with the first line being #!/bin/bash , now if one was to use python or perl as the scripting language instead of bash, then the line would correspondingly change to #!/usr/bin/python or #!/bin/perl , The path that folows the #! is the path to where the interpreter lies, so if you have a custom language that you might want to use, you can specify the path to the interpreter and it would work.

The # is a comment, and it is a good idea to document and write on the top after the first line about the author and the function of the program.

The main guts of the program that outputs the data is the line echo "Hello World" that prints to the terminal the words Hello World.

Is that all that is required to make a script?
Yes, that is all that is required to make a script. Running the file on its own will not work as it is just a text file. So to run it we can use the command line

$ > bash myscript.sh

this shall run the script caled myscript.sh

To make the script run on its own, we need to midify the file from being an ordinary file to an executable file, we do so by changing the file permissions using the command chmod.

$ > chmod +x myscript.sh

Now the myscript.sh file is no longer an odinary text file, but an executable script file
to run the same, we can simply type, at the command line

$ > ./myscript.sh

We shall see in other posts why do we use the ./ before the filename instead of just typing the myscript.sh, an exercise for you, try it out and see what happens if you do not include the ./

2 comments:

  1. Hi Jayant,

    A few questions:
    Q1. This script assumes that echo is within $PATH.
    Is it generally safe to assume that /bin and /usr/bin are in the $PATH variable, or should we always check what is in the $PATH variable before writing a script (e.g. echo $PATH)?
    Alternatively, should we generally declare the location of any commands we use? e.g.

    #!/usr/bin/sh

    ECHO="/bin/echo"
    $ECHO "Hello World"

    Q2. What is the general standard for writing variables in a shell script? e.g. If I wanted to declare the variable "eat this", would I declare it as eat_this, eatThis, EAT_THIS, etc.?

    ReplyDelete
  2. @Robert:
    1. Generally echo is a command that would be in the path, if we start implicitly including static paths, then we are doing away with the portability and for a simple Hello World script, we are complicating it to an extent that beginners would shy away from this or get totally confused.
    The shell would be in the path for the simple reason that it is what you have logged in with.

    You can make a script Iron Clad with variables and static paths...or... ;)

    In the past semesters, there have been students that have written a script like a Java Program. Shell Scripts can handle that kind of "Torture", but they were meant for quick administrative fixes without resorting to coding.

    2. There is no standard as when shell script was created there were very few things called standards in computing. However you can use the Hungarian Notation for the example you have cited above. You can even take it a step further if you want as iThisIsAVeryLongVariableNameForAnIntegerValueHolder and sAComparitivelySmallerVariableName.

    Hope this helped you ;)

    ReplyDelete