UNIT - 3
Shell Scripting
What is Shell?
Shell is a user program that provides a Command Line Interpreter (C.L.I.) that carries the user-
typed, human-readable input commands from the terminal and converts them into instructions the
kernel can understand. The shell examines the commands and communicates the actions for each
user logged in to the kernel.
What is a Unix shell?
The shell used in Unix-based operating systems such as Linux, macOS, and others is called Unix
Shell. However, the term “shell” can describe any command line interface type, and “Unix shell”
refers exclusively to the shells used in Unix-based operating systems.
These Unix shells include several implementations with syntax and features such as command
execution, file manipulation, process management, and input/output redirection.
There are several Unix shells available with unique features for different user needs, with the most
common ones being:
• Bourne Shell (sh): It is the first Unix shell that was introduced in 1970. It offers basic
functionality and scripting capabilities like list files, read inputs, variables, and control
flow statements.
• Bash (Bourne Again Shell): It is the advanced version of Bourne Shell that offers
additional features like command line editing and improved scripting capabilities.
• C Shell (csh): It is known for its C-like syntax and interactive features such as history
substitution.
• Korn Shell (ksh): It was developed by David Korn as an extension of the Bourne Shell,
which provides advanced scripting capabilities and is backward compatible with Bourne
Shell(sh).
• Z Shell (zsh): It is highly customizable with advanced features like tab completion, spelling
correction, and extensive configuration options.
• Fish (Friendly Interactive Shell): fish (stylized in lowercase) is a Unix shell focusing more
on interactivity and usability with features like syntax highlighting, auto suggestions, and
an intuitive scripting language.
What is Shell Scripting?
A shell script is a set of instructions crafted to be executed by an operating system, which serves
as a command line interpreter for automating tasks and executing commands.
shells are interactive, which means they accept commands as input from users and execute them.
However, sometimes we want to execute a bunch of commands routinely, so we have to type in all
commands each time in the terminal.
As a shell can also take commands as input from file, we can write these commands in a file and
can execute them in shell to avoid this repetitive work. These files are called Shell Scripts or Shell
Programs. Shell scripts are similar to the batch file in MS-DOS. Each shell script is saved
with `.sh` file extension e.g., [Link].
A shell script has syntax just like any other programming language. If you have any prior
experience with any programming language like Python, C/C++ etc. It would be very easy to get
started with it.
A shell script comprises the following elements –
• Shell Keywords – if, else, break etc.
• Shell commands – cd, ls, echo, pwd, touch etc.
• Functions
• Control flow – if..then..else, case and shell loops etc.
Common tasks carried out by shell scripts involve handling files, running programs, and
generating text output. Essentially, they provide a streamlined way to automate diverse
operations within the operating system.
Unix shell scripting refers to shell scripting done specifically within Unix-based operating
systems. It is a subset of shell scripting explicitly done in a Unix environment.
The basic steps involved with shell scripting are
• writing the script
• making the script accessible to the shell
• giving the shell execute permission
Shell Scripting Terminologies
Before we delve further into shell scripting, knowing a few terminologies related to the shell is
essential as it will help us understand it better.
• Terminal: An interface to access the shell. It lets users submit commands and see the
results.
• Shell Shebang Line: The first line of a shell script (usually starting with #!) specifies the
interpreter for executing the script. For example, #!/bin/bash indicates the Bash interpreter
should run the script.
• Shell Comments: Comments are lines that the shell interpreter ignores. They are used to
explain the script’s purpose or specific sections. Lines starting with (#) are comments.
• Shell Variables: Variables are named storage locations with values used within the script.
We can assign values to variables using the = operator and then reference them later in
the script using the variable name preceded by a dollar sign (e.g., $name).
• Shell Sourcing a File: Sourcing a file in a shell means executing the commands in the file
in the current shell environment. When you source a file, the commands are executed the
same way they had been typed directly into the shell. A file is sourced by writing
source <fileName> or ./ <fileName> in the command line. For example, if we want to
execute the commands written in [Link], We can source it into the terminal
with ./[Link].
• Commands: Shell scripts are essentially lists of commands, the building blocks. These are
built-in keywords (like cd to change directory, echo to print text) or commands for external
programs.
• Arguments: Arguments are additional information passed to commands within a script.
They can be used to provide specific details for the command’s operation. They start
with – or — (like git —version tells the version of git installed).
• Redirection: This allows us to control the input and output of commands within the script.
Operators like > (redirect output), >> (append to output), < (redirect input) are used for this
purpose.
• Control Flow Statements: These statements dictate the script’s execution order. Common
control flow statements include:
o if/else: Used for conditional execution based on a specific factual or false
condition.
o for/while loops: Used to repeat a block of commands a particular number of times
(for loop) or until a condition is met (while loop).
Why do we need shell scripts?
There are many reasons to write shell scripts:
• To avoid repetitive work and automation
• System admins use shell scripting for routine backups.
• System monitoring
• Adding new functionality to the shell etc.
Some Advantages of shell scripts
• The command and syntax are exactly the same as those directly entered in the command line, so
programmers do not need to switch to entirely different syntax
• Writing shell scripts are much quicker
• Quick start
• Interactive debugging etc.
Some Disadvantages of shell scripts
• Prone to costly errors, a single mistake can change the command which might be harmful.
• Slow execution speed
• Design flaws within the language syntax or implementation
• Not well suited for large and complex task
• Provide minimal data structure unlike other scripting languages. etc
UNIX Editors
Any UNIX system provides many different editors out of the box. In
ed
ed is the original UNIX text editor, and it’s the most basic you can work with. It’s also very rarely used,
if ever used, by most people.
Run it by typing ed. This starts an interactive session. Enter in write mode by typing a on a single line,
and press enter. Then type everything you want, and once you are done, write just a dot (.) on a line and
press enter.
Now type w followed by a file name to save the buffer to a file. It will return the number of bytes written to
the file.
You can then press q to quit.
You can edit a file with ed by invoking it with the file name: ed <filename>. When you press a to add, you
add content to the bottom of the file.
Inside an ed session you can type ,p to print the current file content.
vi / vim
vim is a very popular file editor, especially among programmers. It’s actively developed and frequently
updated, and there’s a very big community around it. There’s even a Vim conference!
vi in modern systems is just an alias to vim, which means vi improved.
You start it by running vi on the command line.
You can specify a filename at invocation time to edit that specific file:
vi [Link]
You have to know that Vim has 2 main modes:
• command (or normal) mode
• insert mode
When you start the editor, you are in command mode. You can’t enter text like you expect from a GUI-
based editor. You have to enter insert mode. You can do this by pressing the i key. Once you do so,
the -- INSERT -- word appear at the bottom of the editor:
Now you can start typing and filling the screen with the file contents:
You can move around the file with the arrow keys, or using the h - j - k - l keys. h-l for left-right, j-k for
down-up.
Once you are done editing you can press the esc key to exit insert mode, and go back to command
mode.
At this point you can navigate the file, but you can’t add content to it (and be careful which keys you press
as they might be commands).
One thing you might want to do now is saving the file. You can do so by pressing : (colon), then w.
You can save and quit pressing : then w and q: :wq
You can quit without saving, pressing : then q and !: :q!
You can undo and edit by going to command mode and pressing u. You can redo (cancel an undo) by
pressing ctrl-r.
Those are the basics of working with Vim. From here starts a rabbit hole we can’t go into in this little
introduction.
I will only mention those commands that will get you started editing with Vim:
• pressing the x key deletes the character currently highlighted
• pressing A goes at the end of the currently selected line
• press 0 to go to the start of the line
• go to the first character of a word and press d followed by w to delete that word. If you follow it
with e instead of w, the white space before the next word is preserved
• use a number between d and w to delete more than 1 word, for example use d3w to delete 3
words forward
• press d followed by d to delete a whole entire line. Press d followed by $ to delete the entire line
from where the cursor is, until the end
emacs
emacs is an awesome editor and it’s historically regarded as the editor for UNIX systems.
Famously vi vs emacs flame wars and heated discussions caused many unproductive hours for
developers around the world.
emacs is very powerful.
You can open a new emacs session simply by invoking emacs:
macOS users, stop a second now. If you are on Linux there are no problems, but macOS does not ship
applications using GPLv3, and every built-in UNIX command that has been updated to GPLv3 has not
been updated. While there is a little problem with the commands I listed up to now, in this case using an
emacs version from 2007 is not exactly the same as using a version with 12 years of improvements and
change. This is not a problem with Vim, which is up to date. To fix this, run brew install emacs and
running emacs will use the new version from Homebrew (make sure you have Homebrew installed)
You can also edit an existing file calling emacs <filename>:
You can start editing and once you are done, press ctrl-x followed by ctrl-w. You confirm the folder:
and Emacs tell you the file exists, asking you if it should overwrite it:
Answer y, and you get a confirmation of success:
You can exit Emacs pressing ctrl-x followed by ctrl-c. Or ctrl-x followed by c (keep ctrl pressed).
There is a lot to know about Emacs. More than I am able to write in this little introduction. I encourage you
to open Emacs and press ctrl-h r to open the built-in manual and ctrl-h t to open the official tutorial.
nano
nano is a more beginner friendly editor.
Run it using nano <filename>.
You can directly type characters into the file without worrying about modes.
You can quit without editing using ctrl-X. If you edited the file buffer, the editor will ask you for
confirmation and you can save the edits, or discard them. The help at the bottom shows you the keyboard
commands that let you work with the file:
pico is more or less the same, although nano is the GNU version of pico which at some point in history
was not open source and the nano clone was made to satisfy the GNU operating system license
requirements.
Steps to write and execute a script
o Open the terminal. Go to the directory where you want to create your script.
o Create a file with .sh extension.
o Write the script in the file using an editor.
o Make the script executable with command chmod +x <fileName>.
o Run the script using ./<fileName>.
Note: In the last step you have to mention the path of the script if your script is in other directory.
Hello World script
Here we'll write a simple programme for Hello World.
First of all, create a simple script in any editor or with echo. Then we'll make it executable with chmod
+x command. To find the script you have to type the script path for the shell.
Look at the above snapshot, script echo Hello World is created with echo command
as hello_world. Now command chmod +x hello_world is passed to make it executable. We have given
the command ./hello_world to mention the hello_world path. And output is displayed.
Shell Scripting Comments
Any line starting with a hash (#) becomes comment. Comment means, that line will not take part in script
execution. It will not show up in the output.
Shell Variables
Last Updated : 16 May, 2024
A shell variable is a character string in a shell that stores some value. It could be an integer, filename,
string, or some shell command itself. Basically, it is a pointer to the actual data stored in memory. We
have a few rules that have to be followed while writing variables in the script (which will be discussed in
the article). Overall knowing the shell variable scripting leads us to write strong and good shell scripts.
Rules for variable definition
A variable name could contain any alphabet (a-z, A-Z), any digits (0-9), and an underscore ( _ ).
However, a variable name must start with an alphabet or underscore. It can never start with a number.
Following are some examples of valid and invalid variable names:
• Valid Variable Names
ABC
_AV_3
AV232
• Invalid variable names
2_AN
!ABD
$ABC
&QAID
Note: It must be noted that no other special character except underscore can be used in a variable name
because all other special characters have special meanings in Shell Scripting.
Defining Variables
Syntax
variable_name = <variable data>
Example
num="1"
name="Devil"
These kinds of variables are scalar variables as they could hold one value at a time.
1) Accessing variable
Variable data could be accessed by appending the variable name with ‘$’ as follows:
#!/bin/bash
VAR_1="Devil"
VAR_2="OWL"
echo "$VAR_1$VAR_2"
Output:
DevilOWL
Unsetting Variables
The unset command directs a shell to delete a variable and its stored data from list of variables. It can be
used as follows:
#!/bin/bash
var1="Devil"
var2=23
echo $var1 $var2
unset var1
echo $var1 $var2
Output:
DEVIL 23
23
Note: The unset command could not be used to unset read-only variables.
3) Read only Variables.
These variables are read only i.e., their values could not be modified later in the script. Following is an
example:
#!/bin/bash
var1="Devil"
var2=23
readonly var1
echo $var1 $var2
var1=23
echo $var1 $var2
Output:
Devil 23
./bash1: line 8: var1: readonly variable
Devil 23
Variable Types
We can discuss three main types of variables:
1) Local Variable:
Variables which are specific to the current instance of shell. They are basically used within the shell, but
not available for the program or other shells that are started from within the current shell.
For example:
`name=Jayesh`
In this case the local variable is (name) with the value of Jayesh. Local variables is temporary storage of
data within a shell script.
2) Environment Variable:
These variables are commonly used to configure the behavior script and programs that are run by shell.
Environment variables are only created once, after which they can be used by any user.
For example:
`export PATH=/usr/local/bin:$PATH` would add `/usr/local/bin` to the beginning of the shell’s search path
for executable programs.
3) Shell Variables:
Variables that are set by shell itself and help shell to work with functions correctly. It contains both, which
means it has both, some variables are Environment variable, and some are Local Variables.
For example:
`$PWD` = Stores working directory
`$HOME` = Stores user’s home directory
`$SHELL` = Stores the path to the shell program that is being used.
we are using two types of variables:
• System Defined Variables and
• User Defined Variables
A variable in a shell script is a means of referencing a numeric or character value. A shell script doesn’t
require you to declare a type for your variables.
System Defined Variables:
These are the variables which are created and maintained by Linux (operating system) itself. Generally,
these variables are defined in CAPITAL LETTERS.
To view the system defined variables, use the command “$set”:
To print the value of above variables, use echo command: #echo $HOME #echo $USERNAME
User Defined Variables:
These variables are defined by users. A shell script allows us to set and use our own variables within the
script. Setting variables allows you to temporarily store data and use it throughout the script, making the
shell script more like a real computer program.
User variables can be any text string of up to 20 letters, digits or an underscore character.
User variables are case sensitive, so the variable Var1 is different from the variable var1.
Values are assigned to user variables using an equal sign.
No spaces can appear between the variable, the equal sign, and the value. e.g. var1 = 10 var2 = -57 var3
= testing
The shell script automatically determines the data type used for the variable value. Variables defined
within the shell script maintain their values throughout the life of the shell script but are deleted when the
shell script completes.
Just like system variables, user variables can be referenced using the dollar sign.
System Calls:
A system call is the programmatic way in which a computer program requests a service from the kernel of
the operating system it is executed on. A system call is a way for programs to interact with the operating
system. A computer program makes a system call when it makes a request to the operating system’s
kernel. System call provides the services of the operating system to the user programs via Application
Program Interface (API). It provides an interface between a process and operating system to allow user-
level processes to request services of the operating [Link] calls are the only entry points into the
kernel system. All programs needing resources must use system calls.
Services Provided by System Calls:
• Process creation and management
• Main memory management
• File Access, Directory and File system management
• Device handling (I/O)
• Protection
• Networking, etc.
Types of System Calls:
There are five different categories of system calls:
• Process Control: end, abort, create, terminate, allocate and free memory.
• File Management: create, open, close, delete, read file etc.
• Device management
• Information maintenance
• Communication
Unix System Calls
System calls in Unix are used for file system control, process control, interprocess communication etc.
Access to the Unix kernel is only available through these system calls. Generally, system calls are similar
to function calls, the only difference is that they remove the control from the user process.
There are around 80 system calls in the Unix interface currently. Details about some of the important
ones are given as follows -
System Call Description
access() This checks if a calling process has access to the required file
chdir() The chdir command changes the current directory of the system
chmod() The mode of a file can be changed using this command
chown() This changes the ownership of a particular file
kill() This system call sends kill signal to one or more processes
link() A new file name is linked to an existing file using link system call.
open() This opens a file for the reading or writing process
pause() The pause call suspends a file until a particular signal occurs.
System Call Description
stime() This system call sets the correct time.
times() Gets the parent and child process times
alarm() The alarm system call sets the alarm clock of a process
fork() A new process is created using this command
chroot() This changes the root directory of a file.
exit() The exit system call is used to exit a process.
Pipes in UNIX
The novel idea of Pipes was introduced by M.D Mcllroy in June 1972– version 2, 10 UNIX installations.
Piping is used to give the output of one command (written on LHS) as input to another command (written
on RHS). Commands are piped together using vertical bar “ | ” symbol. Syntax:
command 1|command 2
Example:
• Input: ls|more
• Output: more command takes input from ls command and appends it to the standard output. It
displays as many files that fit on the screen and highlighted more at the bottom of the screen. To
see the next screen hit enter or space bar to move one line at a time or one screen at a time
respectively.
Filters in UNIX
In UNIX/Linux, filters are the set of commands that take input from standard input stream i.e. stdin,
perform some operations and write output to standard output stream i.e. stdout. The stdin and stdout can
be managed as per preferences using redirection and pipes. Common filter commands
are: grep, more, sort.
1. grep Command:It is a pattern or expression matching command. It searches for a pattern or regular
expression that matches in files or directories and then prints found matches.
Syntax:
$grep[options] "pattern to be matched" filename
Example:
Input : $grep 'hello' ist_file.txt
Output : searches hello in the ist_file.txt and outputs/returns the lines containing 'hello'.
The Options in grep command are:
Grep command can also be used with meta-characters:
Example:
Input : $grep 'hello' *
Output : it searches for hello in all the files and directories.
* is a meta-character and returns matching 0 or more preceding characters
2. sort Command: It is a data manipulation command that sorts or merges lines in a file by specified
fields. In other words it sorts lines of text alphabetically or numerically, default sorting is alphabetical.
Syntax:
$sort[options] filename
The options include:
Example:
$sort [Link]
$sort -n [Link]
3. more Command: It is used to customize the displaying contents of file. It displays the text file contents
on the terminal with paging controls. Following key controls are used:
• To display next line, press the enter key
• To bring up next screen, press spacebar
• To move to the next file, press n
• To quit, press q.
Syntax:
$more[options] filename
Example:
cat [Link] | more
UNIT – 4
Control Structures
You can control the execution of Linux commands in a shell script with control structures. Control
structures allow you to repeat commands and to select certain commands over others. A control structure
consists of two major components: a test and commands. If the test is successful, then the commands
are executed. In this way, you can use control structures to make decisions as to whether commands
should be executed.
There are two different kinds of control structures: loops and conditions. A loop repeats commands,
whereas a condition executes a command when certain conditions are met. The BASH shell has three
loop control structures: while, for, and for-in. There are two condition structures: if and case. The control
structures have as their test the execution of a Linux command. All Linux commands return an exit status
after they have finished executing. If a command is successful, its exit status will be 0. If the command
fails for any reason, its exit status will be a positive value referencing the type of failure that occurred. The
control structures check to see if the exit status of a Linux command is 0 or some other value. In the case
of the if and while structures, if the exit status is a zero value, then the command was successful and the
structure continues.
Test Operations
With the test command, you can compare integers, compare strings, and even perform logical
operations. The command consists of the keyword test followed by the values being compared,
separated by an option that specifies what kind of comparison is taking place. The option can be thought
of as the operator, but it is written, like other options, with a minus sign and letter codes. For example, -
eq is the option that represents the equality comparison. However, there are two string operations that
actually use an operator instead of an option. When you compare two strings for equality you use the
equal sign, =. For inequality you use !=. Table 8-6 lists some of the commonly used options and operators
used by test. The syntax for the test command is shown here:
test value -option value
test string = string
Table 8-6: BASH Shell Test Operators
Integer Comparisons Function
-gt Greater-than
Table 8-6: BASH Shell Test Operators
Integer Comparisons Function
-lt Less-than
-ge Greater-than-or-equal-to
-le Less-than-or-equal-to
-eq Equal
-ne Not-equal
String Comparisons
-z Tests for empty string
= Equal strings
!= Not-equal strings
Logical Operations
-a Logical AND
-o Logical OR
! Logical NOT
File Tests
-f File exists and is a regular file
-s File is not empty
-r File is readable
-w File can be written to, modified
-x File is executable
-d Filename is a directory name
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
Their description with syntax is as follows:
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
switch statement
case statement works as a switch statement if specified value match with the pattern then it will
execute a block of that particular pattern
When a match is found all of the associated statements until the double semicolon (;;) is
executed.
A case will be terminated when the last command is executed.
If there is no match, the exit status of the case is zero.
Syntax:
case in
Pattern 1) Statement 1;;
Pattern n) Statement n;;
esac
Example Programs
Example 1:
Implementing if statement
#Initializing two variables
a=10
b=20
#Check whether they are equal
if [ $a == $b ]
then
echo "a is equal to b"
fi
#Check whether they are not equal
if [ $a != $b ]
then
echo "a is not equal to b"
fi
Output
$bash -f [Link]
a is not equal to b
Example 2:
Implementing [Link] statement
#Initializing two variables
a=20
b=20
if [ $a == $b ]
then
#If they are equal then print this
echo "a is equal to b"
else
#else print this
echo "a is not equal to b"
fi
Output
$bash -f [Link]
a is equal to b
Example 3:
Implementing switch statement
CARS="bmw"
#Pass the variable in string
case "$CARS" in
#case 1
"mercedes") echo "Headquarters - Affalterbach, Germany" ;;
#case 2
"audi") echo "Headquarters - Ingolstadt, Germany" ;;
#case 3
"bmw") echo "Headquarters - Chennai, Tamil Nadu, India" ;;
esac
Output
$bash -f [Link]
Headquarters - Chennai, Tamil Nadu, India.
Looping Statements in Shell Scripting:
There are total 3 looping statements that can be used in bash programming
To alter the flow of loop statements, two commands are used they are,
1. break
2. continue
Their descriptions and syntax are as follows:
`while` statement in Shell Script in Linux
Here the command is evaluated and based on the resulting loop will execute, if the command is
raised to false then the loop will be terminated that.
#/bin/bash
while <condition>
do
<command 1>
<command 2>
<etc>
done
Implementation of `While` Loop in Shell Script.
First, we create a file using a text editor in Linux. In this case, we are using `vim`editor.
vim [Link]
You can replace “[Link]” with the desired name.
Then we make our script executable using the `chmod` command in Linux.
chmod +x [Link]
#/bin/bash
a=0
# lt is less than operator
#Iterate the loop until a less than 10
while [ $a -lt 10 ]
do
# Print the values
echo $a
# increment the value
a=`expr $a + 1`
done
Output:
While Loop in Linux
Explanation:
• #/bin/bash: Specifies that the script should be interpreted using the Bash shell.
• a=0: Initializes a variable a with the value 0.
• while [ $a -lt 10 ]: Initiates a while loop that continues as long as the value a is less than
10.
• do: Marks the beginning of the loop’s body.
• echo $a: Prints the current value of a the console.
• a=expr $a + 1“: Increments the value of a by 1. The expr command is used for arithmetic
expressions.
• done: Marks the end of the loop.
`for` statement in Shell Script in Linux
The for loop operates on lists of items. It repeats a set of commands for every item in a list.
Here var is the name of a variable and word 1 to word N are sequences of characters separated by
spaces (words).
Each time the for loop executes, the value of the variable var is set to the next word in the list of
words, word 1 to word N.
Syntax:
#/bin/bash
for <var> in <value1 value2 ... valuen>
do
<command 1>
<command 2>
<etc>
done
Implementation of `for` Loop with `break` statement in Shell Script.
First, we create a file using a text editor in Linux. In this case, we are using `vim`editor.
vim [Link]
You can replace “[Link]” with the desired name.
Then we make our script executable using the `chmod` command in Linux.
chmod +x [Link]
#/bin/bash
#Start of for loop
for a in 1 2 3 4 5 6 7 8 9 10
do
# if a is equal to 5 break the loop
if [ $a == 5 ]
then
break
fi
# Print the value
echo “Iteration no $a”
done
Output:
Break statement in for Loop in linux
Explanation:
• #/bin/bash: Specifies that the script should be interpreted using the Bash shell.
• for a in 1 2 3 4 5 6 7 8 9 10: Initiates a for loop that iterates over the values 1 through 10,
assigning each value to the variable a in each iteration.
• do: Marks the beginning of the loop’s body.
• if [ $a == 5 ]: Checks if the current value a is equal to 5.
o then: Marks the beginning of the conditional block to be executed if the condition
is true.
o break: Exits the loop if the condition is true.
o fi: Marks the end of the conditional block.
• echo "Iteration no $a": Prints a message indicating the current iteration number.
• done: Marks the end of the loop.
The script sets up a for loop that iterates over the values 1 through 10. During each iteration, it
checks if the value a is equal to 5. If it is, the loop is exited using the break statement. Otherwise,
it prints a message indicating the current iteration number. The loop continues until it completes
all iterations or until it encounters a break statement.
Implementation of `for` Loop with `continue` statement in Shell Script.
First, we create a file using a text editor in Linux. In this case, we are using `vim`editor.
vim for_continue.sh
You can replace “for_continue.sh” with the desired name.
Then we make our script executable using the `chmod` command in Linux.
chmod +x for_continue.sh
#/bin/bash
for a in 1 2 3 4 5 6 7 8 9 10
do
# if a = 5 then continue the loop and
# don’t move to line 8
if [ $a == 5 ]
then
continue
fi
echo “Iteration no $a”
done
Output:
continue statement in for loop in Linux
Explanation:
• #/bin/bash: Specifies that the script should be interpreted using the Bash shell.
• for a in 1 2 3 4 5 6 7 8 9 10: Initiates a for loop that iterates over the values 1 through 10,
assigning each value to the variable a in each iteration.
• do: Marks the beginning of the loop’s body.
• if [ $a == 5 ]: Checks if the current value a is equal to 5.
o then: Marks the beginning of the conditional block to be executed if the condition
is true.
o continue: Skips the rest of the loop’s body and goes to the next iteration if
the condition is true.
o fi: Marks the end of the conditional block.
• echo "Iteration no $a": Prints a message indicating the current iteration number. This line
is skipped if a is equal to 5 due to the continue statement.
• done: Marks the end of the loop.
The script sets up a for loop that iterates over the values 1 through 10. During each iteration, it
checks if the value a is equal to 5. If it is, the loop continues to the next iteration without executing
the remaining statements in the loop’s body. Otherwise, it prints a message indicating the current
iteration number. The loop continues until it completes all iterations.
`until` statement in Shell Script in Linux
The until loop is executed as many times as the condition/command evaluates to false. The loop
terminates when the condition/command becomes true.
Syntax:
#/bin/bash
until <condition>
do
<command 1>
<command 2>
<etc>
done
Implementing `until` Loop in Shell Scrip
First, we create a file using a text editor in Linux. In this case, we are using `vim`editor.
vim [Link]
You can replace “until. sh” with the desired name.
Then we make our script executable using the `chmod` command in Linux.
chmod +x [Link]
#/bin/bash
a=0
# -gt is greater than operator
#Iterate the loop until a is greater than 10
until [ $a -gt 10 ]
do
# Print the values
echo $a
# increment the value
a=`expr $a + 1`
done
Output:
Until loop in Linux
Explanation:
• #/bin/bash: Specifies that the script should be interpreted using the Bash shell.
• a=0: Initializes a variable a with the value 0.
• until [ $a -gt 10 ]: Initiates a until loop that continues as long as the value a is not greater
than 10.
• do: Marks the beginning of the loop’s body.
• echo $a: Prints the current value of a the console.
• a=expr $a + 1“: Increments the value of a by 1. The expr command is used for arithmetic
expressions.
• done: Marks the end of the loop.
Note: Shell scripting is a case-sensitive language, which means proper syntax has to be followed
while writing the scripts.
Examples of Looping Statements
1. Iterating Over Colors Using a For Loop
First, we create a file using a text editor in Linux. In this case, we are using `vim`editor.
vim [Link]
You can replace “[Link]” with the desired name.
Then we make our script executable using `chmod` command in Linux.
chmod +x [Link]
#/bin/bash
COLORS=”red green blue”
# the for loop continues until it reads all the values from the COLORS
for COLOR in $COLORS
do
echo “COLOR: $COLOR”
done
Output:
For until in Linux
Explanation:
• Initialization of Colors:
o COLORS="red green blue": Initializes a variable named COLORS with a space-
separated list of color values (“red”, “green”, and “blue”).
• For Loop Iteration:
o for COLOR in $COLORS: Initiates a for loop that iterates over each value in
the COLORS variable.
o Loop Body:
o echo "COLOR: $COLOR": Prints a message for each color,
displaying the current color value.
o The loop continues until it processes all the values present in
the COLORS variable.
This example demonstrates a simple for loop in Bash, iterating over a list of colors stored in
the COLORS variable. The loop prints a message for each color, indicating the current color being
processed. The loop iterates until all color values are exhausted.
2. Creating an Infinite Loop with “while true” in Shell Script
First we create a file using a text editor in Linux. In this case we are using `vim`editor.
vim [Link]
You can replace “[Link]” with desired name.
Then we make our script executable using `chmod` command in Linux.
chmod +x [Link]
#/bin/bash
while true
do
# Command to be executed
# sleep 1 indicates it sleeps for 1 sec
echo “Hi, I am infinity loop”
sleep 1
done
Output:
infinite loop in linux
Explanation:
Infinite Loop Structure:
• while true: Initiates a while loop that continues indefinitely as the condition true is always
true.
o Loop Body:
o echo "Hi, I am infinity loop": Prints a message indicating that the script is
in an infinite loop.
o sleep 1: Pauses the execution of the loop for 1 second before the next
iteration.
• The loop continues indefinitely, executing the commands within its body repeatedly.
This example showcases the creation of an infinite loop using the while true construct in Bash.
The loop continuously prints a message indicating its status as an infinite loop and includes a
sleep 1 command, causing a one-second delay between iterations. Infinite loops are often used
for processes that need to run continuously until manually interrupted.
3. Interactive Name Confirmation Loop
First we create a file using a text editor in Linux. In this case we are using `vim`editor.
vim [Link]
You can replace “[Link]” with desired name.
Then we make our script executable using `chmod` command in Linux.
chmod +x [Link]
#/bin/bash
CORRECT=n
while [ “$CORRECT” == “n” ]
do
# loop discontinues when you enter y i.e., when your name is correct
# -p stands for prompt asking for the input
read -p “Enter your name:” NAME
read -p “Is ${NAME} correct? ” CORRECT
done
Output:
Interactive script in Linux
Explanation:
• Initialization:
o CORRECT=n: Initializes a variable named CORRECT with the value “n”. This
variable is used to control the loop.
• Interactive Loop:
o while [ "$CORRECT" == "n" ]: Initiates a while loop that continues as long as the
value of CORRECT is equal to “n”.
o Loop Body:
o read -p "Enter your name:" NAME: Prompts the user to enter their
name and stores the input in the NAME variable.
o read -p "Is ${NAME} correct? " CORRECT: Asks the user to confirm
if the entered name is correct and updates the CORRECT variable
accordingly.
o The loop continues until the user enters “y” (indicating the correct name).
This example demonstrates an interactive loop that prompts the user to enter their name and then
asks for confirmation. The loop continues until the user confirms that the entered name is correct
by inputting “y”. The loop utilizes the `read` command for user input and updates the
`CORRECT` variable to control the loop flow.
Functions and it’s types
Last Updated : 12 Sep, 2024
Shell scripting is a powerful tool used to automate tasks in Unix-like operating systems. A shell
serves as a command-line interpreter, and shell scripts often perform file manipulation, program
execution, and text output. Here, we’ll look into functions in shell scripting, exploring their
structure, usage, and types to help you effectively incorporate them into your scripts.
What is a Function in Shell Scripting?
A function is a collection of statements that execute a specified task. Its main goal is to break
down a complicated procedure into simpler subroutines that can subsequently be used to
accomplish the more complex routine. For the following reasons, functions are popular:
• Code Reusability: Functions allow you to write code once and use it multiple times
throughout your script.
• Enhanced Readability: Functions help organize code into logical blocks, making scripts
easier to read and understand.
• Modularity: Functions enable modular programming, where different tasks are handled by
separate, self-contained functions.
• Ease of Maintenance: Functions simplify debugging and updating code since changes can
be made in one place rather than throughout the script.
Basic Structure of a Function in Shell Scripting
The basic syntax for defining a function in shell scripting is:
function_name(){
// body of the function
• The function_name can be any valid string and the body can be any sequence of valid
statements in the scripting language.
• The body of the function can include any sequence of valid shell commands or
statements.
Example
Let us try to understand the concept of functions by looking at an example. The following is a
code to print all the prime numbers between a range [le,ri].
It consists of a function ‘is_prime()‘ which is used to check if the given number is a prime or not.
In this function, we use the variable ‘$1’ to access the first argument, which is the number itself. In
scripting languages, we can access arguments by ‘$i’, where ‘i’ is a number that signifies the
position of the argument.
echo -n "Enter Left-End: "
read le
echo -n "Enter Right-End: "
read ri
is_prime(){
if [ $1 -lt 2 ]; then
return
fi
ctr=0
for((i=2;i<$1;i++)){
if [ $(( $1 % i )) -eq 0 ]; then
ctr=$(( ctr +1 ))
fi
if [ $ctr -eq 0 ]; then
printf "%d " "$1"
fi
printf "Prime Numbers between %d and %d are: " "$le" "$ri"
for((i=le;i<=ri;i++)){
is_prime $i
printf "\n"
Types of Functions
The functions in shell scripting can be boxed into a number of categories. The following are some
of them:
1. The functions that return a value to the caller.
The return keyword is used by the functions for this purpose. The following is one such function
used to calculate the average of the given numbers.
find_avg(){
len=$#
sum=0
for x in "$@"
do
sum=$((sum + x))
done
avg=$((sum/len))
return $avg
find_avg 30 40 50 60
printf "%f" "$?"
printf "\n"
Output:
Explanation:
• The function ‘find_avg()’ calculates the average of the given numbers and returns it using
the ‘return’ statement.
• Since the ‘return’ value is restricted to integers between 0 and 255, it prints the result
using ‘$?’, which holds the exit status of the last executed command.
2. The functions that terminate the shell using the ‘exit’ keyword.
These functions use the ‘exit’ command to stop the shell entirely. This can be useful for error
handling or when a certain condition requires the script to stop immediately.
is_odd(){
x=$1
if [ $((x%2)) == 0 ]; then
echo "Invalid Input"
exit 1
else
echo "Number is Odd"
fi
is_odd 64
Output:
Explanation: The function ‘is_odd()’ checks if a number is odd. If it’s even, it outputs an error
message and exits the script using ‘exit 1’.
3. The functions that alter the value of a variable or variables.
These functions can modify the values of variables directly within their scope or globally if the
variables are defined outside the function.
a=1
increment(){
a=$((a+1))
return
increment
echo "$a"
Output:
Explanation: The function ‘increment()’ increases the value of the variable ‘a’ by 1, demonstrating
how functions can directly alter variable values.
4. The functions that echo output to the standard output.
These functions output information directly to the standard output using the ‘echo’ command.
hello_world(){
echo "Hello World"
return
hello_world
Output:
Explanation: The function ‘hello_world()’ simply prints “Hello World” to the standard output,
illustrating a basic use of echoing in functions.
The Unix Shell: Regular Expressions
Regular Expressions
A regular expression (regex) is a text pattern that can be used for searching and replacing.
Regular expressions are similar to Unix wild cards used in globbing, but much more powerful, and
can be used to search, replace and validate text.
Regular expressions are used in many Unix commands such as find and grep, and also within
most programming languages such as R and Python.
We only show basic usage here to get you started. To get practice, first spend some time
at [Link] to get a better understanding of how to use regular expressions, then find
out how to use them in your text editor to do a search and replace.
Matching characters
We will practice using grep. If a successful match is found, the line of text will be returned;
otherwise nothing.
In [1]:
grep --help | head -n 20
usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
[-e pattern] [-f file] [--binary-files=value] [--color=when]
[--context[=num]] [--directories=action] [--label] [--line-buffered]
[--null] [pattern] [file ...]
Literal character match
In [2]:
echo abcd | grep abcd
abcd
In [3]:
echo abcd | grep bc
abcd
No match for ac
In [4]:
echo abcd | grep ac
Case insensitive match
In [5]:
echo abcd | grep -i A
abcd
In [6]:
echo abcd | grep A
Matching any single character
The . matches exactly one character.
In [7]:
echo abcd | grep a.c
abcd
In [8]:
echo abcd | grep a..c
In [9]:
echo abcd | grep a..d
abcd
Matching a character set
In [10]:
echo a2b | grep [0123456789]
a2b
In [11]:
echo a2b | grep [0-9]
a2b
In [12]:
echo a2b | grep [abc]
a2b
In [13]:
echo a2b | grep [def]
In [14]:
echo a2b | grep [a-z]
a2b
In [15]:
echo a2b | grep [A-Z]
Exceptions
The ^ within a character set says match anything NOT in the set.
In [16]:
echo a2b | grep [A-Z]
In [17]:
echo a2b | grep [^A-Z]
a2b
Pre-defined character sets
Many useful sets of characters (e.g. all digits) have been pre-defined as character classes that you
can use in your regular expressions. Character classes are a bit clumsy in the Unix shell, but
simpler forms are often used in programming languages (e.g. ‘:raw-latex:`\d`’ instead of ‘[:digit:]’).
In [18]:
echo a2b | grep ['[:alpha:]']
a2b
In [19]:
echo a2b | grep ['[:digit:]']
a2b
In [20]:
echo a2b | grep ['[:punct:]']
In [21]:
echo a2,b | grep ['[:punct:]']
a2,b
Alternative expressions
We use the -E argument here to avoid having to escape special characters
-E, --extended-regexp
Interpret pattern as an extended regular expression (i.e. force
grep to behave as egrep).'
In [22]:
echo cat | grep -E '(cat|dog)'
cat
Without -E
We need to escape the special characters (, | and ).
In [23]:
echo cat | grep '\(cat\|dog\)'
cat
We love dogs as well
In [24]:
echo dog | grep -E '(cat|dog)'
dog
But not foxes
In [25]:
echo fox | grep -E '(cat|dog)'
Be careful - use of square brackets means something different
In [26]:
echo fox | grep -E '[cat|dog]'
fox
Character set modifiers
Anchors
^ indicates start of line and $ indicates end of line.
In [27]:
echo abcd | grep ^ab
abcd
In [28]:
echo abcd | grep ab$
In [29]:
echo abcd | grep ^cd
In [30]:
echo abcd | grep cd$
abcd
Repeating characters
• + matches one or more of the preceding character set
• ‘*’ matches zero or more of the preceding character set
• ‘{m, n}’ matches between m and n repeats of the preceding character set.
In [31]:
echo abbbcd | grep abcd
In [32]:
echo abbbcd | grep -E ab+cd
abbbcd
In [33]:
echo abbbcd | grep -E ab*cd
abbbcd
In [34]:
echo abbbcd | grep -E 'ab{1,5}cd'
abbbcd
In [35]:
echo abbbcd | grep -E a[bc]+d
abbbcd
Matching words with word boundaries
\< and \> indicate word boundaries. That is, \<foo\> will only match foo bar or bar foo but
not foobar or barfoo.
In [36]:
echo 'other ones go together' | grep 'the'
other ones go together
In [37]:
echo 'other ones go together' | grep '\<the\>'
In [38]:
echo 'other ones go together' | grep '\<other\>'
other ones go together
Capture groups and back references
In [39]:
echo "123_456_123_456" | grep -E '([0-9]+).*\1'
123_456_123_456
In [40]:
echo "123_456_123_456" | grep -E '([0-9]+)_([0-9]+)_\1_\2'
123_456_123_456
In [41]:
echo "123_456_123_123" | grep -E '([0-9]+)_([0-9]+)_\1_\2'