You are on page 1of 40

UNIX Shell Scripting

TCS Confidential

1
1
Course Prerequisites
Familiarity with the fundamentals of the UNIX
operating system

TCS Confidential

2
2
Learning objectives

1. Introducing Shells entering commands, history, wild cards


2. Introducing Shell scripts alternatives, tools, input/output, variables,
comments, continuation of lines, magic line
3. Shell script constructs using variables, for, while, if, test, case
4. Interaction with the Environment3 environment variables
5. Best practices comments, error messages, debugging
6. Why write scripts? complicated commands, data management,
automation

TCS Confidential

3
3
The Unix Shell

The shell is your interface with the Unix system.

Is an interpreter of commands

Commands
Built in subroutines 4(do not require the kernel to
start another process to run them)
Non built in commands (require the kernel to create
a new child process to perform the command). The
shell waits until the child process finishes before
accepting the next command.

The exit procedure built in command. True/False TCS Confidential

4
4
The Unix Shell

TCS Confidential

5
5
Shells Entering Commands

1. Introducing Shells entering commands, history, wild cards


2. Introducing Shell scripts alternatives, tools, input/output, variables,
comments, continuation of lines, magic line
3. Shell script constructs using variables, for, while, if, test, case
4. Interaction with the Environment6 environment variables
5. Best practices comments, error messages, debugging
6. Why write scripts? complicated commands, data management,
automation

TCS Confidential

6
6
Shell Script What & Why?
1. What is Shell Script?
Shell Script is series of command written in plain text file. Shell script
is just like batch file in MS-DOS but have more power than the MS-
DOS batch file
2. Why Shell Script?
7
Automate frequently run tasks
Remember complicated command/command-line options
Filter through data and logs

TCS Confidential

7
7
Shell Script 3 simple steps
1. Use any editor like vi to write shell script.
2. After writing shell script set execute permission for your script.
Syntax: chmod <permission> <your-script-name>
Example:
$chmod +x your-script-name
8
$chmod 755 your-script-name
3. Execute your script
Syntax:
bash <your-script-name>
sh <your-script-name>
./<your-script-name>
TCS Confidential

8
8
Shell Script Wild cards
1. * - Matches any string or group of characters.
ls ut*.c will show all files having extension .c but file name must
begin with ut.
2. ? - Matches any single character.
ls fo? - will show all files whose names are 3 character long and
9
file name begin with fo
3. [] - Matches any one of the enclosed characters
ls [abc]* - will show all files beginning with letters a,b,c
4. [..-..] Matches range of character
ls [a-d]* - will show all files beginning with letters a,b,c,d
ls [^a-c]* - will show all files beginning with letters other than a,b,c
TCS Confidential

9
9
Shell Script Echo Command
1. Use echo command to display text or value of variable.
2. echo [options] [string, variables...]
Options
-n Do not output the trailing new line.
-e Enable interpretation of the following backslash escaped characters in
the strings: 10
Examples
\b backspace
1. Output Hello World.
\c suppress trailing new line
echo Hello World
\n new line
2. Output Hello World with no new line
\t horizontal tab
echo n Hello World
\\ backslash
TCS Confidential

1
10
Shell Script User Defined Variables
1. To define user defined variable (UDV) use following syntax
Syntax: variable_name=value
Example:
To define variable called 'vehicle' having value Bus
vehicle1=Bus
11
vehicle2=A Bus
echo The value of vehicle1 and 2 is :$vehicle1:$vehicle2:
To define variable called n having value 10
n=10
echo The value of n is $n
Note: Syntax for accessing/printing UDV is $variable_name
TCS Confidential

1
11
Shell Script Evaluation of UDV
$var value of var; nothing if var undefined
${var} same; useful if alphanumerics follow
variable name
${var-str} value of var if defined; else str; $var unchanged.
${var=str} value of var if defined; else
str; if undefined, $var set to str.
${var?str} 12 print
if defined, $var; else,
str and exit shell; if str empty, print:
var:parameter not set
${var+str} str if $var defined, else null.

TCS Confidential

1
12
Shell Script User Defined Variables
1. Rules for naming variable name
Variable name must begin with Alphanumeric character or
underscore character (_), followed by one or more Alphanumeric
character ( eg. HOME, SYSTEM_VERSION )
Don't put spaces on either side of the equal sign when assigning
13
value to variable. ( number=10 )
Variables are case-sensitive, just like filename in Linux. (no, No,
NO, nO are all different variable names)
You can define NULL variable as follows
vech= (or) vech=
Do not use ?, * etc. for naming variables.
TCS Confidential

1
13
Shell Script Referencing Variables
1. Variable is referenced by placing dollar sign in front of variable name
Syntax: $variablename
2. Variable can be referenced using curly braces.
Syntax: ${variablename}

#set initial value


14
myvar=unix
Echo $myvar #unix
Echo ${myvar} #unix
Echo {$myvar} #{unix}
Echo ${myvar}class #unixclass

TCS Confidential

1
14
Shell Script More
1. Exit Status of command or shell script?
echo $? ( zero return value indicates success, else failure )
2. Read statement
Use to get input (data from user) from keyboard and store (data) to
variable. Example
15
Syntax: echo n Please enter your name:
read variable1 read NAME
echo Welcome! $NAME !
3. To run two command with one command line.
Syntax: Example

command1;command2 date;who
TCS Confidential

1
15
Shell Script More
1. Commenting your script
Start each line with a sharp sign (#) #This is a comment line

2. Continuing Lines
Type a backslash, \, at the end of line to continue command on the next
line. name=Raghu
16
echo Value of name is: \
2. Magic Lines $name
First line of scripts and starts with #!
Tells the shell which program should interpret the script.
#!/bin/sh First line of the script
This line tells the shell to launch sh to run the script
TCS Confidential

1
16
Shell Script Shell Arithmetic
1. Use to perform arithmetic operations.
2. Syntax:
expr op1 math-operator op2
Examples:
$expr 1 + 3
17
$expr 2 - 1
$expr 10 / 2
$expr 20 % 3
$expr 10 \* 3
$echo `expr 6 + 3`
$echo `date`
TCS Confidential

1
17
Shell Script Command Line Arg.
1. Tells the command/utility which option to use.
2. Informs the utility/command which file or group of files to process
(reading/writing of files).
$ myshell foo bar

1 2 3 18

1 Shell Script name i.e. myshell ($0)


2 First command line argument i.e. foo ($1)

3 Second command line argument i.e. bar ($2)

$# - No. of Arguments (Here, 2) $* - Expands all argument


TCS Confidential

1
18
Input/Output Redirection

There are three types of file descriptors

Standard Input (0)


Standard Output (1)
Standard Error (2)
19
Input/Output redirection connects processes with files.

TCS Confidential

1
19
Shell Script Input/Output redirection
1. > - To output result (output of command or shell script) to file
Note: If file already exists, it will be overwritten else new file is
created

2. >> - To output result to END of file


20
Note: If file exists , it will be opened and new information/data
will be written to END of file, without losing previous
information/data
If file does not exist, then new file is created

3. < - To take input from file instead of key-board

TCS Confidential

2
20
Input/Output Redirection

Redirect input using the less-than sign.


$more < /etc/passwd

Redirect output using the greater-than sign.


21
$ls tmp > ls.out

Redirecting error using the symbol 2>

$sort < /etc/passwd > results 2>errors

TCS Confidential

2
21
Structured Language Construct - Loops

1. For Loop
2. While Loop
3. For each and every loop
First, the variable used in loop condition must be initialized, then
22
execution of the loop begins.
A test (condition) is made at the beginning of each iteration.
The body of loop ends with a statement that modifies the value of
the test (condition) variable.

TCS Confidential

2
22
Structured Language Construct For Loop

Syntax:
for { variable name } in { list } Example
do #print welcome 5 times
command1 for i in 1 2 3 4 5
command2
23 do
echo "Welcome $i times
last command done
done

TCS Confidential

2
23
Structured Language Construct For Loop
Looping over files

# Example to look at files in a directory and print out file names


for filename in *
do
echo $filename
24
done
# print out the file names ending in .doc
for filename in *.doc
do
echo $filename
done
TCS Confidential

2
24
Structured Language Construct For Loop
Nested for loops for fixed number of iterations
# Nested for loop
for i in 1 2 3 4 5
do
echo n Row $i:
for j in 1 2 3 4 5
25
do
sleep 1 # sleep for a second
echo n $j
done
echo # output new line
done TCS Confidential

2
25
Structured Language Construct While Loop

Example:
Syntax:
#print multiplication table for given number
while [ condition ]
if [ $# -eq 0 ] then
do echo "Syntax : $0 <positive number>"
command1 exit 1
fi
command2 26
n=$1
done
i=1
while [ $i -le 10 ]
Note: do
echo "$n * $i = `expr $i \* $n`"
Loop is executed as long as
i=`expr $i + 1`
given condition is true
done
TCS Confidential

2
26
Shell Script Structured Language Construct - if
1. If condition is used for decision making
if (condition1) then
Example
command1
#Testing commands
command2 if (ls) then
elif (condition2) then echo ls is true
command3 27
else
command4 echo ls is false
else fi
command4
last_command
fi
TCS Confidential

2
27
Structured Language Construct - test
1. test expression OR [ expression ] is used to see if an expression is true
or false.
2. If it is true it returns zero(0)
3. If the expression tests false then a non-zero value (1) is returned.
4. If test encounters an error, it returns number greater than 1
Example 28
#check if first argument passed to the script is a positive number
if (test $1 -gt 0) then
echo "$1 number is positive
else
echo "$1 number is negative
fi TCS Confidential

2
28
Structured Language Construct - test
Comparing Numbers
Test Usage
$x eq $y Returns true if x equal y
$x ne $y Returns true if x does not equal y
$x gt $y Returns true if x is greater than y
$x ge $y Returns 29
true if x is greater than or equal to y
$x lt $y Returns true if x is less than y
$x le $y Returns true if x is less than or equal to y

TCS Confidential

2
29
Structured Language Construct - test
Testing Numbers

# Number test # Number test


echo ***Equal Comparison*** echo ***Less than Comparison***
X=5 X=5
Y=10 Y=10
30
if (test $X eq $Y) then if (test $X lt $Y) then
echo X = Y. echo X < Y.
else else
echo X != Y. echo X > Y.
fi fi

TCS Confidential

3
30
Structured Language Construct - test
Comparing Strings
Test Usage
$s1 = $s2 Returns true if s1 equal s2
$s1 != $s2 Returns true if s1 does not equal s2
$s1 Returns true if s1 is not null
$s1 -z Returns 31
true if length of s1 is zero
$s1 -n Returns true if length of s1 is not zero

TCS Confidential

3
31
Structured Language Construct - test
Comparing Strings

#String test #String test


echo ***Null Comparison*** echo ***Length Check***
String=Unix Class String=Unix Class
if (test $String) then if (test z $String) then
32
echo We have non-null string echo Length of string is zero
else else
echo We have null string echo Length of string is NOT zero
fi fi

TCS Confidential

3
32
Structured Language Construct - test
Testing Files
Test Usage
-d filename Returns true if filename exists and is a directory
-e filename Returns true if filename exists
-f filename Returns true if filename exists and is a regular file
-r filename 33
Returns true if filename exists with read permission
-s filename Returns true if filename exists and is not empty (Len>0)
-w filename Returns true if filename exists with write permission
-x filename Returns true if filename exists with execute permission

TCS Confidential

3
33
Structured Language Construct - test
Using Binary and Not Operators
Test Usage
! Negates the test
-a Returns true if two tests are both true and
false otherwise. This is an AND operation.
# test negation 34
x=5
y=10
if (test ! $x eq $y ) then
echo x != y
else
echo x = y
fi TCS Confidential

3
34
Structured Language Construct - test
Creating shorthand Tests with [

Example
# Test equality of numbers
x=5
y=10
35
if [ $x -eq $y ] then
echo x = y
else
echo x != y
fi

TCS Confidential

3
35
Structured Language Construct Case Stmt.
# operating system quiz
Syntax:
echo Please enter your favorite OS,
case $variable-name in
echo n linux, windows:
pattern1) command1... .. read os

command2;; case $os in


linux)
pattern2) command1 ... ..
echo Wow.. U like Linux!
command2;;
;; 36
patternN) command1 ... windows)

.. command2;; echo Time to check for virus..ha ha ha!


;;
*) command1... ..
*)
command2;;
echo Why did u choose this $os?
esac ;;
esac
TCS Confidential

3
36
Shell Script Pipes & Filters
1. Pipes - A way to connect the output of one program to the input of
another program without any temporary file.
Syntax: command1 | command2
Example: $ ls -l | wc -l
Output of ls command is given as input to wc command So that it
will print number of files in current directory.
37
2. Filter - performs some kind of process on the input and gives output
Example : tail +20 < hotel.txt | head -n30 > hlist
Here head command is filter which takes its input from tail
command and passes this lines as input to head, whose output is
redirected to 'hlist' file.

TCS Confidential

3
37
Shell Script Environment variables
1. Environment variables are set by the operating system during the
startup of shell.
2. It is inherited by any program that is started.
3. It have a special meaning that applies to all scripts.
4. Special operation is required to set
38 an environment variable.
5. It is treated, in most respects, as normal shell variables.

TCS Confidential

3
38
Shell Script Environment Variables
Commonly available environment variables
Variable Usage
USER User name
PWD Current directory
PATH List of directories searched for commands
HOME Users home
39 directory

HOSTNAME Computers host name


LANG Specifies user locale

Note: set command lists all the variables currently set

TCS Confidential

3
39
References
Unix Shell Scripting Basics skillport course
Beginning Unix book by Paul Love et al.

40

TCS Confidential

4
40

You might also like