Session 9 : Operating System Programming Concepts : Lab 9 - Shell Programming - BASH


Lab 9 : Shell Programming – Bash 
 

Shell Syntax – File Conditional
# file conditional

if [ -d file ] # true if the file is a directory
elif [ -e file ] # true if the file exists
elif [ -f file ] # true if the file is a regular file
elif [ -g file ] # true if set-group-id is set on file
elif [ -r file ] # true if file is readable
elif [ -s file ] # true if the file has non-zero size
elif [ -u file ] # true if the set-user-id is set on file
elif [ -w file ] # true if file is writable
elif [ -x file ] # true if file is executable
else
echo "don't know what is the kind of file"

Example:
#!/bin/bash

if [ $# -ne 1 ]
then
echo "this file: $0 is used now "
exit 1
fi

if [ ! -e $1 ]
then
echo "the file doesn't exist"
else
echo "the file exist"
if [ -f $1 ]
then
echo "the file is regualar file"
fi

if [ -d $1 ]
then
echo "the file is a directory "
fi
fi


Shell Syntax – Case
case variable in
pattern ) statements ;;
pattern ) statements ;;
esac

Example:
#!/bin/bash

echo "Is it day or night? Answer yes or no:"
read time
case "$time" in
yes) echo "Now its day.";;
no) echo "Now its night.";;
*) echo "answer is not recognized";;
esac

Shell Syntax-Command Execution
/> usually we put the result of a command execution in a variable
/> we do this by using $(command)
Example :
echo “The current users are $(who)”


Shell Syntax-Arithmetic Expansion
/> expr` ` command is quite slow to execute
/> so we use $(()) expansion which is faster
i=$(($i+1)) # i++


Example: Reading file line by line
#checking parameters

if [ $# -ne 1 ]
then
echo "wrong parameters passed"
exit 1
fi

while read line
do
echo $line
echo "the length of the line is:" ${#line}
done < $1




Example : Reading file word by word
#!/bin/bash

#checking parameters

if [ $# -ne 1 ]
then
echo "wrong number of parameters"
exit 1
fi

for w in `cat $1`
do
echo $w
done




Learning goals: in this laboratory activity you will improve your knowledge about synchronization
by means of pipes. You will also learn how to write complex Bash scripts .

Exercise 1
Implement a C program simulating the evaluation of an exam. The evaluation is carried out by a
teacher and an assistant, correcting the exam and exchanging information to give the final mark.
More in detail, the procedure must be implemented by a program reading the name of two files as
command line parameters and generating two processes, the teacher and the assistant processes.
The teacher process corrects the first part of the exam reading data from the first data file. The file
includes a student for each line in the following format:
Strudent Id” “Surname” “Name” “First part of the mark”
Suppose the maximum size for the first three fields is 20 characters while the fourth field is a
number between 0 and 15.
The teacher and the assistant are synchronized as follow:
1. The teacher process sends to the assistant process the surname and name of the student.
2. The assistant process receives the surname and the name of the students and then simulates
correction of the second part of the exam, reading the second part mark from keyboard.
Then it sends the value read through the keyboard to the teacher process.
3. The teacher process, sums up the two marks and writes data to the second file. This second
file has the same format of the first input file.
Then the processes continue with the following students. Use two pipes to transfer data to the
assistant and to the teacher process. Be careful to terminate the assistant process when the
teacher process terminates.

Exercise 2
Write a Bash script reading two command line parameters, a filename and a string.
The file whose name is passed as the first command line parameter includes a number for
each line.
The string will be the extentions of the output files.
The script has to:
1. Read the file(filename) line by line;
2. Generate for each line the corresponding number of Fibonacci items; If for example the
number is 5, it has to generate the first 5 values of the Fibonacci series;
3. Generate, for each line, a file including the Fibonacci series;The output filename is the result of catenation of the name of the input file and the number read on
the corresponding line;
The suffix is the same as the second command line parameter;
So for example having a input file named fibonacci.txt as follows:
5
10
12
And calling:
./fibonacci.sh fibonacci.txt fib
It would generate three files, one for each line of the input file:
fibonacci5.fib 1 1 2 3 5
fibonacci10.fib 1 1 2 3 5 8 13 21 34 55
fibonacci12.fib 1 1 2 3 5 8 13 21 34 55 89 144



Exercise 3
Write a Bash script that compares the contents of two directories(passed as parameters) (including
both files and sub-directories). It must write to stdout the list of files and sub-directories that don't
belong to both directories passed as parameter.

Exercise 4
A C program named sum.c receives the name of two files as a command line parameter.
The first file contains a list of integer numbers, two for each row.
The second file must contain the sum line by line of the input values.
Once written the C source code for the previous task write a bash script sum.sh which
receives three command line parameters (str1, str2 and str3) and calls the sum.exe executable for
each file that is included in directory named str1, has the same extentions specified by str2 and does
not exist a corresponding file with the same name and extension str3.
For example calling:
sum.bash . in out
searches the current directory for all the files ending in “.in” for which a file with the same name
and ending in “.out” does not exist. If this condition is verified then the executable is called passing
the two files as parameters(sum.exe test1.in test1.out)

Summary
At the end of this laboratory activity you should have understood how to synchronize two processes
by means of a pipe. You should also have improved your understanding about writing bash shell
scripts.







Comments

Popular Posts