Search

Scripting-2 The first Script

Aim : To introduce the reader to writing shell scripts

The shebang: 


If you have seen shell scripts in the past. You would have seen that most professionally written scripts have some thing like #!/bin/sh or #!/bin/bash. This is called as the shebang.
A shebang basically tells the which shell interpreter should be used to interpret the script. A standard linux system would generally have all the shell installed in it, though it might use one of them as its default shell.
Let us assume that user A writes a script using the "bash" shell and gives it to user B whose linux system uses a C shell. The script would obviously fail to run in user B's system even if the system has bash shell installed. Because user B is using the C shell by default. Easiest way to get around this problem is using the shebang.
shebang basically points to the location where the executable of the interpreter  is located. So irrespective of which default shell your system uses, if the path in the shebang is correct the script will be able to run correctly as it will know which interpreter should be used.

What is a script: 


A script is nothing but commands put into  a file and executed together. The commands could be any of the commands available in Linux.

Assume you have to create 100 files in a directory. The command to create a file is touch "filename". 
Let us say you are creating the files by the name file1, fil2.. etc. If you have to do manually on the command line you would have to execute this command a hundred times. But in a script its just a matter of 3 lines

#!/bin/bash

for((i=0;i<100;i++))
do
touch file$i
done

Write the above lines into a file and save it as magic.sh in the directory where you want to create the 100 files.

Open the terminal and "cd" into the directory and run the script as follows
bash magic.sh


Do "ls" and you will a 100 files created in your directory.

Don't worry about how the script works, we will learn that as we go.

We can see that a script can save a lot of time, if you are going to do some work repeatedly.

A script is also helpful in automating work, as multiple commands can be run together in a script.


First Script: 


Lets start by writing a simple script. using the commands "echo". 


#!/bin/sh 
echo "Hello world"


Type the above lines into a file using "vi" or any editor that you are comfortable with and save the script as script1.sh

Open a terminal and "cd" to the folder where you have saved the script.

There  are two ways you can run a script.

1. You could pass your script file to the interpreter which are either "sh" for bourne shell or "bash" for bourne again shell, "csh" for cshell.
   i.e.  sh "scriptname"
          bash "scriptname"
           csh "scriptname".

Note: It is a good practice to save the script as "filename".sh  though the script would work even if not saved with ".sh" extension.

2. The other way of running the script is giving the execute permission to the script using the "chmod" command. i.e.
chmod 777 "filename".sh  

and then to run the script

./filename.sh 


For our first script let's try doing both.
First step before doing that would be to find out which shell are you running currently. The path to your default shell interpreter is stored in an environment variable SHELL.


echo $SHELL
/bin/bash


The above output shows that the shell is a bash shell. Your output might defer depending on what shell is being used.

Note: We will come to what variables and environment variables are a little later so don't worry if you did not understand what SHELL variable is all about. 


You can run the script1 now (assuming a bash shell) as follows.

Note: "$" will be used from here on to indicate the shell prompt. 


$ bash script1.sh
output: 
Hello world


The second way of running the script is to give execute permission to it.

$ chmod 777 script1.sh


Once the execute permission is given, we can run the script as follows.


$ ./script1.sh
Ouput:
Hello world 


Second Script
#!/bin/sh 
echo "Hello world" > file1
cat file1


Type the above lines into a file using the "vi" editor and save it as script2.sh

To run the script, open a terminal and "cd"  to the folder in which you have saved the script.  Run the script using the command

sh script2.sh 

What do you expect the output to be ?

We know that echo will send "Hello world" to the output, which has been redirected using ">" to a new file  called as file1.
The "cat" command sends the contents of the file1 to the output i.e the terminal.

So the output should be the contents of the file file1, which is "Hello World". So you should see "Hello world" printed on the screen when you run this script.



The only difference between the two ways of running the scripts are, in the former a sub shell is created in which the script gets executed, in the later method no sub shell is created.


Try it out:  
1. The command "uname" with various options gives information about your system.
For eg:  If you run the command "uname" with no options, it will display the current operating system.
"uname -m" will display what architecture your computer belongs to i.e i386 or i686 etc.
Write a script that will give the following output

Hello, the operating system in this computer is 
Linux
It belongs to the family 
i686

The second and fourth line of the output might vary depending on what is installed in your computer and what kind of hardware is present in the computer.

2. The command "who" gives the list of users currently logged in to your system.
Write a script that will give the following output

The users who are working now in my system are 
user1  tty7 2010-6-12 11:23 (:0)  


The second line of the output depends on your sour log in name, terminal etc and will vary from one system to another.


Following a good book to start learning shell scripting

Beginning Shell Scripting (Programmer to Programmer)

Follow by Email