How To Write Shell Script: Prev Next
How To Write Shell Script: Prev Next
Note: This will set read write execute(7) permission for owner, for group and other permission is read and execute only(5).
(3) Execute your script as
syntax:
bash your-script-name
sh your-script-name
./your-script-name
Examples:
NOTE In the last syntax ./ means current directory, But only . (dot) means execute given command file in current shell
without starting the new copy of shell, The syntax for . (dot) command is as follows
Syntax:
. command-name
Example:
Now you are ready to write first shell script that will print "Knowledge is Power" on screen. See the common vi command
list , if you are new to vi.
After saving the above script, you can run the script as follows:
This will not run script since we have not set execute permission for our script first; to do this type command
How Shell Locates the file (My own bin directory to execute script)
Tip: For shell script file try to give file extension such as .sh, which can be easily identified by you as shell script.
Exercise:
1)Write following shell script, save it, execute it and note down it's output.
Future Point: At the end why statement exit 0 is used? See exit status for more information.
Variables in Shell
To process our data/information, data must be kept in computers RAM memory. RAM memory is
divided into small locations, and each location had unique number called memory location/address,
which is used to hold our data. Programmer can give a unique name to this memory location/address
called memory variable or variable (Its a named storage location that may take different values, but only
one at a time).
In Linux (Shell), there are two types of variable:
(1) System variables - Created and maintained by Linux itself. This type of variable defined in
CAPITAL LETTERS.
(2) User defined variables (UDV) - Created and maintained by user. This type of variable defined in
lower letters.
You can see system variables by giving command like $ set, some of the important System variables are:
System Variable Meaning
BASH=/bin/bash Our shell name
BASH_VERSION=1.14.7(1) Our shell version name
COLUMNS=80 No. of columns for our screen
HOME=/home/vivek Our home directory
LINES=25 No. of columns for our screen
LOGNAME=students students Our logging name
OSTYPE=Linux Our Os type
PATH=/usr/bin:/sbin:/bin:/usr/sbin Our path settings
PS1=[\u@\h \W]\$ Our prompt settings
PWD=/home/students/Common Our current working directory
SHELL=/bin/bash Our shell name
USERNAME=vivek User name who is currently login to this PC
NOTE that Some of the above settings can be different in your PC/Linux environment. You can print any
of the above variables contains as follows:
Exercise:
1) If you want to print your home directory location then you give command:
a)
OR
(b)
Which of the above command is correct & why? Click here for answer.
Caution: Do not modify System variable this can some time create problems.
Example:
# this is ok
# Error, NOT Ok, Value must be on right side of = sign.
To define variable called 'vech' having value Bus
But there will be problem for any of the following variable declaration:
(3) Variables are case-sensitive, just like filename in Linux. For e.g.
Above all are different variable name, so to print value 20 we have to use $ echo $NO and not any of the
following
# will print 10 but not 20
# will print 11 but not 20
# will print 2 but not 20
(4) You can define NULL variable as follows (NULL variable is variable which has no value at the time
of definition) For e.g.
$ vech=
$ vech=""
Try to print it's value by issuing following command
Nothing will be shown because variable has no value i.e. NULL variable.
(5) Do not use ?,* etc, to name your variable names.
How to define User defined variables How to print or access value of UDV
Up
(UDV) (User defined variables)
It will print 'Bus',To print contains of variable 'n' type command as follows
Caution: Do not try $ echo vech, as it will print vech instead its value 'Bus' and $ echo n, as it will print
n instead its value '10', You must use $ followed by variable name.
Exercise
Q.1.How to Define variable x with value 10 and print it on screen.
Q.2.How to Define variable xn with value Rani and print it on screen
Q.3.How to print sum of two numbers, let's say 6 and 3?
Q.4.How to define two variable x=20, y=5 and then to print division of x and y (i.e. x/y)
Q.5.Modify above and store division of x and y to variable called z
Q.6.Point out error if any in following script
echo Command
Use echo command to display text or value of variable.
echo [options] [string, variables...]
Displays text or variables value on screen.
Options
-n Do not output the trailing new line.
-e Enable interpretation of the following backslash escaped characters in the strings:
\a alert (bell)
\b backspace
\c suppress trailing new line
\n new line
\r carriage return
\t horizontal tab
\\ backslash
For e.g. $ echo -e "An apple a day keeps away \a\t\tdoctor\n"
How to display colorful text on screen with bold or blink effects, how to print text on any row, column
on screen, click here for more!
Shell Arithmetic
Use to perform arithmetic operations.
Syntax:
expr op1 math-operator op2
Examples:
Note:
expr 20 %3 - Remainder read as 20 mod 3 and remainder is 2.
expr 10 \* 3 - Multiplication use \* and not * since its wild card.
For the last statement not the following points
(1) First, before expr keyword we used ` (back quote) sign not the (single quote i.e. ') sign. Back quote is
generally found on the key under tilde (~) on PC keyboard OR to the above of TAB key.
(2) Second, expr is also end with ` i.e. back quote.
(3) Here expr 6 + 3 is evaluated to 9, then echo command prints 9 as sum
(4) Here if you use double quote or single quote, it will NOT work
For e.g.
$ echo "expr 6 + 3" # It will print expr 6 + 3
$ echo 'expr 6 + 3' # It will print expr 6 + 3
Example:
$ echo "Today is date"
Can't print message with today's date.
$ echo "Today is `date`".
It will print today's date as, Today is Tue Jan ....,Can you see that the `date` statement uses back quote?
Exit Status
By default in Linux if particular command/shell script is executed, it return two type of values which is
used to see whether command or shell script executed is successful or not.
(1) If return value is zero (0), command is successful.
(2) If return value is nonzero, command is not successful or some sort of error executing command/shell
script.
This value is know as Exit Status.
But how to find out exit status of command or shell script?
Simple, to determine this exit Status you can use $? special variable of shell.
For e.g. (This example assumes that unknow1file doest not exist on your hard drive)
$ rm unknow1file
It will show error as follows
rm: cannot remove `unkowm1file': No such file or directory
and after that if you give command
$ echo $?
it will print nonzero value to indicate error. Now give command
$ ls
$ echo $?
It will print 0 to indicate command is successful.
Exercise
Try the following commands and not down the exit status:
$? useful variable, want to know more such Linux variables click here to explore them!
Run it as follows:
Wild card
Meaning Examples
/Shorthand
$ ls * will show all files
will show all files whose first
$ ls a*
name is starting with letter 'a'
Matches any string or group of will show all files having
* $ ls *.c
characters. extension .c
will show all files having
$ ls ut*.c extension .c but file name must
begin with 'ut'.
will show all files whose names
$ ls ?
are 1 character long
? Matches any single character. will show all files whose names
$ ls fo? are 3 character long and file name
begin with fo
Matches any one of the will show all files beginning with
[...] $ ls [abc]*
enclosed characters letters a,b,c
Note:
[..-..] A pair of characters separated by a minus sign denotes a range.
Example:
$ ls /bin/[a-c]*
Will show all files name beginning with letter a,b or c like
/bin/arch /bin/awk /bin/bsh /bin/chmod /bin/cp
/bin/ash /bin/basename /bin/cat /bin/chown /bin/cpio
/bin/ash.static /bin/bash /bin/chgrp /bin/consolechars /bin/csh
But
$ ls /bin/[!a-o]
$ ls /bin/[^a-o]
If the first character following the [ is a ! or a ^ ,then any character not enclosed is matched i.e. do not
show us file name that beginning with a,b,c,e...o, like
Answer:
No. of argument to this command
Command Actual Argument
(i.e $#)
ls 1 foo
cp 2 y and y.bak
mv 2 y.bak and y.okay
tail 2 -10 and myf
mail 1 raj
sort 3 -r, -n, and myf
date 0
clear 0
NOTE:
$# holds number of arguments specified on command line. And $* or $@ refer to all arguments passed to
script.
In shell if we wish to refer this command line argument we refer above as follows
myshell it is $0
foo it is $1
bar it is $2
Here $# (built in shell variable ) will be 2 (Since foo and bar only two Arguments), Please note at a time
such 9 arguments can be used from $1..$9, You can also refer all of them by using $* (which expand to
`$1,$2...$9`). Note that $1..$9 i.e command line arguments to shell script is know as "positional
parameters".
Exercise
Try to write following for commands
Shell Script Name ($0),
No. of Arguments (i.e. $#),
And actual argument (i.e. $1,$2 etc)
Answer
Following script is used to print command ling argument and will show you how to access them:
Run it as follows
$ ls > filename
It means put output of ls command to filename.
There are three main redirection symbols >,>>,<
(1) > Redirector Symbol
Syntax:
Linux-command > filename
To output Linux-commands result (output of command or shell script) to file. Note that if file already
exist, it will be overwritten else new file is created. For e.g. To send output of ls command give
$ ls > myfiles
Now if 'myfiles' file exist in your current directory it will be overwritten without any type of warning.
(2) >> Redirector Symbol
Syntax:
Linux-command >> filename
To output Linux-commands result (output of command or shell script) to END of file. Note that if file
exist , it will be opened and new information/data will be written to END of file, without losing previous
information/data, And if file is not exist, then new file is created. For e.g. To send output of date
command to already exist file give command
$ date >> myfiles
(3) < Redirector Symbol
Syntax:
Linux-command < filename
To take input to Linux-command from file instead of key-board. For e.g. To take input for cat command
give
$ cat < myfiles
Pipes
A pipe is a way to connect the output of one program to the input of another program without any
temporary file.
Filter
If a Linux command accepts its input from the standard input and produces its output on standard output
is know as a filter. A filter performs some kind of process on the input and gives output. For e.g..
Suppose you have file called 'hotel.txt' with 100 lines data, And from 'hotel.txt' you would like to print
contains from line number 20 to line number 30 and store this result to file called 'hlist' then give
command:
$ tail +20 < hotel.txt | head -n30 >hlist
Here head command is filter which takes its input from tail command (tail command start selecting from
line number 20 of given file i.e. hotel.txt) and passes this lines as input to head, whose output is
redirected to 'hlist' file.
Consider one more following example
$ sort < sname | uniq > u_sname
Here uniq is filter which takes its input from sort command and passes this lines as input to uniq; Then
uniqs output is redirected to "u_sname" file.
What is Processes
Process is kind of program or task carried out by your PC. For e.g.
$ ls -lR
ls command or a request to list files in a directory and all subdirectory in your current directory - It is a
process.
Process defined as:
"A process is program (command given by user) to perform specific Job. In Linux when you start
process, it gives a number to process (called PID or process-id), PID starts from 0 to 65535."
$ ls / -R | wc -l
This command will take lot of time to search all files on your system. So you can run such command in
Background or simultaneously by giving command like
$ ls / -R | wc -l &
The ampersand (&) at the end of command tells shells start process (ls / -R | wc -l) and run it in
background takes next command immediately.
Process & PID defined as:
"An instance of running command is called process and the number printed by shell is called process-id
(PID), this PID can be use to refer specific running process."
1) Is it example of Multitasking?
2) How you will you find out the both running process (MP3 Playing & Letter typing)?
3) "Currently only two Process are running in your Linux/PC environment", Is it True or False?, And
how you will verify this?
4) You don't want to listen music (MP3 Files) but want to continue with other work on PC, you will take
any of the following action:
1. Turn off Speakers
2. Turn off Computer / Shutdown Linux Os
3. Kill the MP3 playing process
4. None of the above
Click here for answers.
Introduction
Making decision is important part in ONCE life as well as in computers logical driven program. In fact
logic is not LOGIC until you use decision making. This chapter introduces to the bash's structured
language constructs such as:
Decision making
Loops
Is there any difference making decision in Real life and with Computers? Well real life decision are quite
complicated to all of us and computers even don't have that much power to understand our real life
decisions. What computer know is 0 (zero) and 1 that is Yes or No. To make this idea clear, lets play
some game (WOW!) with bc - Linux calculator program.
$ bc
After this command bc is started and waiting for your commands, i.e. give it some calculation as follows
type 5 + 2 as:
5+2
7
7 is response of bc i.e. addition of 5 + 2 you can even try
5-2
5/2
See what happened if you type 5 > 2 as follows
5>2
1
1 (One?) is response of bc, How? bc compare 5 with 2 as, Is 5 is greater then 2, (If I ask same question to
you, your answer will be YES), bc gives this 'YES' answer by showing 1 value. Now try
5<2
0
0 (Zero) indicates the false i.e. Is 5 is less than 2?, Your answer will be no which is indicated by bc by
showing 0 (Zero). Remember in bc, relational expression always returns true (1) or false (0 - zero).
Try following in bc to clear your Idea and not down bc's response
5 > 12
5 == 10
5 != 2
5 == 5
12 < 2
Expression Meaning to us Your Answer BC's Response
5 > 12 Is 5 greater than 12 NO 0
5 == 10 Is 5 is equal to 10 NO 0
5 != 2 Is 5 is NOT equal to 2 YES 1
5 == 5 Is 5 is equal to 5 YES 1
1<2 Is 1 is less than 2 Yes 1
It means when ever there is any type of comparison in Linux Shell It gives only two answer one is YES
and NO is other.
In Linux Shell Value Meaning Example
Zero Value (0) Yes/True 0
Remember both bc and Linux Shell uses different ways to show True/False values
Value Shown in bc as Shown in Linux Shell as
True/Yes 1 0
False/No 0 Non - zero value
if condition
if condition which is used for decision making in shell script, If given condition is true then command1 is
executed.
Syntax:
For compression you can use test or [ expr ] statements or even exist status can be also used.
Example:
Following script determine whether given argument number is positive.
Run it as follows
$ chmod 755 ispostive
$ ispostive 5
5 number is positive
$ispostive -45
Nothing is printed
$ispostive
./ispostive: test: -gt: unary operator expected
Detailed explanation
The line, if test $1 -gt 0 , test to see if first command line argument($1) is greater than 0. If it is true(0)
then test will return 0 and output will printed as 5 number is positive but for -45 argument there is no
output because our condition is not true(0) (no -45 is not greater than 0) hence echo statement is skipped.
And for last statement we have not supplied any argument hence error ./ispostive: test: -gt: unary
operator expected, is generated by shell , to avoid such error we can test whether command line argument
is supplied or not.
test or [ expr ] works with
1.Integer ( Number without decimal point)
2.File types
3.Character strings
Logical Operators
Logical operators are used to combine two or more condition at a time
Operator Meaning
! expression Logical NOT
expression1 -a expression2 Logical AND
if...else...fi
If given condition is true then command1 is executed otherwise command2 is executed.
Syntax:
Try it as follows:
$ chmod 755 isnump_n
$ isnump_n 5
5 number is positive
$ isnump_n -45
$ isnump_n
./ispos_n : You must give/supply one integers
$ isnump_n 0
0 number is negative
Detailed explanation
First script checks whether command line argument is given or not, if not given then it print error
message as "./ispos_n : You must give/supply one integers". if statement checks whether number of
argument ($#) passed to script is not equal (-eq) to 0, if we passed any argument to script then this if
statement is false and if no command line argument is given then this if statement is true. The echo
command i.e.
echo "$0 : You must give/supply one integers"
| |
| |
1 2
1 will print Name of script
2 will print this error message
And finally statement exit 1 causes normal program termination with exit status 1 (nonzero means script
is not successfully run).
The last sample run $ isnump_n 0 , gives output as "0 number is negative", because given argument is
not > 0, hence condition is false and it's taken as negative number. To avoid this replace second if
statement with if test $1 -ge 0.
Nested if-else-fi
You can write the entire if-else construct within either the body of the if statement of the body of an else
statement. This is called the nesting of ifs.
Multilevel if-then-else
Syntax:
Above program gives error for last run, here integer comparison is expected therefore error like "./elf: [: -gt: unary operator
expected" occurs, but still our program notify this error to user by providing message "Opps! a is not number, give number".
for Loop
Syntax:
In above 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.
Exercise
Try to understand the shell scripts (for loops) shown in exercise chapter.
while loop
Syntax:
Loop is executed as long as given condition is true. For e.g.. Above for loop program (shown in last
section of for loop) can be written using while loop as:
The $variable-name is compared against the patterns until a match is found. The shell then executes all
the statements up to the two semicolons that are next to each other. The default is *) and its executed if
no match is found. For e.g. write script as follows:
$ sh -v dsh1.sh 4 5
Use -v option to debug complex shell script.
Introduction
Linux contains powerful utility programs. You can use these utility to
Locate system information
For better file management
To organize your data
System administration etc
Following section introduce you to some of the essential utilities as well as expression. While
programming shell you need to use these essential utilities. Some of these utilities (especially sed & awk)
requires understanding of expression. After the quick introduction to utilities, you will learn the
expression.
Sr.No Name
11 Vivek
12 Renuka
13 Prakash
14 Ashish
15 Rani
smark
Sr.No Mark
11 67
12 55
13 96
14 36
15 67
cut utility cuts out selected data from sname file. To select Sr.no. field from sname give command as
follows:
$cut -f1 sname
11
12
13
14
15
Command Explanation
cut Name of cut utility
Using (-f) option, you are specifying the extraction field number. (In this example
-f1
its 1 i.e. first field)
sname File which is used by cut utility and which is use as input for cut utility.
egg order 4
cacke good 10
cheese okay 4
pen good 12
floppy good 5
After crating file issue command
$ awk '/good/ { print $3 }' inventory
10
12
5
awk utility, select each record from file containing the word "good" and performs the action of printing
the third field (Quantity of available goods.). Now try the following and note down its output.
$ awk '/good/ { print $1 " " $3 }' inventory
General Syntax of awk utility:
Syntax:
awk 'pattern action' {file-name}
For $ awk '/good/ { print $3 }' inventory example,
/good/ Is the pattern used for selecting lines from file.
{print This is the action; if pattern found, print on of such action. Here $3 means third record in
$3} selected record. (What $1 and $2 mean?)
inventory File which is used by awk utility which is use as input for awk utility.
Syntax:
sed {expression} {file}
Use of sed utility: sed is used to edit (text transformation) on given stream i.e a file or may be input from
a pipeline.
Hello I am vivek
12333
12333
welcome
to
sai computer academy, a'bad.
what still I remeber that name.
oaky! how are u luser?
what still I remeber that name.
hello world!
cartoons are good
especially toon like tom (cat)
what
the number one song
12221
they love us
I too
After saving file, issue following command,
$ grep "too" demofile
cartoons are good
especially toon like tom (cat)
I too
grep will locate all lines for the "too" pattern and print all (matched) such line on-screen. grep prints too,
as well as cartoons and toon; because grep treat "too" as expression. Expression by grep is read as the
letter t followed by o and so on. So if this expression is found any where on line its printed. grep don't
understand words.
Syntax:
grep "word-to-find" {file-name}
Introduction
In the chpater 5, "Quick Tour of essential utilities", you have seen basic utilities. If you use them with
other tools, these utilities are very useful for data processing or for other works. In rest part of tutorial we
will learn more about patterns, filters, expressions, and off course sed and awk in depth.
Example:
$ ex demofile
The : (colon) is ex prompt where you can type ex text editor command or regular expression. Its time to
open our demofile, use ex as follows:
$ ex demofile
"demofile" [noeol] 20L, 387C
Entering Ex mode. Type "visual" to go to Normal mode.
:
As you can see, you will get : prompt, here you can type ex command, type q and press ENTER key to
exit from ex as shown follows: (remember commands are case sensetive)
:q
vivek@ls vivek]$
After typing the q command you are exit to shell prompt.
.....
...
.....
Okay! I will stop.
NOTE Here 1 is 1st line and $ is the special character of ex which mean last-line character. So 1,$ means
print from 1st line to last-line character (i.e. end of file). Here p stands print.
Deleting lines
Give command
:1, d
I love linux.
NOTE
Here 1 is 1st line and d command indicates deletes (Which deletes the 1st line).
You can even delete range of line by giving command as
:1,5 d
Copying lines
Give command as follows
:1,4 co $
:1,$ p
I love linux.
It is different from all other Os
....
.....
. (DOT) is special command of linux.
Okay! I will stop.
I love linux.
It is different from all other Os
My brother Vikrant also loves linux.
NOTE Here 1,4 means copy 1 to 4 lines; co command stands for copy; $ is end of file. So it mean copy
first four line to end of file. You can delete this line as follows
:18,21 d
Okay! I will stop.
:1,$ p
I love linux.
It is different from all other Os
My brother Vikrant also loves linux.
He currently lerarns linux.
Linux is cooool.
Linux is now 10 years old.
Next year linux will be 11 year old.
Rani my sister never uses Linux
She only loves to play games and nothing else.
Do you know?
. (DOT) is special command of linux.
Quitting the ex
Give command
:q
Finding words
Command like
:g/the/p
It is different from all other Os
My brother Vikrant also loves linux who also loves unix.
Will find word like theater, the, brother, other etc. What if you want to just find the word like "the" ? To
find the word (Let's say Linux) you can give command like
:/\<Linux\>
Linux is cooool.
:g/\<Linux\>/p
Linux is cooool.
Linux is now 10 years old.
Rani my sister never uses Linux
The symbol \< and \> respectively match the empty string at the beginning and end of the word. To find
the line which contain Linux pattern at the beginning give command
:/^Linux
Linux is cooool.
As you know $ is end of line character, the ^ (caret) match beginning of line. To find all occurrence of
pattern "Linux" at the beginning of line give command
:g/^Linux
Linux is cooool.
Linux is now 10 years old.
And if you want to find "Linux" at the end of line then give command
:/Linux $
Rani my sister never uses Linux
Following command will find empty line:
:/^$
To find all blank line give command:
:g/^$
To view entire file without blank line you can use command as follows:
:g/[^/^$]
Hello World.
This is vivek from Poona.
I love linux.
It is different from all other Os
[:digit:] Digit (0 to 9)
[:graph:] Printing character, like print, except that a space character is excluded
[:lower:] Lowercase letter (a to z)
[:print:] Printing character (0x20 to 0x7E)
[:punct:] Punctuation character (ctrl or space)
Space, tab, carriage return, new line, vertical tab, or form feed (0x09
[:space:]
to 0x0D, 0x20)
[:upper:] Uppercase letter (A to Z)
[:xdigit:] Hexadecimal digit (0 to 9, A to F, a to f)
For e.g. To find digit or alphabet (Upper as well as lower) you will write
:/[0-9A-Za-Z]
Instead of writing such command you could easily use predefined classes or range as follows
:/[[:alnum:]]
The . (dot) matches any single character.
For e.g. Type following command
:g/\<.o\>
She only loves to play games and nothing else.
Do you know?
This will include lo(ves), Do, no(thing) etc.
* Matches the zero or more times
For e.g. Type following command
:g/L*
Hello World.
This is vivek from Poona.
....
....
:g/Li*
Linux is cooool.
Linux is now 10 years old.
Rani my sister never uses Linux
:g/c.*and
. (DOT) is special command of linux.
Here first c character is matched, then any single character (.) followed by n number of single character
(1 or 100 times even) and finally ends with and. This can found different word as follows command or
catand etc.
In the regular expression metacharacters such as . (DOT) or * loose their special meaning if we use as \.
or \*. The backslash removes the special meaning of such meatcharacters and you can use them as
ordinary characters. For e.g. If u want to search . (DOT) character at the beginning of line, then you can't