Unit III Shell Scripting
Unit III Shell Scripting
. Shell is a user program or it's environment provided for user interaction. Shell is an command
language interpreter that executes commands read from the standard input device (keyboard)
or from a file.
sh
The Bourne shell, called "sh," is one of the original shells, developed for Unix computers by
Stephen Bourne at AT&T's Bell Labs in 1977. Its long history of use , many software
developers are familiar with it. It offers features such as input and output redirection, shell
scripting with string and integer variables, and condition testing and looping.
1. Bash(bourne-Again shell)
Bash (Bourne-Again SHell): Bash is the most common and widely used shell in
Linux. It is the default shell for most Linux distributions and is compatible with the Bourne
shell (sh) and its derivatives.
2. Bourne Shell
The Bourne shell (sh) is one of the original Unix shells and is the ancestor of many of
the shells used in Linux today. It was written by Stephen Bourne at Bell Labs in the
early 1970s and was the default shell in Unix for many years.
The Bourne shell is a command-line interpreter that provides basic functionality for
running commands, executing scripts, and managing files and directories. It supports
variables, loops, conditional statements, and other programming constructs that allow
users to create simple scripts and automate tasks.Although the Bourne shell is no
longer the default shell in most Linux distributions,
3. csh
Developers have written large parts of the Linux operating system in the C and C++
languages. Using C syntax as a model, Bill Joy at Berkeley University developed the "C-
shell," csh, in 1978. It has two advantages over bash shell.It allows aliasing of commands.
Another benefit is C shell has command history features.
ksh
David Korn developed the Korn shell, or ksh, about the time tcsh was introduced. Ksh
improves the functionality by adding floating-point arithmetic, job control, command
aliasing and command completion. AT&T held proprietary rights to ksh until 2000, when it
became open source.
Basic Shell configuration files
, ~/.profile, ~/.bashrc, ~/.bash_logout, ~/.bash_history
/etc/profile
It contains Linux system wide environment and startup programs. It is used by all users
with bash, ksh, sh shell. Usually used to set PATH variable, user limits, and other settings for
user. It only runs for login shell.
/etc/bashrc
Unchanging Variables
In some applications a need may arise for variables to have a constant or fixed value. For
instance, if we want that a’s value should always remain 20 and not changed, we can achieve
this by saying,
$ a=20
$ readonly a
When the variables are made readonly, the shell does not allow as to change their values. All
such variables can be listed by entering readonly at the $ prompt.
Positional Parameters
On many occasions we need to convey information to a program. For this the shell uses
something called ‘positional parameters’. These can be thought of as variables defined by the
shell. They are nine in number, named $1 though $9.
Command-line arguments help make shell scripts interactive for the users. command-line
arguments are an essential part of any practical shell scripting uses.
The bash shell has special variables reserved to point to the arguments which we pass through
a shell script. Bash saves these variables numerically ($1, $2, $3, … $n)
Here, the first command-line argument in our shell script is $1, the second $2 and the third is
$3. This goes on till the 9th argument. The variable $0 stores the name of the script or the
command itself.
Conditional Statements
There are total 5 conditional statements which can be used in bash programming
1. if statement
2. if-else statement
3. if..elif..else..fi statement (Else If ladder)
4. if..then..else..if..then..fi..fi..(Nested if)
5. switch statement
if statement
This block will process if specified condition is true.
Syntax:
if [ expression ]
then
statement
fi
if-else statement
If specified condition is not true in if part then else part will be execute.
Syntax
if [ expression ]
then
statement1
else
statement2
fi
if..elif..else..fi statement (Else If ladder)
To use multiple conditions in one if-else block, then elif keyword is used in shell. If
expression1 is true then it executes statement 1 and 2, and this process continues. If none of
the condition is true then it processes else part.
Syntax
if [ expression1 ]
then
statement1
statement2
.
.
elif [ expression2 ]
then
statement3
statement4
.
.
else
statement5
fi
if..then..else..if..then..fi..fi..(Nested if)
Nested if-else block can be used when, one condition is satisfies then it again checks
another condition. In the syntax, if expression1 is false then it processes else part, and again
expression2 will be check.
Syntax:
if [ expression1 ]
then
statement1
statement2
.
else
if [ expression2 ]
then
statement3
.
fi
fi
Here the string word is compared against every pattern until a match is found. The statement(s)
following the matching pattern executes. If no matches are found, the case statement exits
without performing any action.
There is no maximum number of patterns, but the minimum is one.
When statement(s) part executes, the command ;; indicates that the program flow should jump
to the end of the entire case statement. This is similar to break in the C programming language.
*) represent the default case
[])represent the range of values
? represents any character
Examples
[0-9] represent the range of digits 0 to 9
[aeiou]*)represent the word starts with aeiou
*[0-9])represents the word ends with a digit
???) represent the word is three lettered
Looping statements in shell
Types of loops
1. Until loop
2. While loop
3. For loop
Until loop
The until loop is useful when you need to execute a set of commands until a condition is true.
Syntax
until [ condition ]
do
statement 1
statement 2
done
While Loop
The while loop enables you to execute set of commands repeatedly until some condition
occurs.it usually used when you need to manipulate the value of a variable repeatedly.
Syntax
while [ condition ]
do
statement 1
statement 2
done
For loop
Example
for no in {1..10}
do
echo $no
done
Test command
The test command evaluates the Expression parameter, and if the expression value is True,
returns a zero (True) exit value. Otherwise, the test command returns a nonzero (False) exit
value
There are three types of test command
1. Numerical tests
2. String test
3. File test
Numerical tests are used when comparisons between values of two numbers are to be done
Operator Meaning
-gt Greater than
-eq Equal to
Example
If test $a –eq 0
File test
The test command has several options for checking the status of a file
-s file True if the file exists and has a size greater
than 0
-f file True if the file exits and is not a directory
-d file True if the file exists and is a directory
-c file True if the file exists and is a character
special file
-r file True if the file exists and you have a read
permission to it
-w file True if the file exists and you have a write
permission to it
-x file True if the file exists and you have a execute
permission to it
-k file True if the file exists and its sticky bit is set
Example
If [ -w $filenme ]
String test
Condition Meaning
String1=string2 True if strings are same
String1!=string2 True if the strings are not equal
-z String True if the length of the string is zero
String True if the string is not a null string
Shell functions
Shell functions are similar to subroutines, procedures, and functions in other programming
languages. A function is a collection of statements that execute a specified task. Using
functions to perform repetitive tasks is an excellent way to create code reuse. Functions enable
you to break down the overall functionality of a script into smaller, logical subsections, which
can then be called upon to perform their individual tasks when needed.
Creating Functions
To declare a function, simply use the following syntax −
function_name () {
list of commands
}