0% found this document useful (0 votes)
29 views

Unix-Unit 2

This document discusses shell scripting in Unix/Linux systems. It covers: 1) What shell scripts are and how they are used to store sequences of commands and automate tasks. Shell scripts begin with #! and specify an interpreter. 2) Common uses of shell scripts include avoiding repetition of tasks and automating difficult commands. 3) Control structures for shell scripts including conditionals like if/then/else and loops like for to control script flow. 4) Additional concepts covered include variables, output redirection, testing files/strings/integers, and more. Shell scripts allow automation of common Linux/Unix tasks through programming in the shell.

Uploaded by

Pavan Prince
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Unix-Unit 2

This document discusses shell scripting in Unix/Linux systems. It covers: 1) What shell scripts are and how they are used to store sequences of commands and automate tasks. Shell scripts begin with #! and specify an interpreter. 2) Common uses of shell scripts include avoiding repetition of tasks and automating difficult commands. 3) Control structures for shell scripts including conditionals like if/then/else and loops like for to control script flow. 4) Additional concepts covered include variables, output redirection, testing files/strings/integers, and more. Shell scripts allow automation of common Linux/Unix tasks through programming in the shell.

Uploaded by

Pavan Prince
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

UNIX Shell Programming

Unix Shell programming:


o Shell scripts.
▪ Definition.
▪ Uses of shell scripts.
▪ Writing shell scripts.
❖ Shell scripts
• A shell script is a text file with Unix commands in it.
• Shell scripts usually begin with a #! and a shell name (complete pathname of shell).
o Pathname of shell be found using the which command.
o The shell name is the shell that will execute this script.
▪ E.g: #!/bin/bash

If no shell is specified in the script file, the default is chosen to be the currently executing shell.

Any Unix command can go in a shell script


o Commands are executed in order or in the flow determined by control statements.
• Different shells have different control structures
o The #! line is very important.
o We will write shell scripts with the Bourne shell (bash).
A shell script as a standalone is an executable program:
o Must use chmod to change the permissions of the script to be executable also.
• Can run script explicitly also, by specifying the shell name.
o E.g: $ bash myscript
o E.g: $ csh myscript

Consider the example


o $ bash myscript
o Invokes the bash shell and then runs the script using it.
o It’s almost as if we had the line #! /bin/bash in the file myscript.
o Similarly with the second example using csh.
o myscript need not be an executable as bash is running the script on its behalf.

Why write shell scripts?


o To avoid repetition:
▪ If you do a sequence of steps with standard Unix commands over and
over, why not do it all with just one command?
▪ Or in other words, store all these commands in a file and execute them one
by one.

To automate difficult tasks:


▪ Many commands have subtle and difficult options that you don’t want to
figure out or remember every time .

1
Simple Example
• Assume that I need to execute the following commands once in a while when I run out of
disk space:
rm -rf $HOME/.netscape/cache
rm -f $HOME/.netscape/his*
rm -f $HOME/.netscape/cookies
rm -f $HOME/.netscape/lock
rm -f $HOME/.netscape/.nfs*
rm -f $HOME/.pine-debug*
rm -fr $HOME/nsmail

We can put all those commands into a shell script, called myscript.
$ cat myscript
#! /bin/bash
rm -rf $HOME/.netscape/cache
rm -f $HOME/.netscape/his*
rm -f $HOME/.netscape/cookies
rm -f $HOME/.netscape/lock
rm -f $HOME/.netscape/.nfs*
rm -f $HOME/.pine-debug*
rm -fr $HOME/nsmail

To run the script:


o Step 1:
▪ $ chmod u+x myscript
o Step 2:
▪ Run the script:
▪ $ ./myscript
• Each line of the script is processed in order.

Shell variables:

o Declared by:
o varname=varvalue
o To make them an environment variable, we export it.
o export varname=varvalue

Assigning the output of a command to a variable:


o Using backquotes, we can assign the output of a command to a variable:
#! /bin/bash
filelist=`ls`
echo $filelist

2
Example:
$ ls
a b c html/
$ filelist=`ls`
$ echo $filelist
a b c html/

The expr command:

o Calculates the value of an expression.

o E.g:
$ value=`expr 1 + 2`
$ echo $value
3
Notes on expr :

• Why do we need the expr command ???



o E.g:
$ file=1+2
$ echo $file
1+2

NOTE: 1+2 is copied as it is into val and not the result of the expression, to get the result, we
need expr.

Variables as arguments:
NOTE: count is replaced with its value by the shell!
$ count=5
$ count=`expr $count + 1`
$ echo $count
6

• expr supports the following operators:


o arithmetic operators: +,-,*,/,%
o comparison operators: <, <=, ==, !=, >=, >
o boolean/logical operators: &, |
o parentheses: (, )
o precedence is the same as C, Java

3
Control statements

• Without control statements, execution within a shell scripts flows from one statement to
the next in succession.
• Control statements control the flow of execution in a programming language.
• The three most common types of control statements:
o conditionals: if/then/else, case, ...
o loop statements: while, for, until, do, ...
o branch statements: subroutine calls (good programming practice), goto (usage not
recommended).
for loops
• for loops allow the repetition of a command for a specific set of values.
• Syntax:
for var in value1 value2 ...
do
command_set
done
• command_set is executed with each value of var (value1, value2, ...) in sequence

Notes on for
• Example: Listing all files in a directory.
#! /bin/bash
for i in *
do
echo $i
done

NOTE: * is a wild card that stands for all files in the current directory, and for will go through
each value in *, which is all the files and $i has the filename.

Example output:
$ chmod u+x listfiles
$ ./listfiles
a
b
c
html
listfiles

4
Conditionals

• Conditionals are used to “test” something.


o In Java or C, they test whether a Boolean variable is true or false.
o In a Bourne shell script, the only thing you can test is whether or not a command
is “successful”.
• Every well behaved command returns back a return code.
o 0 if it was successful
o Non-zero if it was unsuccessful (actually 1..255)
o This is different from C.
The if command
• Simple form:
if decision_command_1
then
command_set_1
fi
• Importance of having then on the next line:
o Each line of a shell script is treated as one command.
o then is a command in itself
▪ Even though it is part of the if structure, it is treated separately.

Example
if grep unix myfile >/dev/null
then
echo "It's there"
fi
grep returns 0 if it finds something
returns non-zero otherwise
redirect to /dev/null so that
"intermediate" results do not get
printed

Using else with if


Example:
#! /bin/bash
if grep "UNIX" myfile >/dev/null
then
echo UNIX occurs in myfile
else
echo No!
echo UNIX does not occur in myfile
fi

5
Using elif with if

#! /bin/bash
if grep "UNIX" myfile >/dev/null
then
echo UNIX occurs in myfile
elif grep “DOS” myfile > /dev/null
then
echo DOS appears in myfile not UNIX
else
echo nobody is here in myfile
fi

Using colon in shell scripts

• Sometimes, we do not want a statement to do anything.


o In that case, use a colon ‘:’
▪ if grep UNIX myfile > /dev/null
▪ then
▪ :
▪ fi
▪ Does not do anything when UNIX is found in myfile .

The test command

• Use for checking validity.


• Three kinds:
o Check on files.
o Check on strings.
o Check on integers
• Testing on files.
o test –f file: does file exist and is not a directory?
o test -d file: does file exist and is a directory?
o test –x file: does file exist and is executable?
o test –s file: does file exist and is longer than 0 bytes?

Example
#!/bin/bash
count=0
for i in *; do
if test –x $i
then
count=`expr $count + 1`
fi
done
echo Total of $count files executable

6
NOTE: expr $count + 1 serves the purpose of count++
• Testing on strings.
o test –z string: is string of length 0?
o test string1 = string2: does string1 equal string2?
o test string1 != string2: not equal?
Example
#! /bin/bash
if test -z $REMOTEHOST
then
:
else
DISPLAY="$REMOTEHOST:0“
export DISPLAY
fi

NOTE: This example tests to see if the value of REMOTEHOST is a string of length > 0 or not,
and then sets the DISPLAY to the appropriate value.
• Testing on integers.
o test int1 –eq int2: is int1 equal to int2 ?
o test int1 –ne int2: is int1 not equal to int2 ?
o test int1 –lt int2: is int1 less than to int2 ?
o test int1 –gt int2: is int1 greater than to int2 ?
o test int1 –le int2: is int1 less than or equal to int2 ?
o test int1 –ge int2: is int1 greater than or equal to int2 ?

Example
#!/bin/bash
smallest=10000
for i in 5 8 19 8 7 3
do
if test $i -lt $smallest
then
smallest=$i
fi
done
echo $smallest
NOTE: This program calculates the smallest among the numbers 5, 8, 19, 8, 3.
• The test command has an alias ‘[]’.
o Each bracket must be surrounded by spaces

#!/bin/bash
smallest=10000
for i in 5 8 19 8 7 3
do
if [ $i -lt $smallest ]
then

7
smallest=$i
fi
done
echo $smallest

The while loop


• While loops repeat statements as long as the next Unix command is successful.
• Works similar to the while loop in C.
Example
#! /bin/bash
i=1
sum=0
while [ $i -le 100 ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo The sum is $sum.

NOTE: The value of i is tested in the while to see if it is less than or equal to 100.

The until loop


• Until loops repeat statements until the next Unix command is successful.
• Works similar to the do-while loop in C.

Example
#! /bin/bash
x=1
until [ $x -gt 3 ]
do
echo x = $x
x=`expr $x + 1`
done
NOTE: The value of x is tested in the until to see if it is greater than 3.

You might also like