Unix Script
Unix Script
http://www.virtualpathtech.com
VirtualPath Techno Solutions
INTRODUCTION
What's Kernel
Kernel is the heart of Linux O/S. It manages resource of Linux O/S. Resources means facilities
available in Linux. For e.g. Facility to store data, print data on printer, memory, file
management etc. Kernel decides who will use this resource, for how long and when. It runs
your programs (or set up to execute binary files) its Memory resident portion of Linux. It
performance following task:-
→ I/O management
→ Process management
→ Device management
→ File management
→ Memory management
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 1 of 122
Types of Shells in Linux:
→ Shell is not part of the system kernel, but it uses system kernel to execute the
programs.
→ There are 13 types of shells widely used across, Among them SH, BASH, KSH are
more popular
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 2 of 122
Creating a Shell Script:
→ To write shell script we can use any of the Linux's text editor such as vi, vim, nano,
emacs etc., even you can use cat command.
→ It is preferable to use vi or vim editor as it is widely used and known by many Linux
admins.
Basics of Unix/Linux:
→ Before we can go to shell scripting, let’s review some important basics of Unix/Linux
administration.
→ To determine the type of the file we can get it from ls –l command. The first
character defines the type.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 3 of 122
File Permissions:
→ To change the ownership permissions of file using chown command and this
command can only be executed by root user.
→ To change the file actions permissions of a file using chmod command and this can
be given by the owner of the file.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 4 of 122
Let’s Get Started:
Shebang:
The #! Syntax used in scripts to indicate an interpreter for execution under UNIX /
Linux operating systems. Most Linux shell and perl / python script starts with the following
line:
#!/bin/bash
OR
#!/usr/bin/perl
OR
#!/usr/bin/python
→ It is called a shebang or a "bang" line.
→ It is nothing but the absolute path to the Bash interpreter (shell).
→ It consists of a number sign and an exclamation point character (#!), followed by the
full path to the interpreter such as /bin/bash.
→ All scripts under Linux execute using the interpreter specified on a first line.
→ Almost all bash scripts often begin with #!/bin/bash (assuming that Bash has been
installed in /bin)
→ This ensures that Bash will be used to interpret the script, even if it is executed
under another shell.
→ The shebang was introduced by Dennis Ritchie between Version 7 Unix and 8 at Bell
Laboratories. It was then also added to the BSD line at Berkeley
→ If no shebang interpreter line is added, then it would consider /bin/sh as default. It is
recommended to give #!/bin/bash for best results.
Do you know?
→ In musical notation, a "#" is called a sharp and an exclamation point - "!" - is
sometimes referred to as a bang. Thus, shebang becomes a shortening of sharp-
bang. The term is mentioned in Elizabeth Castro's Perl and CGI for the World Wide
Web.
Comment:
→ You should be aware of the fact that you might not be the only person reading your
code. A lot of users and system administrators run scripts that were written by other
people. If they want to see how you did it, comments are useful to enlighten the
reader.
→ Comments also make your own life easier. Say that you had to read a lot of man
pages in order to achieve a particular result with some command that you used in
your script. You won't remember how it worked if you need to change your script
after a few weeks or months, unless you have commented what you did, how you
did it and/or why you did it.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 5 of 122
Executing a Shell Script:
→ The most simplest and the easiest way to run a script in Unix/Linux is as follows
Or
→ Do you know that almost 90% of first time writers of shell script will get below error
This error is because every script needs execute permission which might be missing
So, let’s first apply the execute “x” permission on the file
ECHO COMMAND
The built-in echo command is an older form of printf command in Linux/Unix systems.
It used to display the text or variables on the output screen. The following the example
narrates the usage of echo command.
Output
Options:
→ -n: (No New Line) a line field is automatically added after the string is displayed, It
can be suppressed with the - n option.
→ -e : (enable escape sequence) If the - e option is enabled then echo command will
enable escape sequences
\n - New line escape sequence.
\t - New horizontal tap escape sequence.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 6 of 122
ECHO -n Option:
→ ECHO Command is used to print the text on the output screen and by default it
creates a new line feed automatically after the string is displayed.
→ We can suppress ·t using -n option and the following commands will show the
difference in using -n option.
Without –n Option:
With –n Option:
In the above example you can see the command prompt is in the same line instead of new
line if we are using -n option.
Echo –e option:
→ Option -e stands for enabling escape sequence in echo command. In some cases you
may require to print multiple lines of output using echo command. Usually we have
to use multiple echo commands to do so, but we can do the same in echo command
by enabling -e option in it.
→ The following are the most used option in echo escape sequence.
\n - New line escape sequence.
\t - New horizontal tab escape sequence.
Without –e Option:
In this output to get two lines we have to use two echo commands separated by a “;”
semicolon.
With –e Option:
With \n
With \t
If the escape sequence like \n \t is used without –e it won’t be treated as special characters.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 7 of 122
Lab Exercises:
Output:
Output:
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 8 of 122
Colorizing the Output:
Shell scripts commonly used ANSI escape codes for color output. Following table shows
Numbers representing colors in Escape Sequences.
The "\033[" begins the escape sequence. You can also use "\e[" instead of "\033[", COLOR
specifies a foreground color, according to table above. The "m" terminates escape
sequence, and text begins immediately after that.
Or
The problem with above statement is that the magenta color that starts with the 35 color
code is never switched back to the regular color, so any text you type after the prompt and
even prompt also is still in the Magenta color.
Now you won't see anything new on the screen, as this echo statement was not passed any
string to display. But it has done its job, which was to restore the normal viewing mode.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 9 of 122
→ Escape sequence also allow you to control the manner in which characters are
displayed on the screen.
→ The following table summarizes numbers representing text attributes in Escape
Sequences.
Combining all these Escape Sequences, you can get more fancy effect. Use the following
template for writing colored text on a colored background.
echo -e "\033[COLOR1;COLOR2m sample text\033[0m"
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 10 of 122
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 11 of 122
MISCELLANEOUS COMMANDS
There are few commands which can be used only in scripting and which may make our job
easy in certain situations.
EVAL Command:
The eval command line, performs all shell substitutions, and then executes the
command line and returns the exit status of the executed command.
In the above example if we are expecting a value of 10 as the output but we are not getting
the exact value why because the value is in single quote which is making our variable to
treat-a normal character. In such situation if we need to exactly substitute the values and
then the command is to be executed we have to use eval command.
The colon command is usually used in if statements which requires at least one statement.
Type Command:
The type shell built-in command will make you know whether the command you are
executing is an alias, function, shell built-in or the command installed on the server. The
syntax of type command is as follows
type <Command>
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 12 of 122
The following examples describe the usage of type command
Sleep Command:
The sleep command pauses for a given number of seconds. The basic syntax is
sleep n
Where n is the number of seconds to sleep or pause. Some types of UNIX enable
other time units to be specified. It is usually recommended that n not exceed 65,534.
Sleep can be used to give a user time to read an output message before clearing the screen
Trap Command:
The trap command is used to catch a signal that is sent to a process created due to
execution of script. An action is taken based on the signal by using the action which is define
in the trap command instead of taking the default effect on the process. The basic syntax is
as follows.
trap ' ' <Signal Number>
In the single quotes we can give the commands when appropriate signal is given trap
command will execute those commands. We can get all the signals available in Linux using
Kill -l command.
Note: In single quotes if we don't give any commands then trap command will not allow the
shell to pass those signals to that particular process.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 13 of 122
Lab Exercises:
Output
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 14 of 122
Output:
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 15 of 122
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 16 of 122
QUOTES
Wild Cards:
Wildcards are a shell feature that makes the command line much more powerful
than any GUI file managers. If you want to select a big group of files in a graphical file
manager, you usually have to select them with your mouse. This may seem simple, but in
some cases it can be very frustrating. For, example, you have a directory with a huge
amount of all kinds of files and subdirectories, and you have decided to move all the LOG
files, which have the word "Linux" somewhere in the middle of their names, from that big
directory into another directory. What's a simple way to do this? If the directory contains a
huge amount of differently named LOG files, your task is everything but simple!
In the Linux CLI that task is just as simple to perform as moving only one LOG file, and it's so
easy because of the shell wildcards. Wildcards are special characters that allow you to select
filenames that match certain patterns of characters. This helps you to select even a big
group of files with typing just a few characters, and in most cases it’s easier than selecting
the files with a mouse.
In wild cards we looked at shell substitution, which occurs automatically whenever you
enter a command containing a wildcard character or a $ parameter. The way the shell
interprets these and other special characters is generally useful, but sometimes it is
necessary to turn off shell substitution and let each character stand for itself. Usually we use
echo command to quote the text and to turn off the special meaning of a character is called
quoting, and it can be done in three ways:
→ Using Backslash
→ Using Single Quotes
→ Using Double Quotes
Meta Characters:
Here is a list of most of the shell special characters also called Meta characters.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 17 of 122
Using Backslash:
By using, backslash we can quote or nullify the specialty of only one character at a
time. We can prevent the echo command or shell from interpreting a character by placing a
backslash ("\") in front of a special character.
In the above example to nullify the specialty of the semicolon (;) character we used
backslash.
In the above example we expected the output as $1500 which shown as 500 because of
having dollar ($1) special character initialized by shell as a variable. So after quoting it using
backslash the output is as expected.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 18 of 122
The comparisons table for all the three quotes is as follows.
S.No Parameter Backlash Single Double
Quote Quote
1. Variable Use Backslash to change the No Yes
2. Wild Card meaning of the characters to escape No No
3. Command Substitute special characters. No Yes
Lab Exercises:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 19 of 122
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 20 of 122
Output:
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 21 of 122
Output:
INPUT/ OUTPUT:
Redirectors:
→ The shell and many UNIX commands take their input from standard input (STDIN),
write output to standard output (STDOUT), and write error output to standard error
(STDERR). By default, standard input is connected to the terminal keyboard and
standard output and error to the terminal screen.
→ Redirection of I/O to a file, is accomplished by specifying the destination on the
command line using a redirection Meta characters followed by the desired
destination.
→ The BASH uses a format for redirection which includes numbers. The numbers refers
to the file descriptor numbers (0 standard input, 1 standard output, 2 standard
error).
Characters Actions
> Redirect standard output
2> Redirect standard error
2>&1 Redirect standard error to standard output
< Redirect standard input
| Pipe standard output to another command
>> Appends to standard output
2>&1| Pipe standard output and standard error to another command
Example Details
$ls >list.out Redirects the output of ls command to a file list.out
Note: If there is a file list.out this particular command removes the old
data and add the out of the latest command.
$lss 2>list.err Redirects the STDERR to list.err file. Whereas the normal output will be
printed on the screen.
$ls 2>&1 >list.out Redirects both STDOUT and STDERR > to list.out file
$ls >>list.out Appends output of ls command to list.out file. It will append the latest
data to the file and wont erases the earlier data.
mail -s TEST This command will Send an email to the user but whereas the body
myuser@vpts.com message will be taken from a file list.out instead of STDIN from
<list.out keyboard.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 22 of 122
Pipes:
A pipe is used to connect output of one program or command to the input of other
command/program. It is symbolized by “|”
Filters:
Shell scripts are often called on to manipulate and reformat the output from
commands that they execute. Sometimes this task is as simple as displaying only part of the
output by filtering out certain lines. In most instances, the processing required is much more
sophisticated. Now we see few basic text filtering commands with example.
Head:
head is a program on UNIX and Unix-like systems used to display the first few lines of
a text file or piped data. By default it will show the first 10 lines of a file.
Syntax: head [-number I -n number] filename
$ head list .out
$ head -n 20 list. out
$ head -20 list.out
Tail:
Tail is a program on UNIX and Unix-like systems used to display the last few lines of a
text file or piped data. By default it will show the last 10 lines of a file.
Syntax: tail [options] filename
$ tail –n 5 /etc/passwd ### Prints the last five lines of the file
$ tail –f /var/log/messages ### To see the continuous output of appended lines
Grep:
The word grep stands for globally regular expression print. The grep command
allows you to search one file or multiple files for lines that contain a pattern.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 23 of 122
The following are the examples for grep command with different options.
To search for a word root in /etc/passwd file
To find the block number of search result pattern, it will list the lines of pattern found along
with the block number of the line.
While searching for the multiple files for the same pattern it will display the lines along with
the file names. For example we are searching for a pattern root from files /etc/passwd &
/etc/shadow.
# grep root /etc/passwd /etc/shadow
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 24 of 122
Highlighting the words searched by displaying in colors in the output
Grep can be combined with other commands by taking input from them.
#cat /etc/passwd |grep root
#lvdisplay |grep mylv
Cut Command:
The cut command in UNIX (or Linux) is used to select sections of text from each line
of files. You can use the cut command to select fields or columns from a line by specifying a
delimiter or you can select a portion of the text by specifying the range or characters.
Basically the cut command slices a line and extracts the text.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 25 of 122
Example for –f option with –d
Exit Status:
Any program completes execution under the UNIX or Linux system, it returns a
status back to the system. This status is a number that usually indicates whether the
program successfully ran or not, and hence called as exit status. By convention, an exit
status of zero indicates that a program succeeded, and non-zero (1-255) indicates that it
failed. Failures can be caused by invalid arguments passed to the program, or by an error
condition detected by the program.
The shell variable $? is automatically set by the shell to the exit status of the last command
executed. Naturally, you can use echo to display its value at the terminal.
Note that the numeric result of a "failure" for some commands can vary from one UNIX
version to the next, but success is always signified by a zero exit status.
The following are the few exit status which will be generated regularly while executing the
scripts or commands.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 26 of 122
In shell scripting we can make our scripts generic using the exit codes. We can exit our script
using exit command followed by a number.
Following examples narrates Exit States:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 27 of 122
AWK COMMAND
Awk is a powerful language to manipulate and process text files. It is especially helpful when
the lines in a text files are in a record format, i.e., when each line (record) contains multiple
fields separated by a delimiter.
Even when the input file is not in a record format, you can still use awk to do some basic file
and data processing. You can also write programming logic using awk even when there are
no input files that needs to be processed. In short, AWK is a powerful language that can
come in handy to do daily routine Job.
The employee.txt is a comma delimited file that contains 5 employee records in the
following format
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 28 of 122
Awk program structure (BEGIN, body, END block).
1. BEGIN Block
Syntax for BEGIN block:
BEGIN { awk commands }
The begin Block gets executed only once at the beginning, before awk starts executing the
body block for all the lines in the input file.
→ The begin block is a good place to print report headers, and initialize variables.
→ You can have one or more awk commands in the begin block.
→ The keyword BEGIN should be specified in upper case.
→ Begin block is optional.
2. Body Block
Syntax for body block:
/pattern/ {action}
The body block gets executed once for every line in the input file.
→ If the input file has 10 records, the commands in the body block will be executed 10
times (once for each record in the input file).
→ There is no keyword for the body block. We discussed pattern and action previously.
3. END Block
Syntax of end block:
END { awk-commands }
The end block gets executed only once at the end, after awk completes executing the body
block for all the lines in the input-file.
• The end block is a good place to print report footer and do any clean-up activities.
• You can have one or more awk commands in the end block.
• The keyword END should be specified in upper case.
Note: By default the delimiter taken by awk is either tab space or a single space, To specify
custom delimiter we have to use –F option.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 29 of 122
2. To filter the columns along with enabling search as well.
In the above example along with the filtering column we are also filtering based on text
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 30 of 122
8. To see the usage of Output Field Separator (OFS).
OFS is for input field separator. OFS is for output field separator. OFS is printed
between consecutive fields in the output. By default, awk prints the output fields with space
between the fields.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 31 of 122
11. To check the number of records
→ NR is very helpful. When used inside the loop, this gives the line number. When used
in the END block, this gives the total number of records in the file.
→ Even though NR stands for "Number of Records", it might be appropriate to call this
as "Number of the Record", as it actually gives you the line number of the current
record.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 32 of 122
SED COMMAND
SED stands for Stream Editor. It is very powerful tool to manipulate, filter, and transform
text. Sed can take input from a file, or from a pipe. You might even have several sed one line
commands in your bash startup file that you use for various scenarios without exactly
understanding the sed scripts. For beginners, sed script might look cryptic. Once you
understand the sed commands in detail, you'll be able to solve a lot of complex text
manipulation problems by writing a quick sed script.
→ Sed reads one line at a time from the {input-file} and executes the {sed-commands}
on that particular line.
→ It reads the 1st line from the {input-file} and executes the {sed-commands} on the
1st line. Then it reads the 2nd line from the {input-file} and executes the {sed-
commands} on the 2nd line. Sed repeats this process until it reaches the end of the
{input-file}.
→ There are also a few optional command line options that can be assed to sed as
indicated by [options].
For all sed examples, we will be using the following employee.txt file. Please create this text
file to try out the commands given in this book.
The above employee database contains the following fields for every record:
→ Employee Id
→ Employee Name
→ Title
Let’s see the different ways of editing the file using SED editor with different options.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 33 of 122
7. To print a file we use 'p' command in sed.
In the above you can see the double lines of output. That is due to the sed work behavior
the first entry is due to the backup of the line and whereas the other line is the exact
output.
8. To print a file without any duplicate lines we use -n option in print command of sed.
10. To search multiple expressions and print the matched lines we have to use -e option in
sed command.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 34 of 122
11. To execute multiple commands from a file we have to use - f option.
In the above example we are passing multiple commands which are listed in mycomm.sed
file. Usually we use this option in such an environment where we need commonly used
options each and every time, then we can go with creating a file with all the option and use
that particular file in the sed command.
In the above example we are specifying before print that to print only the 2nd line
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 35 of 122
16. To print lines starting from the 1st match of "Jason" until the 4th line.
17. To print lines starting from the 1st match of "Raj" until the last line.
18. To print lines starting from the line matching "Raj" until the line matching "Jane”
19. To print the line matching "Jason" and 2 lines immediately after that.
The following are the different ways of using the delete (d) command.
1. To delete only the 2nd line from a file.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 36 of 122
3. To delete from line 2 through last line.
6. To delete lines starting from the 1st match of Jason until the 4th line.
7. To delete lines starting from the 1st match of "Raj" until the last line.
8. To delete lines starting from the line matching "Raj" until the line matching.
9. To delete lines starting from the line matching "Jason" and 2 lines immediately after
that.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 37 of 122
10. To delete all empty lines in a file
In the above example you can observe that the content on employee.txt file is copied to a
new file output.txt and also you can see the duplicate lines while writing to other file. We
can use - n option to not print the original lines on the screen. Usually in general people use
Print command to print the content and use redirectors to write in a new file.
2. To Write the content of sed.txt file to output.txt-file without displaying the output
# sed -n w output .txt sed .txt
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 38 of 122
4. To write lines from 1 to 4
8. To write lines starting from the 1st match of "Jason" until the 4th line.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 39 of 122
9. To write the line matching “Jason” and the next 2 lines immediately after that.
2. To Replace Manager with Director only on lines that contains the keyword “Sales”
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 40 of 122
3. To Replace the 1st occurrence of lower case a with upper case A.
5. To write only the line that was changed by the substitute command to output.txt.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 41 of 122
8. To add a new record to the end of the employee.txt file.
2. To insert a new record before the last line of the employee.txt file.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 42 of 122
3. To insert two lines before the line that matches 'Jason'.
The sed change command (c) lets you replace an existing line with new text. The
syntax for change command is as follows.
sed '[address] c the-line-to-insert' input-file
1. To delete the record at line number 2 and replace it with a new record
2. To delete the line that matches 'Raj' and replaces it with two new lines.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 43 of 122
You can also combine the “a, i, and c” commands. The following sed example does all these
three things:
→ a - Append 'Rock Johnson' after 'Jason'
→ i - Insert 'Mark Wahlberg' before 'Jason'
→ c - Change 'Jason' to 'Joe Mason'
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 44 of 122
VARIABLE
A variable in a shell script is a means of referencing a numeric or character value. And unlike
formal programming languages, a shell script doesn't require you to declare a type for your
variables. Thus, you could assign a number to the variable stuff and then use it again in the
same script to hold a string of characters. To access the value (contents) of a variable, prefix
it with a dollar ($) sign. Usually the variables are used to store the path of the files & output
of commands.
Setting a Variable:
The shell enables us to set and unset a variable
Syntax: name=value Ex: NAME=Musab
→ Here 'name' is the name of the variable and 'value' is the data that variable name
hold.
→ In the above example, NAME is the variable and Musab is the data named to the
variable.
→ Variables of this type are called scalar variables, which mean it can hold only one
value at a time.
In order to store multiple words or multiple lines in a variable you need to use either single
quote or double quote.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 45 of 122
Command Substitution:
The Bourne shell can redirect a command's standard output back to the shell's own
command line. That is, you can use a command’s output as an argument to another
command, or you can store the command output in a shell variable. We can do this by
enclosing a command in back quotes (') or in braces (()).
Arithmetic Substitution:
In ksh and bash, the shell enables integer arithmetic to be performed. This avoids having to
run an extra program such as expr or be to do math in a shell script. This feature is not
available in sh.
Syntax: VAR=$((expression))
The following are the operators could be used in the arithmetic substitution.
OPERATOR DESCRIPTION
Note: Arithmetic Substitution usually won't give the value in decimals, to get the values in
decimal way we have to go with other command “bc”.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 46 of 122
Lab Exercises:
Output:
ARRAY VARIABLES
The variable in Bash is a scalar. From Bash 2.0 it can support a type of variable called Array
Variable which can handle multiple values at a same time. All the naming rules discussed for
Shell Variables would be applicable while naming arrays. Shell does not create a bunch of
blank array items to fill the space between indexes. It will keep track of an array index that
contains values. In KSH numerical indices for arrays must be between 0 and 1023. In bash
there is no limitation. The index supports only integers and we cannot use floating and
decimal values. If an array variable with the same name as scalar variable is defined, the
value of scalar variable becomes the value of the element of the array at index 0.
Syntax:
Example:
Fruits[0]="Apple"
Fruits[1]="Mango"
Fruits[2]="Orange"
Fruits[3]="Banana"
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 47 of 122
Another way with which you can initialize the Array variables are as follows:
Syntax:
ArrayName=(Value1 Value2 Value3 .. ValueN)
Example:
Fruits=(Apple Mango Orange Banana)
Output
Unset Variable:
The shell built -in unset command is used for unsetting the data that an initialized variable is
holding.
Syntax: unset <Variable> Ex: unset name
The above example will unset the data for the variable name is holding.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 48 of 122
Read-only Variables:
The Shell allows us to make or mark a variable as read-only; once the value is marked
as read-only it cannot be changed or manipulated.
Syntax: readonly <Variable Name> Ex: readonly name
The above example will set the variable "name" as readonly.
Environment Variables:
Generally when you create a variable it is only valid on your shell, it is not available
for others on different terminal/shell. Environmental variables are such variables which are
available globally for everyone who are using the shell, although they may be connected to
different session of terminal.
Normally all our variables are local. Local variable can be used in same shell, if you load
another copy of shell (by typing the /bin/bash at the $ prompt) then new shell will ignore all
old shell's variable.
To set an environment variable we have to use the export command to use across the
environment.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 49 of 122
Syntax: export <Variable Name> export name
In Bash Shell, with the set command we can check whether the variable is set to some value
or not. If we want to check only the variables which are exported then export -p is the
command.
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 50 of 122
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 51 of 122
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 52 of 122
Output:
Output:
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 53 of 122
READ COMMAND
To get input from the keyboard, you use the read command. The read command takes input
from the keyboard and assigns it to a variable. It is mainly used at the time taking
confirmation from the user to do the jobs that seems to be little dangerous.
Syntax: read <Variable Name> Ex: read a
It reads the input from the user and stores in a variable. In the above example it will store in
variable named "a''.
Read - p Option:
If you would like to print the output while reading a variable then you have use - p
option. This is helpful to reduce one echo command to display the output.
Syntax: read -p "Message to be printed" variable
Ex: read -p "Enter You Name" name
Read -s Option:
If you would like to go with silent read we have to go with - s option in read
command. Usually for reading passwords you have to mention the - s option; read -s option
disables the feature of showing the text that you enter on the screen.
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 54 of 122
Lab Exercises:
Output:
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 55 of 122
CONDITIONS
The order in which commands execute in a shell script is called the flow of the script. In the
scripts that you have looked at so far, the flow is always the same because the same set of
commands executes every time.
In most scripts, you need to change the commands that execute depending on some
condition provided by the user or detected by the script itself. When you change the
commands that execute based on a condition, you change the flow of the script
The case statement is the other major form of flow control available in the shell. Let’s see a
syntax to use it.
case WORD in
pattern1)
list1
;;
pattern2)
list2
;;
esac
The case statement can execute commands based on a pattern matching decision. The
word <WORD> is matched against every pattern <PATTERN n> and on a match, the
associated list <LIST n> is executed. Every command list is terminated by ;;, this rule is
optional for the very last command list (i.e. you can omit the ;; before the esac). In the place
of PATTERN if we give "*" then if the WORD is not matched with any of the PATTERN then
the LIST will be executed.
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 56 of 122
Same thing with multiple patterns, each pattern must be delimit using pipe symbol
case <word> in
<Pattern1>|<Pattern2>)
List1
;;
<Pattern3>)
List2
;;
esac
Output
Lab Exercises:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 57 of 122
Output:
Test Command:
test command or [ expr ] is used to see if an expression is true, and if it is true it
return zero(O), otherwise returns nonzero for false.
Syntax:
test expression
Here expression is constructed using one of the special options to the test command. The
test command returns either 0 (true) or a 1 (false) after evaluating an expression.
The types of expressions understood by test can be broken into three types:
→ File tests
→ String comparisons
→ Numerical comparisons
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 58 of 122
File Tests:
File test expressions test whether a file fits some particular criteria. The general
syntax for a file test is
test option file or [ option file ]
Option Description
In the above example we are checking a file case2.sh as regular file or 0 using –f option and
it returned a value 0 (true) which means it is true that particular file is a regular file. Then we
are checking the same file whether it is block special file or not using -b option and it
returned a value 1 (false) which means that particular file is not a block special file. Likewise
we can check the files with all the options mentioned in the above table.
The test command also supports string comparisons. We can perform the following
operations in string comparisons.
→ Checking whether a string is empty or not.
→ Checking whether two strings are equal or not.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 59 of 122
The syntax for string comparison is
test option string or [ option string ]
In the above-example we are defining a variable with an empty data and we are using -z
option to check the variable has zero length and it returned a zero value as the string is
empty. The same variable we checked with -n option and it returned a non-zero value as it
doesn’t have data.
Note: Make sure to use double quotes for strings while using the options.
In the above example we initialized two variables with different data in it and we are
checking them whether those are equal or not. Where first returned 1 and other 0.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 60 of 122
Numerical Comparisons:
The test command enables us to compare two integers. The Syntax is as follows.
test integer1 option integer2 or [ integer1 operator integer2 ]
Option Description
int1 -eq int2 True if int1 is equals to int2.
int1 -ne int2 True if int1 is not equal to int2.
int1 -lt int2 True if int1 is less than int2.
int1 -le int2 True if int1 is less than or equal to int2.
int1 -gt int2 True if int1 is greater than int2.
int1 -ge int2 True if int1 is greater than or equal to int2.
The IF Conditions:
The “if” conditional statements perform different computations or actions
depending on whether a programmer-specified Boolean condition evaluates to true or
false. These statements are used to execute different parts of your shell program depending
on whether certain conditions are true. The ability to branch makes shell scripts powerful.
Simple if:
This statement is also called as simple if statement, the example is as follows.
If [ conditional expression ]
Then
Statement 1
Statement 2
...
fi
If the given conditional expression is true or returns zero, it enters and executes the
statements enclosed between the keywords "then" and "fi". If the given expression is false
or returns a non-zero, then consequent statement list is executed.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 61 of 122
If Else:
If then else statement are usually called as if else statement and the syntax is as
follows.
if [ conditional expression ]
then
Statement1
Statement2
...
else
Statement3
Statement4
...
fi
If the conditional expression is true or returns a zero value, it executes the statement1 and
2. If the conditional expression is false or returns a nonzero, it jumps to else part, and
executes the statement3 and 4. After the execution of if/else part, execution resume with
the consequent statements.
Else If Ladder:
If..elif..else..fi statements are called as Else if ladder statements. The syntax is as
follows
if [ conditional expression1 ]
then
Statement1
Statement2
elif [ conditional expression2 ]
then
Statement3
Statement4
else
Statement5
fi
If you want to check more than one conditional expression then you have to use Else If
conditional statements. It checks expression 1, if it is true executes statement 1, 2. If
expression1 is false, it checks expression2, and if all the expression is false, then it enters
into else block and executes the statements in the else block.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 62 of 122
The following example narrates the usage of Else-If Statements.
Nested If:
lf..then..if..then..fi..fi Statements are called as Nested If Statements. The Syntax is as
follows.
if [ conditional expression1 ]
then
statement1
statement2
else
if [ conditional expression2 ]
then
statement3
fi
fi
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 63 of 122
Lab Exercises:
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 64 of 122
Output:
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 65 of 122
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 66 of 122
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 67 of 122
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 68 of 122
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 69 of 122
Compound Expressions
→ So far you have seen individual expressions, but many times you need to combine
expressions in order to satisfy a particular expression. When two or more
expressions are combined, the result is called a compound expression.
→ You can create compound expressions using the test command's built in operators,
or you can use the conditional execution operators, && and ||.
→ Also you can create a compound expression that is the negation of another
expression by using the! Operator.
The following is the example that narrates the usage of compound expressions of
commands.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 70 of 122
An example for the usage of compound expressions of if statement
Output:
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 71 of 122
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 72 of 122
Output
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 73 of 122
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 74 of 122
LOOPS
Loops enable you to execute a series of commands multiple times. There are two basic
types of loops
→ The while loop
→ The for loop
While loop.
The while loop is a control flow statement that allows code or commands to be
executed repeatedly based on a given condition or expression is true or not. The following is
the syntax for basic while loop.
while [ condition ]
do
command1
command2
command3
done
If the given condition is true then the given commands will be executed continuously
one by one till the condition get fails. So it is all our responsibility to control the condition
otherwise the loop may get into infinite loop.
Note: The conditions can be either numerical or string or file check conditions.
The following is the example that narrates the usage of while loop.
Lab Exercises:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 75 of 122
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 76 of 122
Output:
UNTIL Loop:
Until loop is very similar to the while loop, except that the loop executes until the
expression executes successfully. As long as the given condition fails, the loop continues.
The following is the basic syntax for until loop.
until [ condition ]
do
commandl
command2
command3
done
If the given condition is false then the given commands will be executed till the condition
get pass or true. Until loop is actually the reverse of the while loop and also in other words
these two loops are in vice versa in terms of logic in condition checking.
Lab Exercises:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 77 of 122
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 78 of 122
Output:
Output:
FOR Loop:
Unlike While and Until loop, FOR Loop operates on list of items given, for loop are typically
used when the number of operations is known before entering the bash loop. Bash supports
two kinds of for loop. The first form of bash for loop is:
for varname in list
do
commands ##Body of the loop
done
In the above syntax:
→ for, in, do and done are keywords
→ List is any list which has list of items
→ varname is any Bash variable name.
In this form, the for statement executes the command enclosed in a body, once for each
item in the list. The current item from the list will be stored in a variable "varname" each
time through the loop. This varname can be processed in the body of the loop. This list can
be a variable that contains several words separated by spaces. If list is missing in for
statement, then it takes the positional parameters that were passed into the shell.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 79 of 122
The following is the example narrating FOR Loop first form.
The second form of for loop is similar to for loop in 'C' programming language, which has
three expression (initialization, condition and updating).
for (( expr1; expr2; expr3 ))
do
commands
done
→ In the above bash for command syntax, before the first iteration, expr1 is evaluated.
This is usually used to initialize variables for the loop.
→ All the statements between do and done is executed repeatedly until the value of
expr2 is TRUE.
→ After each iteration of the loop, expr3 is evaluated. This is usually use to increment a
loop counter.
The following is the example narrating FOR Loop Second form:
#!/bin/bash
Lab Exercises:
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 80 of 122
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 81 of 122
Output:
Note: You can see the difference in output while executing the program.
Usually select loop includes the either case or Else-if Statements for better control on the
loops.
Lab Exercises:
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 82 of 122
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 83 of 122
Output:
Loop Control:
The break and continue are loop control commands in shell to have better control of
loops in some cases of infinite loop or even in regular loops. The break command terminates
the loop (breaks out of it), while continue causes a jump to the next iteration of the loop,
skipping all the remaining commands in that particular loop cycle.
Lab Exercises:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 84 of 122
Output:
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 85 of 122
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 86 of 122
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 87 of 122
FUNCTIONS
Bash shell functions are a way to group several UNIX I Linux commands for later execution
using a single name for the group. Bash shell function can be executed just like a regular
UNIX command. Shell functions are executed in the current shell context without creating
any new process to interpret them.
Both bash aliases and functions allow you to define shortcuts for longer or more
complicated commands. However, aliases don't allow control-flows, arguments, and other
trickery things which these functions allows.
Using Functions:
The syntax for creating a functions goes as follows.
name( )
{
list
}
In the above syntax name is the name of the function and list is the list of commands. The
list of commands, list, is referred to as the body of the function. And also the parenthesis i.e,
( { } ) are required followed by the name of function name. The job of a function is to bind
name to list, so that whenever name is specified, list is executed. When a function is
defined, list is not executed; the shell parses list to ensure that there are no syntax errors
and stores name in its list of commands.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 88 of 122
Local Variables in functions:
By default all the variables in shell are global. Modifying such variables will change
the value in the whole script. So this may lead to have bugs while building a large scripts, we
can overcome this by defining a variables locally in the function using local command. The
scope these functions will be within the function and also global variables will temporarily
overwritten by local variables.
Export a Function:
We can export a function using export command –f option. The following example
explains the function exporting.
fname( ) {
echo "Welcome to Scripting
}
export -f fname
Read-only Functions:
We can make a function as readonly using readonly command -f option. The
following example narrates readonly function.
fname( ) {
echo "Welcome to Scripting
}
readonly -f fname
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 89 of 122
Lab Exercises:
#vim script50.sh
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 90 of 122
Output:
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 91 of 122
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 92 of 122
OPTION PARSING
Special Variables:
The shell defines several special variables that are relevant to option parsing. In
addition to these, a few variables give the status of commands that the script executes. The
following table describes all of the special variables defined by the shell.
Variable Description
$0 The name of the command being executed. For shell scripts, this is the path
with which it was invoked.
$n These variables correspond to the arguments with which a script was invoked.
Here n is a positive decimal number corresponding to the position of an
argument (the first argument is $1, the second argument is $2, and so on).
$# The number of arguments supplied to a script.
$* All the arguments are double quoted. If a script receives two arguments, $* is
equivalent to $1 $2.
$@ All the arguments are individually double quoted. If a script receives two
arguments, $@ is equivalent to $1 $2.
$? The exit status of the last command executed.
$$ The process number of the current shell. For shell scripts, this is the process ID
under which they are executing.
$! The process number of the last background command.
Usage Statements:
Another common use for $0 is in the usage statement for a script, which is a short
message informing the user how to invoke the script properly. All scripts used by more than
one user should include such a message.
In general, the usage statement is something like the following:
echo "Usage: $0 [options] [files]"
Using Basename
Currently, the message displays the entire path with which the shell script was invoked, but
what is really required is the name of the shell script. You can correct this by using the
basename command. The basename command takes an absolute or relative path and
returns the file or directory name. Its basic syntax is
basename file
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 93 of 122
Let’s see it with an example
You can use the basename command in echo input giving it as command in command
quotes.
$ USAGE="Usage: `basename $0` Input"
$ echo $USAGE
Shift Command:
Using shift command, command line arguments can be accessed. This command
causes the positional parameters shift to the left. Shift [n] where n defaults to 1. It is useful
when several parameters need to be parsed to a script because the values are limited to
only 9.
For example in early $1=10, $2=20, $3=30. If we use the shift command the pointer will shift
a value and $1=20 and $2=30. If we give a number to shift command then it will shift that
many values at a time. A detailed example will be coming later in the manual.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 94 of 122
Lab Exercises:
#vim script55.sh
Output:
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 95 of 122
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 96 of 122
Output:
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 97 of 122
Output:
GETOPTS
Getopts obtains options and their arguments from a list of parameters that follows the
standard POSIX.2 option syntax (that is, single letters proceeded by a - and possibly followed
by an argument value). Typically, shell scripts use getopts to parse arguments passed to
them. When you specify args on the getopts command line, getopts parses those
arguments instead of the script command line.
The process by which getopts parses the options given on the command line is
1. The getopts option examines all the command line arguments, looking for arguments
starting with the - character.
2. When an argument starting with the - character is found, it compares the characters
following the – to the characters given in the option-siring.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 98 of 122
3. If a match is found, the specified variable is set to the option: otherwise, variable is
set to the? Character.
4. Steps 1 through 3 are repeated until all the options have been considered.
Getopts always includes with while Ioop in order to read all the options in a loop manner
and also most of the times getopts has to include in order to read the arguments and the
values parsed to arguments and to store in a constant variable.
If any value is missing to the argument or any other unknown options were parsed then
getopts by default gives errors, In order to handle these errors we can either set OPTERR
value to 0 or we can start the options in getopts with colon.
OPTERR: (Values 0 or 1) Indicates if Bash should display error messages generated by the
getopts built-in. The value is initialized to 1 on every shell startup - so be sure to always set
it to 0 if you don’t want to see annoying messages!
The other way of managing the unwanted output is redirecting the error output to
/dev/null.
Using OPTERR:
OPTERR=0
while getopts a:b: var
Using Colon:
while getopts :a:b: var
Using redirection:
while getopts a:b: var >/dev/null 2>&1
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 99 of 122
The special variables we are talking about are different for each and every command
we execute, likewise different for each and individual script. Function will have its own
environment created and at the same time the special variables also will be in initialized for
a function too in shell.
The following example narrates the usage of functions with special variables.
In the above example you can see the values parsed to the function and we accessing those
values like the same as in normal script using the special variables.
Note: The scope of these special variables in a function is in the function itself, it cannot
affect the other functions as well in the main program.
Lab Exercises:
#vim script60.sh
Output:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Output
several lines of
my data
listed here
A heredoc can also be used to comment or inactive multiple lines instead of using “#”.
An example for commenting multiple lines with heredoc is as follows.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
else
echo "BACKUP FAILED"
exit
fi
sleep 2
echo "COPYING THE FILE ON 61 SERVER"
scp $dest$src* $server:/opt
if [ $? -eq 0 ];then
echo "copying done successfully"
else
echo "Copying failed"
fi
Note: if there is a trusted relationship or password less login is configured the files will be
automatically transferred, else it will wait for manual entry of password.
4. Taking backup of the user given input file, storing on user defined destination and
transferring to other server.
#!/bin/bash
##SCRIPT TO TAKE BACKUP
server=192.168.10.61
date=`date +%Y%m%d`
clear
read -p 'Pl enter full path of the file to backup: ' file
read -p 'Pl enter the destination to store the backup: ' dest
if [ -z $file -o -z $dest ]; then
echo "Invalid Input"
exit
fi
echo "BACKUP IS STARTING"
sleep 2
http://www.virtualpathtech.com
VirtualPath Techno Solutions
else
echo "BACKUP FAILED"
exit
fi
sleep 2
echo "COPYING THE FILE ON 61 SERVER"
scp $dest/$file* $server:/opt
if [ $? -eq 0 ];then
echo "copying done successfully"
else
echo "Copying failed"
fi
Note: if there is a trusted relationship or password less login is configured the files will be
automatically transferred, else it will wait for manual entry of password.
5. Script to monitor CPU usage and send mails as per critical and warning situation
$vim cpumon.sh
#!/bin/bash
##PurPose: Real time CPU Utilization Monitoring Shell Script
##Date: 13th Nov 2017
HOSTNAME=`hostname`
PATHS="/"
WARNING=90
CRIT=98
CAT=/bin/cat
MAILER=/bin/mail
CRITmailto="YOUREMAIL@DOMAIN.COM" ##replace your email for critical alerts
mailto="YOUREMAIL@DOMAIN.COM" ##replace your email for warning alerts
mkdir -p /var/log/cpuhistory
LOGFILE=/var/log/cpuhistory/hist-`date +%h%d%y`.log
touch $LOGFILE
for path in $PATHS
do
CPU_LOAD=`top -b -n 2 -d1 | grep "Cpu(s)" | tail -n1 | awk '{print $2}' |awk -F. '{print $1}'`
if [ -n "$WARNING" -a -n "$CRIT" ]; then
if [ "$CPU_LOAD" -ge "$WARNING" -a "$CPU_LOAD" -lt "$CRIT" ]; then
echo " `date "+%F %H:%M:%S"` WARNINGING - $CPU_LOAD on Host
$HOSTNAME" >> $LOGFILE
echo "CPU Load is Warning $CPU_LOAD on $HOSTNAME" | $MAILER -s
"CPU Load is Warning $CPU_LOAD on $HOSTNAME" $mailto
exit 1
http://www.virtualpathtech.com
VirtualPath Techno Solutions
My dear students/aspirants, scripting is all about logic and creativity. There are many
situations in which you can use scripting for automation, monitoring and other important
activities. It’s all about your necessity and requirements which you can put in a script and
make your work easy. After all “Necessity is the mother of all inventions”. So, keep
inventing and exploring the power of scripting, you’ll never be bored of it and will always
be surprised and admire it. –Musabuddin Syed.
http://www.virtualpathtech.com
VirtualPath Techno Solutions