Transcript Document
C151 Multi-User Operating Systems
Scripts and Shell Programming
Shell Script
A file containing list of commands
Usually it has the extension .sh.
Each line (command) is interpreted and executed by a
shell program (interpreter).
We will discuss bash shell.
-------------------------------------------------------Shell interpreter
Shell script
Steps to Run a Shell Script
Create a file (say file_name.sh)using your favorite editor
Make the file executable
chmod 700 file_name.sh
Run the script
file_name.sh
First Line
The first line should look like this
#!/bin/bash
This line tells:
Which shell (interpreter) should be used to execute the script: bash
Where to find the shell (interpreter): /bin/bash
Comments
Comments: any line starting with a # will be considered as comments
Exception: The very first one
Example:
#!/bin/bash
#This is my first shell script
pwd
ls –l
Combining Commands
Several commands on the same line: separated
with ;
Example:
cd .. ; make ; cd src
Output
Use echo command
#!/bin/bash
#This is my first shell script
echo “This is my current working directory”
pwd
echo “These are the files in my current working directory”
ls –l
See This Script
test.sh
#!/bin/bash
echo $0
echo $1
echo $2
echo $#
echo $*
See This Script
test.sh
#!/bin/bash
echo $0
echo $1
echo $2
echo $#
echo $*
See this script
test.sh
#!/bin/bash
echo $0
echo $1
echo $2
echo $#
echo $*
Command Line Parameters
The command parameters are arguments that can be given in the
command line when we run the script.
$0 is the name of the script file.
$1 is the first argument in the command line, $2 is the second, etc.
$# is the number of arguments.
$* contains all the arguments.
Variables
The name of a variable is a placeholder for its value, the data
it holds.
If v is the name of a variable, then $v is a reference to its
value
What result will be displayed for the following script?
#!/bin/bash
v=23
echo v
echo $v
Variables
The name of a variable is a placeholder for its value, the data
it holds.
If v is the name of a variable, then $v is a reference to its
value
What result will be displayed for the following script?
#!/bin/bash
v=23
echo v
echo $v
v
23
Assignment operator =
Rule:
No space permitted on either side of = sign.
Variable Rule
Assign a value to name of a variable
Read variable value through reference of the variable
Example:
Name of the variable: num
Value of the variable $num
num
23
Task 1
Create two variables: a, b
Assign a with value 100
Assign b with value 200
Disply the values of a and b
Reassign b with the value of a
Display the values of a and b
Script for Task 1
#!/bin/bash
a=100
b=200
echo $a
echo $b
b=$a
echo $a
echo $b
Script for Task 1
#!/bin/bash
a=100
b=200
echo $a
echo $b
b=$a
echo $a
echo $b
100
200
100
100
Number Operations
All computations in a shell script are done with integers.
A number operation must be placed within the following
syntax:
$[expression]
For example
a=$[2+3]
echo $a
(5 will be displayed)
Number Operations
v1=100
echo $v1
v2=$v1/2
echo $v2
(100)
(?)
Number Operation
v1=100
echo $v1
v2=$v1/2
echo $v2
(100)
(100/2)
Number Operation
v1=100
echo $v1
v2=$[$v1/2]
echo $v2
(100)
(50)
Arithmetic Operators
+ plus
- minus
* multiplication
/ division
** exponentiation
% modulo
Task 2
Create two variables a and b, and assign each with a value.
Calculate value a plus value b and assign it to variable c
Calculate value a minus value b and assign it to variable d
Calculate value c multiply value d and assign it to variable e
Calculate value e divided by 2 and assign it to variable f
Display value of f
Script for Task 2
#!/bin/bash
a=5
b=3
c=$[$a+$b]
d=$[$a-$b]
e=$[$c*$d]
f=$[$e/2]
echo $f
Script for Task 2
#!/bin/bash
a=5
b=3
c=$[$a+$b]
d=$[$a-$b]
e=$[$c*$d]
f=$[$e/2]
echo $f
8
Input
Use read command
read variable_name
Input Example
#!/bin/bash
echo “Enter your name:”
read name
echo “Enter your age:”
read age
echo “The age of $name is $age”
Input Example
#!/bin/bash
echo “Enter your name:”
read name
echo “Enter your age:”
read age
echo “The age of $name is $age”
Integer Comparison
Test if two integer variables are equal or not
if [ $a -eq $b ]
-Note: must have a space before and after
brackets and operators
Integer Comparison
-eq is equal to
-ne is not equal to
-gt is greater than
-ge is greater than or equal to
-lt is less than
-le is less than or equal to
If Statement
if [ condition ]
then
command(s)
else
command(s)
fi
if [ condition ]
then
command(s)
elif [ condition ]
then
command(s)
else
command(s)
fi
You can have more elif [] then statements
Task 3
Ask user to enter two integer numbers and display which
number is greater
Script for Task 3
#!/bin/bash
echo “Enter number 1:”
read a
echo “Enter number 2:”
read b
if [ $a -ge $b ]
then
echo “$a is greater than or equal to $b”
else
echo “$a is less than $b”
fi
String Comparison
== is equal to
if [ “$a” == “$b” ]
!= is not equal to
if [ “$a” != “$b” ]
Note:
1. Must have a space before and after [ and ].
2. Must have spaces before and after operators == and !=
3. Must use double quote for string variables
4. You can also use double quote for number variables.
But, it is not required.
Task 4
Ask user to enter two strings and display whether they are
equal or not
Script for Task 4
#!/bin/bash
echo “Enter string 1:”
read a
echo “Enter string 2:”
read b
if [ “$a” == “$b” ]
then
echo "$a is same as $b"
else
echo "$a is different from $b"
fi
Logical Operators
&&AND
|| OR
Example:
#!/bin/bash
a=100
b=200
if ([ $a -gt 0 ] && [ $b -gt 0])
then
echo "both $a and $b are positive"
fi
While Loop
while [ condition ]
do
command(s)...
done
Note: keep working in loop as long as condition is
true (terminate if condition is false)
Task 5
Ask the user to continually enter an integer number and
show the summation of all the numbers the user has entered
so far.
The program terminates when user enters word “end”
No input error checking is required
Scripts for Task 5
#!/bin/bash
sum=0
echo "Enter a number(end to exit)"
read n
while [ "$n" != "end" ]
do
sum=$[$sum+$n]
echo “The sum of the numbers you entered so far is $sum"
echo "Enter a number(end to exit)"
read n
done
Until Loop
until [ condition ]
do
command(s)...
done
Keep in loop as long as condition is false
(terminate when condition is true)
Condition is opposite to while loop
Scripts for Task 5 (using until loop)
#!/bin/bash
sum=0
echo "Enter a number(end to exit)"
read n
until [ "$n" == "end" ]
do
sum=$[$sum+$n]
echo “The sum of the numbers you entered so far is $sum"
echo "Enter a number(end to exit)"
read n
done
A Note about Conditions in
Brackets
Always keep spaces between the brackets and the
actual check/comparison
Always keep spaces between comparison
operators and variable (data)
if [ $a -lt 0 ]
while [ $a –gt $b ]
until [ “$a” == “$b” ]
For Loop
for loop is used to extract items from a list
for arg in $list_variable
do
command(s)...
done
For Loop Example
#!/bin/bash
fruits=“apples oranges pears bananas”
for fruit in $fruits #fruits is considered as a list (items are separated by spaces)
do
echo $fruit # Each fruit on a separate line.
done
• Result
apples
oranges
pears
bananas
For Loop Example
#!/bin/bash
fruits=“apples oranges pears bananas”
for fruit in “$fruits” #fruits is considered as a single item
do
echo $fruit
done
Result:
apples oranges pears bananas
When to use double quote on
string variable?
Should use double quote:
Single value string (no need to separate words)
Should not use double quote:
String is a list of word and we need to separate words
from the list
Command Substitution
Assigns the output of a command to a variable
variable_name=$(command)
Example
#!/bin/bash
cw=$(pwd)
echo cw
Redirect Output to Files
We can redirect the output of Shell Script from Screen to a
File
echo …….. >> file_name
Task 6
Write a shell script so that it can output the following
information to a file called test.txt
Current working directory
Number of files in current working directory
Full path of each file in current working directory
Script for Task 6
#!/bin/bash
cw=$(pwd)
files=$(ls)
nf=$(ls | wc -w)
echo "Current working directory is $cw" >> test.txt
echo "There are $nf file(s), they are listed below" >> test.txt
for file in $files
do
echo $cw/$file >> test.txt
done
Reading Assignment
Chapters 8, 9, and 10