Cracking IT Interview

HOME | NEXT ->

What is shell script ?


A shell script runs in Interpreter mode, here code is executed line by line, not like whole code is going to be compiled at one shot like C and other high level languages (HLL).


Shell script is comparatively slower than the programs written in C or other HLL.


When a group of commands need to be executed on regular bases, we store it in a file. We can use this file as shell script by adding few lines of code. We run this shell script instead of running multiple commands. So, shell script can be considered as a collection of Unix commands with some scripting logic and syntax.







Starting with first line:


#!/bin/sh


  • We call this line as Hash Bang line or Interpreter line.
  • It tells the interpreter to execute rest lines of code with the shell mentioned in this line.
  • Special directive "#!" is treated a magic number and it indicates the absolute path of the shell "/bin/sh" where path starts from root "/". That's why we call this line as magic line as well.
  • Shell "sh" (here Bourne Shell) is nothing but a command line interpreter which executes commands.
  • Shell types:
    • /bin/sh - "Bourne Shell"
    • /bin/bash - "Bourne again shell" or simply "Bash shell"
    • /bin/ksh - "Korn shell" 
    • /bin/csh - "C shell"
    • /bin/tcsh - "Tab C shell"


How to execute shell script ?


sh testscript.sh


  • Specify the shell interpreter in the command line and give the script name. It ignores the shell mentioned in the Hash bang line and execute the code with specified shell in the command line.


./testscript.sh


  • "dot and forward slash" with script file name will execute with given shell in Hash bang line.





Giving Comments:


It's good practice to use comments in script. whenever there are lots of scripts stored in the working environment and scripts having various lines of codes, it becomes easier for anyone to go through the codes. It increases the readability and comfort for high level overview of the script for its functionality.


single line comment:

#This is single line comment 


you can use multiple line comments like C:

/* this is

multiple line

comments*/


Printing output on screen:


We print output with echo "statement". Do not use ";" at the end like C.

echo "Hello World"


Taking Input:

We use read statement to take input value to a variable.

echo "Enter a number"

read num         #num is a variable

echo "you have entered $num" #printing your entered number


There are two ways to take input into a shell script:


  • Interactive mode: Using read command to make script interactive and you have to enter the value manually at run time.


Example script: sum of two numbers

[Vikas ~]$ vi sum.sh
#!/bin/sh
echo "Enter first number : "
read num1
echo "Enter second number : "
read num2
sum=`expr $num1 + $num2`
echo "Sum of given numbers = $sum"


output:
Enter first number :
5
Enter second number :
6
Sum of given numbers = 11


  • Non-interactive mode: Using command line arguments with shell script name while running the script at the shell prompt.


Example script: sum of two numbers
[vikas ~]$ vi add.sh
#!/bin/sh
read $1,$2
sum=`expr $1 + $2`
echo "arguments entered = $1,$2"
echo "Sum of given arguments = $sum"


output:
[vikas~]$ sh add.sh 5 6
read: 5,6
arguments entered = 5,6
Sum of given arguments = 11


Some more on Command line arguments:


  • Shell script runs non-interactively in spite of that it is able to pass value. Very useful when scripts are scheduled to run periodically live in business environment.
  • when arguments are given along with the shell script, they go to assign to a special containers called "Positional parameters"
  • The first passing argument is read by parameter "$1", second is read by "$2" and so on till "$9".
  • If the of arguments we are passing is more than 9, then use "${10}", "${11}", and so on till any number of arguments.
  • So, positional parameters starts from $0,$1, $2,...…..$9,${10},${11},.... where "$0" holds the command/script name itself.


Positional Parameters and their use:


  • $0 : holds name of the executed command
  • $1,$2...$9 : holds the command line argument
  • $# : holds number of arguments given in command line
  • $* : holds set of parameters as a single string
  • $@ : holds set of parameter where each quoted parameter in treated as separate argument
  • $? : holds exit status of the last command
  • $! : hold PID of the last background job
  • $$ : holds PID of the current shell


Example script: to print the positional parameters
[vikas ~]$ vi pptest.sh
#!/bin/sh
read $1,$2,$3
echo "you have entered parameters = $1 , $2 , $3"
echo "total numbers of parameter entered = $#"
echo "script name / command = $0"
echo "parameters value = $*"
echo "parameters value = $@"
echo "exit status for last command = $?"
echo "-----------------------------------------------"
echo "PID of current shell = $$"
echo "PID of last background job = $!"
echo "-----------------------------------------------"


output:
[vikas ~]$ sh pptest.sh 10 20 30
read: 10,20,30
you have entered parameters = 10 , 20 , 30
total numbers of parameter entered = 3
script name / command = pptest.sh
parameters value = 10 20 30
parameters value = 10 20 30
exit status for last command = 0
-----------------------------------------------
PID of current shell = 44570
PID of last background job =
-----------------------------------------------


Example script to differentiate $* and $@:


[vikas ~]$ vi pptest2.sh
#!/bin/sh
echo "---------------------------------"
echo "using \$* in below loop"
echo "---------------------------------"
for i in $*
do
echo "value of i = $i"
done
echo ""
echo "---------------------------------"
echo "using \$@ in below loop"
echo "---------------------------------"
for i in "$@"
do
echo "value of i = $i"
done


output:


[vikas ~]$ sh pptest2.sh 10 "20 30" 40 "50 60 70"
---------------------------------
using $* in below loop
---------------------------------
value of i = 10
value of i = 20
value of i = 30
value of i = 40
value of i = 50
value of i = 60
value of i = 70
---------------------------------
using $@ in below loop
---------------------------------
value of i = 10
value of i = 20 30
value of i = 40
value of i = 50 60 70



HOME | NEXT ->