CLICK HERE TO DOWNLOAD PPT ON Shell Programming
Shell Programming Presentation Transcript
1.Shell Programming
2.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).
Pathname of shell be found using the which command.
The shell name is the shell that will execute this script.
E.g: #!/bin/bash
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).
Pathname of shell be found using the which command.
The shell name is the shell that will execute this script.
E.g: #!/bin/bash
3.If no shell is specified in the script file, the default is chosen to be the currently executing shell.
4.Any Unix command can go in a shell script
Commands are executed in order or in the flow determined by control statements.
Different shells have different control structures
The #! line is very important.
We will write shell scripts with the Bourne shell (bash).
Commands are executed in order or in the flow determined by control statements.
Different shells have different control structures
The #! line is very important.
We will write shell scripts with the Bourne shell (bash).
5.A shell script as a standalone is an executable program:
Must use chmod to change the permissions of the script to be executable also.
Can run script explicitly also, by specifying the shell name.
E.g: $ bash myscript
E.g: $ csh myscript
Must use chmod to change the permissions of the script to be executable also.
Can run script explicitly also, by specifying the shell name.
E.g: $ bash myscript
E.g: $ csh myscript
6.Consider the example
$ bash myscript
Invokes the bash shell and then runs the script using it.
Its almost as if we had the line #! /bin/bash in the file myscript.
Similarly with the second example using csh.
myscript need not be an executable as bash is running the script on its behalf.
$ bash myscript
Invokes the bash shell and then runs the script using it.
Its almost as if we had the line #! /bin/bash in the file myscript.
Similarly with the second example using csh.
myscript need not be an executable as bash is running the script on its behalf.
7.Why write shell scripts ?
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 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.
8.Why write shell scripts ?
To automate difficult tasks:
Many commands have subtle and difficult options that you don’t want to figure out or remember every time .
To automate difficult tasks:
Many commands have subtle and difficult options that you don’t want to figure out or remember every time .
9.Simple Example
Assume that I need to execute the following commands once in a while when I run out of disk space:
Assume that I need to execute the following commands once in a while when I run out of disk space:
10.We can put all those commands into a shell script, called myscript.
11.To run the script:
Step 1:
$ chmod u+x myscript
Step 2:
Run the script:
$ ./myscript
Each line of the script is processed in order.
Step 1:
$ chmod u+x myscript
Step 2:
Run the script:
$ ./myscript
Each line of the script is processed in order.
12.Shell variables:
Declared by:
varname=varvalue
To make them an environment variable, we export it.
export varname=varvalue
Declared by:
varname=varvalue
To make them an environment variable, we export it.
export varname=varvalue
13.Assigning the output of a command to a variable:
Using backquotes, we can assign the output of a command to a variable:
Using backquotes, we can assign the output of a command to a variable:
14.Notes on expr
expr supports the following operators:
arithmetic operators: +,-,*,/,%
comparison operators: <, <=, ==, !=, >=, >
boolean/logical operators: &, |
parentheses: (, )
precedence is the same as C, Java
expr supports the following operators:
arithmetic operators: +,-,*,/,%
comparison operators: <, <=, ==, !=, >=, >
boolean/logical operators: &, |
parentheses: (, )
precedence is the same as C, Java
15.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.
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.
16.Control statements
The three most common types of control statements:
conditionals: if/then/else, case, ...
loop statements: while, for, until, do, ...
branch statements: subroutine calls (good programming practice), goto (usage not recommended).
The three most common types of control statements:
conditionals: if/then/else, case, ...
loop statements: while, for, until, do, ...
branch statements: subroutine calls (good programming practice), goto (usage not recommended).
17.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
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
18.Example: Listing all files in a directory.
19.Conditionals
Conditionals are used to “test” something.
In Java or C, they test whether a Boolean variable is true or false.
In a Bourne shell script, the only thing you can test is whether or not a command is “successful”.
Conditionals are used to “test” something.
In Java or C, they test whether a Boolean variable is true or false.
In a Bourne shell script, the only thing you can test is whether or not a command is “successful”.
20.Every well behaved command returns back a return code.
0 if it was successful
Non-zero if it was unsuccessful (actually 1..255)
This is different from C.
0 if it was successful
Non-zero if it was unsuccessful (actually 1..255)
This is different from C.
21.Simple form:
if decision_command_1
then
command_set_1
fi
if decision_command_1
then
command_set_1
fi
22.Importance of having then on the next line:
Each line of a shell script is treated as one command.
then is a command in itself
Even though it is part of the if structure, it is treated separately.
Each line of a shell script is treated as one command.
then is a command in itself
Even though it is part of the if structure, it is treated separately.
23.Using else with if
#! /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
#! /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
24.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
#! /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
25.Using colon in shell scripts
Sometimes, we do not want a statement to do anything.
In that case, use a colon ‘:’
if grep UNIX myfile > /dev/null
then
:
fi
Does not do anything when UNIX is found in myfile .
Sometimes, we do not want a statement to do anything.
In that case, use a colon ‘:’
if grep UNIX myfile > /dev/null
then
:
fi
Does not do anything when UNIX is found in myfile .
26.The test command
Use for checking validity.
Three kinds:
Check on files.
Check on strings.
Check on integers
Use for checking validity.
Three kinds:
Check on files.
Check on strings.
Check on integers
27.Notes on test
Testing on files.
test –f file: does file exist and is not a directory?
test -d file: does file exist and is a directory?
test –x file: does file exist and is executable?
test –s file: does file exist and is longer than 0 bytes?
Testing on files.
test –f file: does file exist and is not a directory?
test -d file: does file exist and is a directory?
test –x file: does file exist and is executable?
test –s file: does file exist and is longer than 0 bytes?
28.Notes on test
Testing on strings.
test –z string: is string of length 0?
test string1 = string2: does string1 equal string2?
test string1 != string2: not equal?
Testing on strings.
test –z string: is string of length 0?
test string1 = string2: does string1 equal string2?
test string1 != string2: not equal?
29.Notes on test
Testing on integers.
test int1 –eq int2: is int1 equal to int2 ?
test int1 –ne int2: is int1 not equal to int2 ?
test int1 –lt int2: is int1 less than to int2 ?
test int1 –gt int2: is int1 greater than to int2 ?
test int1 –le int2: is int1 less than or equal to int2 ?
test int1 –ge int2: is int1 greater than or equal to int2 ?
Testing on integers.
test int1 –eq int2: is int1 equal to int2 ?
test int1 –ne int2: is int1 not equal to int2 ?
test int1 –lt int2: is int1 less than to int2 ?
test int1 –gt int2: is int1 greater than to int2 ?
test int1 –le int2: is int1 less than or equal to int2 ?
test int1 –ge int2: is int1 greater than or equal to int2 ?
30.The test command has an alias ‘[]’.
Each bracket must be surrounded by spaces
31.The while loop
While loops repeat statements as long as the next Unix command is successful.
Works similar to the while loop in C.
Each bracket must be surrounded by spaces
31.The while loop
While loops repeat statements as long as the next Unix command is successful.
Works similar to the while loop in C.
31.Example
32. The until loop
Until loops repeat statements until the next Unix command is successful.
Works similar to the do-while loop in C.
Until loops repeat statements until the next Unix command is successful.
Works similar to the do-while loop in C.
33.Next Lecture
Unix Shell programming:
Command line arguments.
Reading input in shell programs.
Revision:
Storage management.
Examples.
Unix Shell programming:
Command line arguments.
Reading input in shell programs.
Revision:
Storage management.
Examples.
0 comments