UNIX Shell Scripting - by Dinesh Kumar S
UNIX Shell Scripting - by Dinesh Kumar S
Contents
Chapters
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Chapter 11
Topic
Introduction
SSH Client
Unix Shells
Text Editors
A Beginning to Shell Scripting
Operators
Variables Manipulation (Advance)
Conditional Statements
Looping Statements
Control Statements
Functions
Page no.
3
4
8
11
19
33
39
43
47
74
79
2
Unix Shell Programming by Dinesh Kumar S
Introduction
Chapter 1
Linux:
3
Unix Shell Programming by Dinesh Kumar S
SSH Client
Chapter 2
Secure Shell (or) SSH is a network protocol that is used to exchange or share information
between two different networks.
This is used on Linux & UNIX systems to access SHELL accounts.
All the information exchanged/transmitted between networks is encrypted.
It uses public key cryptography to authenticate remote computer user.
Putty
Tera Term
Putty:
It is a terminal emulator application which acts as client for SSH, Telnet, rLogin.
Download: http://www.putty.org/
Tera Term:
Its an open source terminal emulator. It emulates different terminals from VT100 to VT382.
It also supports telnet, SSH1, SSH2 and serial connections.
Download: http://hp.vector.co.jp/authors/VA002416/teraterm.html
4
Unix Shell Programming by Dinesh Kumar S
Putty:
Double click Putty.exe then,
Input the server IP Address and open the session. Provide Username/password.
5
Unix Shell Programming by Dinesh Kumar S
Tera Term:
Double click TeraTrain.exe then.
Input the IP Address. Then select the options as per the screen shot below and press ok.
6
Unix Shell Programming by Dinesh Kumar S
7
Unix Shell Programming by Dinesh Kumar S
Chapter 3
UNIX Shells
UNIX shell is a command line like DOS in Windows. Its a user interface for UNIX operating
system. Mainly shells are used for inputting user OS commands.
It is called Shell because it hides all the information behind the shell interface.
Types of Shells:
C Shell (csh):
It is called C shell because the syntax used here is similar to c language. It adds many features
compare to bourne shell. This shell is not widely used now. It was developed by Bill Joy.
8
Unix Shell Programming by Dinesh Kumar S
Finding Shell:
How to find which shell we are working at?
There is a simple command to check which shell we are working at.
echo $SHELL (or)
echo $0
9
Unix Shell Programming by Dinesh Kumar S
Switching Shell:
We can switch between shells in two ways:
Temporarily
Permanently
Temporarily:
By default we will be using bash shell. After logging into the UNIX we can change to other shell
in same session. This is done by entering the name of the shell.
zsh - Z Shell
scsh Scheme Shell (not found)
dash Debain Almquist Shell (not found)
10
Unix Shell Programming by Dinesh Kumar S
Permanently:
To change the shell permanently whenever we log in into the UNIX use the command chsh
(Change Shell).
For these changes to be done we need to modify shell and environment variables.
I have changed the shell from bash to csh (c Shell). When I check the current shell for the
current session it is bash.
Now log in again & check which shell you are in for current session, it will be csh.
As I am using SSH client, we cannot change the shell.
11
Unix Shell Programming by Dinesh Kumar S
Chapter 4
Text Editors
Text editors have similar functionality like word processors. Several text editors are available
in Linux. We are going to see only major ones.
Editors:
Vim
Pico
Emacs
Joe
Vim:
Its an improved version of Vi. It is also called as programmers editor. Also it contains many
power tools.
Pico:
Its a simple text editor with pine e-mailer. It is very easy to use & powerful.
Emacs:
Its an extensible & customizable editor. This editor is user friendly & supports many languages.
Joe:
Its a full featured terminal based editor. It is very similar to WordStar & Emacs word
processor.
12
Unix Shell Programming by Dinesh Kumar S
Help Commands:
Ctrl + h
Ctrl + h t
help command.
help with tutorial
13
Unix Shell Programming by Dinesh Kumar S
File Commands:
Ctrl x + Ctrl f
Finds file, it prompt for a file name & loads the file into editor.
Ctrl x + Ctrl s
14
Unix Shell Programming by Dinesh Kumar S
Cursor Movements:
Ctrl + a
Ctrl + e
Ctrl + n
Ctrl + p
Esc + f
Esc + b
15
Unix Shell Programming by Dinesh Kumar S
Ctrl + f
Ctrl + b
Ctrl + v
Esc + v
Ctrl + d
Delete a char.
Esc + d
Delete word.
Ctrl + k
Kill line.
Ctrl + @
Set region.
Ctrl + w
Kill region.
16
Unix Shell Programming by Dinesh Kumar S
Ctrl + y
Esc + w
Copy region.
Search Commands:
Ctrl + s
Search forward.
Ctrl + r
Search backward.
17
Unix Shell Programming by Dinesh Kumar S
Esc + %
18
Unix Shell Programming by Dinesh Kumar S
Chapter 5
Rule to write Shell script:
Write Script
Give Execute
permission to user
Run Script
Debug (optional)
Write Script:
Create a shell script using a text editor (Emacs). Save the script file as,
.sh
.bash
Example: emacs my_first_script.bash
emacs my_first_script.sh
19
Unix Shell Programming by Dinesh Kumar S
20
Unix Shell Programming by Dinesh Kumar S
Run Script:
Run the shell script as below,
Bash <script_name>
Sh <script_name>
./<script_name>
Debug:
If there is an error in shell script, to find out the error we need to type the following command
with options.
Bash <option> <script_name>
Sh <option> <script_name>
Options:
v Print script line as they read.
x While executing command it expands system variables and arguments.
21
Unix Shell Programming by Dinesh Kumar S
22
Unix Shell Programming by Dinesh Kumar S
23
Unix Shell Programming by Dinesh Kumar S
Note: While initializing a variable there should not be any space between variable, operator and
value.
We can display multiple variables in single echo command.
24
Unix Shell Programming by Dinesh Kumar S
echo Command:
This command is used to display variable values or texts.
Syntax:
Echo
25
Unix Shell Programming by Dinesh Kumar S
Quotes:
Types of quotes:
Double quote
Anything inside double quotes removes meaning of string except \ and $.
Single quote
Anything inside single quotes remains unchanged.
` `
Back quote
Anything inside back quote executes command.
26
Unix Shell Programming by Dinesh Kumar S
Exit Status:
As we know we can embed a shell command inside shell script. If we want to know about the
status of the executed command i.e. whether it is success or failure we are going to use exit
status.
Syntax: exit $?
Zero (0)
success
Others
Error
27
Unix Shell Programming by Dinesh Kumar S
28
Unix Shell Programming by Dinesh Kumar S
Redirecting input/output:
Types: There are 3 redirection types as follows:
1) >
2) >>
3) <
>:
>>:
This will append the output to existing file at last. If data exists it will be left if not it
will be added. Example: ls >> out_redirect1
<:
29
Unix Shell Programming by Dinesh Kumar S
ls >> out_redirect1
30
Unix Shell Programming by Dinesh Kumar S
Pipe: (|)
Pipe is used to link output of one program as input to another program.
Example: ls | cat
As above the output of ls is given as input to cat command.
Example 2: w | sort > out_redirect3
The output of w i.e. users will be sorted & output will be redirected to the file.
31
Unix Shell Programming by Dinesh Kumar S
Output:
32
Unix Shell Programming by Dinesh Kumar S
Operators
Chapter 6
Test Operators:
Test Operators
-e
-f
-b
-c
-p
-s
-r
-w
-x
-g
-u
-O
-nt
-ot
True then
File exists
File is normal file not directory or system or device files.
File is blocked device
File is character device
File is pipe
File is symbolic link
File has read permission
File has write permission
File has execute permission
Group id flag set to file
User id flag set to file
User is owner of file
Newer than (f1 nt f2)
Older than (f1 ot f2)
Comparison Operators:
Numbers:
Operator
-eq
-ne
-gt
-ge
-lt
-le
<
<=
>
>=
Description
Equal
Not equal
Greater than
Greater than or equal to
Less than
Lesser than or equal to
Less than
Less than or equal to
Greater than
Greater than or equal to
Example
if [$var1 -eq $var2]
if [$var1 ne $var2]
if [$var1 gt $var2]
if [$var1 ge $var2]
if [$var1 lt $var2]
if [$var1 -lt$var2]
if (($var1 < $var2))
if (($var1 <= $var2))
if (($var1 > $var2))
if (($var1 >= $var2))
33
Unix Shell Programming by Dinesh Kumar S
Strings:
Operator
=
==
!=
Description
equal to
For comparison
Not equal to
<
Less than
>
Greater than
-z
-n
String is null
String not null
Example
if [$var1 = $var2]
if [$var1 == $var2]
if [$var1 != $var2]
if [[$var1 < $var2]] (or)
if [$var1 \< $var2]
if [[$var1 > $var2]] (or)
if [$var1 \> $var2]
if [-z $str]
if [-n $str]
Note: you can use ( ) parenthesis or [ ] Square brackets to enclose variables in conditions.
Example 1: Using operators with integers.
34
Unix Shell Programming by Dinesh Kumar S
Arithmetic Operators:
35
Unix Shell Programming by Dinesh Kumar S
36
Unix Shell Programming by Dinesh Kumar S
37
Unix Shell Programming by Dinesh Kumar S
Comma Operator:
It combines two or more arithmetic operations. But only last arithmetic operation is value is
returned.
38
Unix Shell Programming by Dinesh Kumar S
Chapter 7
Builtin Variables:
These are variables which affect bash script behavior.
Variables
$BASH
$BASH_ENV
$BASH_VERSINFO
$BASH_VERSION
$EDITOR
$EUID
$GROUP
$HOME
$HOSTNAME
$HOSTTYPE
$MACHTYPE
$IGNOREOF
$LINENO
$OLDPWD
$OSTYPE
$PATH
$PPID
$PROMPT_COMMAND
$PWD
$REPLY
$SECONDS
$SHELLOPTS
Description
Displays bash path.
Points to bash environment variables.
Displays bash shell version.
Displays bash version.
Displays editor used by script.
Displays user id.
Displays user group id.
Displays home directory of user.
Displays host name.
Displays host type.
Displays hardware type.
Ignore EOF.
Displays line number in script.
Displays old directory which user worked.
Displays Operating System type.
Displays all path of user.
Displays process ID.
Displays variable holding command to execute.
Displays present working directory.
Default value when value is not given to read variable.
Displays no. of seconds script being executed.
Displays list of shell options.
Note:
I have not discussed all the internal variables but most commonly used.
All the built-in variable command should be in UPPER CASE.
39
Unix Shell Programming by Dinesh Kumar S
40
Unix Shell Programming by Dinesh Kumar S
Positional Parameters:
Positional Parameters are passed from command line to script or to a variable.
In a simple manner when arguments are passed from command line to script it is called
command line arguments. The same variables, when used inside the script are called as
positional parameters.
41
Unix Shell Programming by Dinesh Kumar S
Positional Parameters
$0
$1 to $9
$#
$*
$@
Description
Script Name
Arguments passed to the script
No. of command line arguments
Displays all parameters in single line
Same as $* but considers each parameter as a single word
Output:
42
Unix Shell Programming by Dinesh Kumar S
Chapter 8
Conditional Statements
Conditional Statements:
if condition
if else if condition
Nested if
if elif Condition
I. if condition:
If the condition is satisfied then statements inside body is executed.
Syntax:
43
Unix Shell Programming by Dinesh Kumar S
44
Unix Shell Programming by Dinesh Kumar S
Method I:
Method II:
45
Unix Shell Programming by Dinesh Kumar S
Method III:
Method IV:
46
Unix Shell Programming by Dinesh Kumar S
Looping Statements
Chapter 9
Loop:
A Loop is a block of code which repeatedly executes statements until loop condition is satisfied.
Bash Scripting Supports
for Loop
while Loop
until Loop
Points to note:
Variables in loop conditions should be initialized.
Before executing the loop body test condition should be satisfied.
In body of loop the test variable should be modified.
For Loop:
Syntax:
For Variable_name in [List]
Do
Statements..
Done
(Or)
For Variable_name in [List]; Do
Statements..
Done
Note: If do & for in same line then separate it by using semicolon ;.
47
Unix Shell Programming by Dinesh Kumar S
Note:
Sakthi Vinush Hima Shovan Senthil Lokesh Sudhir Vishnu Srikanth
If you give the list within then it becomes single string.
48
Unix Shell Programming by Dinesh Kumar S
Example 2:
Note: Use can access multiple values in list using Positional parameters such as $1, $2.
In Example_1 $1 Vinush
$2
CSE
49
Unix Shell Programming by Dinesh Kumar S
50
Unix Shell Programming by Dinesh Kumar S
*
[ab]*
Example 1:
51
Unix Shell Programming by Dinesh Kumar S
Example 2:
Example 3:
52
Unix Shell Programming by Dinesh Kumar S
53
Unix Shell Programming by Dinesh Kumar S
54
Unix Shell Programming by Dinesh Kumar S
55
Unix Shell Programming by Dinesh Kumar S
56
Unix Shell Programming by Dinesh Kumar S
II.While loop:
While loop checks the condition first & execute the loop statements till the condition is
satisfied. This loop is compliment to for loop. This loop is mainly used when the loop repetition
is not known.
Syntax:
While [ condition ]
do
Statements.
done
(Or)
While [ condition ]; do
Statements.
done
Note: If do & for in same line then separate it by using semicolon ;.
(Or)
While [[ condition ]]; do
Statements.
done
57
Unix Shell Programming by Dinesh Kumar S
58
Unix Shell Programming by Dinesh Kumar S
59
Unix Shell Programming by Dinesh Kumar S
While statement_1
statement_2
statement_3
statement_4
do
body_statements.
done
(Or)
While statement_1
statement_2
statement_3
statement_4; do
body_statements.
done
60
Unix Shell Programming by Dinesh Kumar S
Statement 1:
Statement 2:
Statement 3:
61
Unix Shell Programming by Dinesh Kumar S
Unix Syntax:
To execute condition & variable increment in same way UNIX uses the syntax below.
While (( Condition ))
do
Statements.
done
Example:
62
Unix Shell Programming by Dinesh Kumar S
Example 1:
Example 2:
63
Unix Shell Programming by Dinesh Kumar S
64
Unix Shell Programming by Dinesh Kumar S
Example 1:
65
Unix Shell Programming by Dinesh Kumar S
Example 2:
66
Unix Shell Programming by Dinesh Kumar S
III.Until loop:
Until loop check condition at first and executes the statements in loop body till the condition is
false.
This is compliment to while loop. Also it checks for Termination Condition at top of the loop.
Syntax:
Until [Checks True_Condition]
do
statements
done
(Or)
67
Unix Shell Programming by Dinesh Kumar S
Explanation:
Iteration 1:
5<0
Iteration 2:
4 < 0 False { 2 * 4 =8 }
Iteration 3:
3 < 0 False { 2 * 3 =6 }
Iteration 4:
2 < 0 False { 2 * 2 =4 }
Iteration 5:
1 < 0 False { 2 * 1 =2 }
Iteration 6:
0 < 0 False { 2 * 0 =0 }
Iteration 7:
False { 2 * 5 =10 }
Note:
The statements in body is executed only when condition is false than is when checking the
condition it should be compliment or opposite one. Then only control flow passes into loop.
68
Unix Shell Programming by Dinesh Kumar S
Nested Loops:
Loop within a loop is called Nested loop.
Syntax:
For Variable_name in [List]
Do
Statements..
For Variable_name in [List]
Do
Statements..
Done
Done
(Or)
While [ condition ]
do
Statements.
While [ condition ]
do
Statements.
done
done
(Or)
Until [Checks True_Condition]
do
statements
Until [Checks True_Condition]
do
statements
done
done
69
Unix Shell Programming by Dinesh Kumar S
Example:
Script to display the below pattern:
Expected output:
70
Unix Shell Programming by Dinesh Kumar S
71
Unix Shell Programming by Dinesh Kumar S
72
Unix Shell Programming by Dinesh Kumar S
73
Unix Shell Programming by Dinesh Kumar S
Control Statements
Chapter 10
# Default Option
(Or)
Case $variable in
Condition_1) Command;;
Condition_2) Command;;
.
Condition_n) Command;;
esac
74
Unix Shell Programming by Dinesh Kumar S
75
Unix Shell Programming by Dinesh Kumar S
76
Unix Shell Programming by Dinesh Kumar S
77
Unix Shell Programming by Dinesh Kumar S
Select:
Select statement is adopted from Korn shell. This is also used to build menus.
Select variable [list]
do
Command
Break
done
78
Unix Shell Programming by Dinesh Kumar S
Chapter 11
Functions
A function is a subroutine, which when executed implements some set of operations or task.
Syntax:
function func_name ()
{
Command
}
(Or)
func_name ()
{
Command
}
79
Unix Shell Programming by Dinesh Kumar S
80
Unix Shell Programming by Dinesh Kumar S
81
Unix Shell Programming by Dinesh Kumar S
Note:
82
Unix Shell Programming by Dinesh Kumar S
83
Unix Shell Programming by Dinesh Kumar S
84
Unix Shell Programming by Dinesh Kumar S
When multiple functions with same name is defined inside a shell script only second function i.e.
lastly declared function will be active.
85
Unix Shell Programming by Dinesh Kumar S
86
Unix Shell Programming by Dinesh Kumar S