LAB1.
Basic Unix Commands, Simple Shell scripts
a)Illustrate the usage of Unix commands and vi editor concept.
b)Implement a shell program to find and display largest and smallest of three numbers
#!/bin/bash
echo "Please enter the three numbers"
read x
read y
read z
if [ "$x" -ge "$y" ] && [ "$x" -ge "$z" ]
then
echo "$x is greatest"
elif [ "$z" -ge "$y" ] && [ "$z" -ge "$x" ]
then
echo "$z is greatest"
else
echo "$y is greatest"
fi
if [ "$x" -le "$y" ] && [ "$x" -le "$z" ]
then
echo "$x is smallest"
elif [ "$z" -le "$y" ] && [ "$z" -le "$x" ]
then
echo "$z is smallest"
else
echo "$y is smallest"
fi
1
LAB2-Simple Shell scripts/Command Substitution
1. Find the number n is divisible by m or not using shell script. Where m and n are
supplied as command line argument or read from keyboard interactively
# (i) using read (ii) using cmd line arguments
# (i)
echo "enter 2 numbers: "
read m
read n
y=$(expr $m % $n)
if [ $y -eq 0 ]
then
echo "$m is divisible by $n"
else
echo "$m is not divisible by $n"
fi
# (ii)
y=$(expr $1 % $2)
if [ $y -eq 0 ]
then
echo "$1 is divisible by $2"
else
echo "$1 is not divisible by $2"
fi
2
2. Plan and implement a shell program to search a pattern in a file that will take
both pattern and file name from the command line arguments.
#!/bin/sh
#[Link]
if [ $# -eq 0 ]; then
echo "no arguments"
else
grep "$1" $2
fi
#!/bin/sh
#[Link]
# $# refers to no of cmd line args
if [ $# -eq 0 ]; then
echo "Enter the pattern"
read pat
echo "Enter the filename"
read fnm
else
pat=$1
fnm=$2
fi
sh [Link] $pat $fnm
3
LAB3 - File attributes/expr command demonstration
1. Design a shell program that takes two file names, checks the permissions for these
files are identical and if they are identical, output the common permissions;
otherwise output each file name followed by its permissions.
display_perm() {
r=$(ls -l $1 | cut -c 2)
w=$(ls -l $1 | cut -c 3)
x=$(ls -l $1 | cut -c 4)
echo "\nowner permissions:"
if [ "$r" = "r" ]; then
echo "READ"
else
echo "NO READ"
fi
if [ "$w" = "w" ]; then
echo "WRITE"
else
echo "NO WRITE"
fi
if [ "$x" = "x" ]; then
echo "EXECUTE"
else
echo "NO EXECUTE"
fi
r=$(ls -l $1 | cut -c 5)
w=$(ls -l $1 | cut -c 6)
x=$(ls -l $1 | cut -c 7)
echo "\ngroup permissions:"
if [ "$r" = "r" ]; then
echo "READ"
else
echo "NO READ"
fi
if [ "$w" = "w" ]; then
4
echo "WRITE"
else
echo "NO WRITE"
fi
if [ "$x" = "x" ]; then
echo "EXECUTE"
else
echo "NO EXECUTE"
fi
r=$(ls -l $1 | cut -c 8)
w=$(ls -l $1 | cut -c 9)
x=$(ls -l $1 | cut -c 10)
echo "\nother permissions:"
if [ "$r" = "r" ]; then
echo "READ"
else
echo "NO READ"
fi
if [ "$w" = "w" ]; then
echo "WRITE"
else
echo "NO WRITE"
fi
if [ "$x" = "x" ]; then
echo "EXECUTE"
else
echo "NO EXECUTE"
fi
echo "Enter 2 file names"
read file1
read file2
if [ -e $file1 -a -e $file2 ]; then
p1=$(ls -l $file1 | cut -c 2-10)
p2=$(ls -l $file2 | cut -c 2-10)
if [ "$p1" = "$p2" ]; then
echo "Same permission"
display_perm $file1
else
echo "\n Different permissions"
echo "\n **first file permissions**"
display_perm $file1
echo "\n **second file permissions**"
5
display_perm $file2
fi
else
echo "File not Found , does not exist"
fi
exit
2. Implement a shell program to display the length of the name and also display first
three characters and last three characters in the name in two different lines if the
name contains at least 6 characters.
LAB 4. Arithmetic operators/Command Substitution
1. Write a shell program to implement simple calculator operations.
#!\bin\sh
echo "options are: \n+:add\n-:subtract\n*:multiply\n/:divide\n"
echo "enter 2 numbers"
read a
read b
echo "\n enter your choice"
read ch
if [ "$b" -eq 0 ] && [ "$ch" = "/" ]; then
echo "error-division by 0"
else
case $ch in
'+')
y=$(expr $a + $b)
echo "sum = $y";;
'-')
y=$(expr $a - $b)
echo "diff = $y";;
'*')
y=$(expr $a \* $b)
echo "product = $y";;
'/')
y=$(expr $a / $b)
echo "division = $y";;
*)
echo "invalid choice";;
esac
fi
6
2. Design a Shell Program that takes the any number of arguments and print them in
same order and in reverse order with suitable messages.
echo "program name : $0"
if [ $# -eq 0 ]; then
exit
fi
echo "no of arguments : $#"
echo "the input arguments are"
num=1
for i in "$@";
do
echo "arg$num is $i"
num=$(expr $num + 1)
done
echo "arguments are in reverse order"
num=$#
while [ $num -ne 0 ];
do
eval echo "arg$num is \$$num"
num=$(expr $num - 1)
done
7
LAB 5. String handling operations/Command Substitution
1. For the given path names (E.g., a/b,a/b/c), design a shell script to create all
the components in that path names as directories.
#!/bin/sh
if [ $# -ne 1 ]; then
echo "no arguments"
exit
fi
curdir=$(pwd)
for dir in $(echo $1 | tr '/' ' '); do
if [ -d $dir ]; then
echo "$dir exists under $(pwd)"
cd $dir
else
mkdir $dir
echo "$dir created under $(pwd)"
cd $dir
fi
done
cd $curdir
2. Develop a shell script that performs following string handling operations
i) Calculate the length of the string
ii) locate a position of a character in a string
iii) extract last three characters from string
echo "Enter the string"
read str
echo "Enter your option"
read opt
case $opt in
'1')
if [ -z "$str" ]
then
echo "null string"
else
z=`expr "$str" : '.*'`
echo "string length is $z"
fi;;
'2')
echo "Enter the character to be searched:"
read ch
z=`expr "$str" : '[^'$ch']*'$ch''`
echo "Character is present at $z position";;
'3')
echo "first 3 characters"
z=`expr "$str" : '\(...\).*'`
echo "$z";;
'4')
echo "last 3 characters"
x=`expr "$str" : '.*\(...\)'`
echo "$x" ;;
*)
echo "Invalid Choice";;
esac
8
LAB 6. Command Substitution
1. For every filename, check whether file exists in the current directory or not and
then convert its name to uppercase only if a file with new name doesn’t exist using
shell script.
#theory
#case 1: [Link] doesnt exist so cant convert to [Link]
#case 2: [Link] exist [Link] will be created
#case 3: [Link] exists [Link] also exists so a will not be converted
#"$@" it stores cmdline arguments as single string
#"$#" it stores number of cmdline arguments
# tr stands for translate
for file in "$@";do
if [ -f $file ];then
ufile=`echo $file | tr '[a-z]' '[A-Z]'`
if [ -f $ufile ];then
echo "$ufile also exists"
else
mv $file $ufile
fi
else
echo "$file doesn't exist"
fi
done
OUTPUT:
student@nmamit:~$ ls
[Link]
student@nmamit:~$ touch [Link] [Link] [Link]
student@nmamit:~$ sh [Link] [Link] [Link] [Link]
[Link] also exists
[Link] doesn't exist
student@nmamit:~$ ls
[Link] [Link] [Link] [Link]
10)
#!/usr/bin/perl
print "string:";
$a=<STDIN>;
print "number of times string to be displayed:";
chop($b=<STDIN>);
$c=$a x $b;
print "result is:\n$c";
OUTPUT:
$perl [Link]
string: Nitte
number of times string to be displayed:2
result is:
Nitte
Nitte
9
[Link]
11)
#implent a PERL"Practical Extraction and Reporting Language() script that takes file
as argument checks whether file exists and files binary if file is binary.
foreach $f (@ARGV)
{
if(-e $f)
{
if(-B $f)
{
print "$f is a BINARY FILE\n";
}
else
{
print "$f is NOT a Binary File\n";
}
}
else
{
print "$f doesn't Exist\n";
}
}
OUTPUT:
~$ perl [Link] [Link]
[Link] is NOT A Binary File
~$ perl [Link] [Link]
[Link] is a BINARY FILE
~$ perl [Link] [Link]
[Link] doesn’t Exist
PERL File Test Operators
The file test operator -e accepts a filename or filehandle as an argument.
The following list illustrates the most important Perl file test operators:
• -r: check if the file is readable
• -w: check if the file is writable
• -x: check if the file is executable
• -o: check if the file is owned by effective uid.
• -R: check if file is readable
• -W: check if file is writable
• -X: check if file is executable
• -O: check if the file is owned by real uid.
• -e: check if the file exists.
• -z: check if the file is empty.
• -s: check if the file has nonzero size (returns size in bytes).
• -f: check if the file is a plain file.
• -d: check if the file is a directory.
• -l: check if the file is a symbolic link .
• -p: check if the file is a named pipe (FIFO): or Filehandle is a pipe.
• -S: check if the file is a socket.
10
• -b: check if the file is a block special file.
• -c: check if the file is a character special file.
• -t: check if the file handle is opened to a tty.
• -u: check if the file has setuid bit set.
• -g: check if the file has setgid bit set.
• -k: check if the file has sticky bit set.
• -T: check if the file is an ASCII text file (heuristic guess).
• -B: check if the file is a “binary” file (opposite of -T).
PRACTICE ONLY DONT WRITE IN RECORD{
#1) check whether number is even or odd
#!/usr/bin/perl
print "enter number : ";
$num=<STDIN>;
chomp($num);
if($num%2==0){
print "$num is even\n";
}
else{
print "$num is odd\n";
}
#2)squareroot of given number
$s=sqrt($num);
print "squareroot of $num is $s\n";
#3)conversion of decimal to binary
$binary = sprintf("%b", $num);
print "Binary of $num is $binary\n";
#4)conversion of binary
$decimal= sprintf("%d", $num);
print "Decimal of $binary is $decimal\n";
}
3. PERL script that echoes its command line arguments, one per line after translating
all lower case letters to upper case.
#! /usr/bin/perl
#OUTPUT:
#perl [Link]
#you have not entered the arguments
#perl [Link] student sss
#STUDENT
#SSS
die("you have not entered the arguments\n")if(@ARGV==0);
foreach $arg(@ARGV)
{
$arg=~ tr/a-z/A-Z/;
printf("$arg\n");
}
4. PERL program to find the sum of digits of an unsigned number passed through
argument.
#!/usr/bin/perl
#OUTPUT:
#perl [Link]
#12345
#sum of digits of 12345 is 15
11
foreach $num (@ARGV)
{
$original_no=$num;
until($num==0)
{
$digit=$num%10;
$sum=$sum+$digit;
$num=int($num/10);
}
}
print("sum of digits of $original_no is $sum");
AWK 3 PROGRAMS
1)
2)
12
3)
13
C PROGRAMS 2
1)
14
2)
15
16
2. Execution of exercise Shell scripts
LAB 7. Process
1. C program to do the following: Using fork( ) create a child process. The child
process prints its own process-id and id of its parent and then exits. The parent
process waits for its child to finish (by executing the wait( )) and prints its own
process-id and the id of its child process and then exits.
2. C program that creates a child process to read commands from the standard
input and execute them (a minimal implementation of a shell - like program). You can
assume that no arguments will be passed to the commands to be executed.
LAB 8. Signal :
1. Write a C Program to register signal handler for SIGINT and when it receives the
signal, the program should print some information about the origin of the signal.
2. Write a C program which illustrates sending signal from one process to another by
using kill API. Also check if the program has permission to send the signal or not.
LAB 9. Write a C Program to register signal handler for SIGSTOP.
LAB 10. AWK scripts
Write a C Program to handle user defined signals.
LAB 11. AWK scripts
Write a C Program to create a Daemon process.
LAB 12. Miscellaneous
Exercise of shell programs, C programs on processes and signals
17