SPP
SPP
sh
-------------------------------------------------------------------------------
pro4.sh
pro5.sh
-------------------------------------------------------------------------------
pro6.sh
:'Write shell script that accept filename and displays last modification time if
file exists, otherwise display appropriate message.'
clear
echo "enter file name"
read fn
if [ -e $fn ]
then
echo "last modification time is";ls -l $fn | cut -d " " -f 7
else
echo "file does not exists"
fi
-------------------------------------------------------------------------------
pro7.sh
:'Write a shell script to display the login names that begin with "s".'
clear
c="u"
for i in `who | cut -d " " -f 1`
do
x=`echo $i | cut -c 1`
if [ $x = $c ]
then
echo $i
fi
done
-------------------------------------------------------------------------------
pro8.sh
:'Write a shell script to remove the zero sized file from the current directory'
cnt=0
clear
for i in `ls`
do
if [ ! -s $i ]
then
rm $i
echo "$i is removed"
cnt=`expr $cnt + 1`
fi
done
echo "$cnt files removed"
-------------------------------------------------------------------------------
pro9.sh
:'Write a shell script to display the name of all the executable file from the
current directory.'
cnt=0
clear
for i in `ls`
do
if [ -x $i ]
then
echo "$i is executable file"
cnt=`expr $cnt + 1`
fi
done
echo "$cnt executable files"
-------------------------------------------------------------------------------
pro10.sh
:'Write a shell script that will display welcome message according to time'
clear
d=$(date +%H)
if [ $d -lt 12 ]
then
echo "Good Morning"
elif [ $d -lt 16 ]
then
echo "Good Afternoon"
elif [ $d -lt 20 ]
then
echo "Good Evening"
else
echo "Good Night"
fi
-------------------------------------------------------------------------------
pro11.sh
:'Write a shell script to find number of ordinary files and directory files.'
clear
f=0
d=0
for i in `ls`
do
if [ -f $i ]
then
f=`expr $f + 1`
fi
if [ -d $i ]
then
d=`expr $d + 1`
fi
done
echo "ordinary file:-$f"
echo "directory:-$d"
-------------------------------------------------------------------------------
pro12.sh
:'Write a shell script that takes a filename from the command line and checks
whether the file is an ordinary file or not.
If it is an ordinary file then it should display the contents of the file.
If it is not an ordinary file then script should display the message:
“File does not exist or is not ordinary, cannot display.“'
clear
if [ $# -gt 0 ]
then
if [ -f $1 ]
then
cat $1
else
echo "File does not exist or is not ordinary, cannot display"
fi
else
echo "Enter argument"
fi
-------------------------------------------------------------------------------
pro13.sh
:'Write a shell script that takes a filename from the user and checks whether it is
a directory file or not.
If it is a directory, then the script should display the contents of the
directory.
If it is not a directory file then script should display the message:
“File is not a directory file“
'
clear
if [ -d $1 ]
then
ls $1
else
echo "File is not a directory file"
fi
-------------------------------------------------------------------------------
pro14.sh
:'Write a shell script that takes a filename as an argument and checks if the file
exists and is executable.
If the file is executable then the shell script should display the message:
“File exists”
If the file does not exists and is not executable then the script should
display the message:
“File does not exist or is not executable.”'
clear
if [ -e $1 -a -x $1 ]
then
echo "File exists"
else
echo "File does not exits or is not executable"
fi
-------------------------------------------------------------------------------
pro15.sh
pro16.sh
:'Write a shell script that calculates the number of ordinary and directory files
in your current working directory.'
clear
f=0
d=0
for i in `ls`
do
if [ -f $i ]
then
f=`expr $f + 1`
fi
if [ -d $i ]
then
d=`expr $d + 1`
fi
done
echo "ordinary file:- $f"
echo "directory:- $d"
-------------------------------------------------------------------------------
pro17.sh
:'Write a shell script that accepts 2 filenames and checks if both exists; if both
exist then append the content of the second file into the first file.'
clear
echo "Enter file name"
read x
echo "Enter file name"
read y
if [ -e $x -a -e $y ]
then
echo "First File"
cat $x
echo "Second File"
cat $y
cat $y >> $x
echo "Merged File"
cat $x
else
echo "file does not exists"
fi
-------------------------------------------------------------------------------
pro18.sh
:'Write a shell script that takes the name of two files as arguments and performs
the following:
i. Displays the message :
“Displaying the contents of file :(first argument)”
and displays the contents page wise.
ii. Copies the contents of the first argument to second argument.
iii. Finally displays the message :
“File copied successfully.”'
clear
if [ -e $1 ]
then
echo "Displaying the contents of file :$1"
cat $1 | more
cp $1 $2
echo "File copied successfully."
fi
-------------------------------------------------------------------------------
pro19.sh
:'Write a shell script to display the following menu and acts accordingly:
i. Calendar of the current month and year.
ii. Display “Good Morning/Good Afternoon/Good Evening” according to the current
login time.
iii. User name, Users home directory.
iv. Terminal name, Terminal type.
v. Machine name.
vi. No. of users who are currently logged in; List of users who are currently
logged in.'
clear
echo "i. Calendar of the current month and year."
echo "ii. Display “Good Morning/Good Afternoon/Good Evening” according to the
current login time."
echo "iii. User name, Users home directory."
echo "iv. Terminal name, Terminal type."
echo "v. Machine name."
echo "vi. No. of users who are currently logged in; List of users who are currently
logged in."
read c
case $c in
1)cal;;
2)d=who am i | tr -s " " " " | cut -d " " -f 4 | cut -c 1-2
if [ $d -lt 12 ]
then
echo "Good morning"
elif [ $d -lt 16 ]
then
echo "Good afternoon"
elif [ $d -lt 20 ]
then
echo "Good evening"
else
echo "good night"
fi;;
3)echo "User name :- $LOGNAME"
echo "User directory :- $HOME";;
4)echo "Machine name:- "; uname -m;;
5)echo "No. of users who are currently logged in:- "who | wc -l;;
*)echo "invalid input";;
esac
-------------------------------------------------------------------------------
pro20.sh
:'Write a shell script that displays the following menu and acts accordingly
1. Concatenates two strings
2. Renames a file
3. Deletes a file.
4. Copy the file to specific location'
clear
echo "
1. Concatenates two strings
2. Renames a file
3. Deletes a file.
4. Copy the file to specific location"
read c
case $c in
1) echo enter First string
read a
echo enter Second string
read b
s=$a$b
echo "Concated string : "$s;;
2) mv abc.txt ABC.txt;;
3) rm -f xyz.txt;;
4) cp abc.txt abc/;;
*) echo "invalid choice"
esac
-------------------------------------------------------------------------------
pro21.sh
:'Write a shell script to change the suffix of all your *.txt files to .dat.'
clear
for i in `ls *.txt`
do
f=`echo $i | cut -d "." -f 1`
mv $i $f.dat
done
-------------------------------------------------------------------------------
pro22.sh
pro23.sh
:'Write a shell script to get all files of home directory and rename them if their
names start with c.
Newname = oldname111'
clear
y="111"
c="c"
for i in `ls $HOME`
do
if [ -f $i ]
then
x=`echo $i | cut -c 1`
if [ $x = $c ]
then
fn=`echo $i | cut -d "." -f 1`
ex=`echo $i | cut -d "." -f 2`
mv $i $fn$y.$ex
fi
fi
done
-------------------------------------------------------------------------------
pro24.sh
:'Write a shell script that takes two filename as arguments. It should check
whether the contents of two files are same or not, if they are same then second
file should be deleted.'
clear
echo "enter file name"
read x
echo "enter file name"
read y
echo $total
if [ $total -eq 0 ]
then
echo "Both file same"
rm -f $y
else
echo "Both file does not same"
fi
-------------------------------------------------------------------------------
pro25.sh
:'Write a shell script that accepts two directory names from the command line and
copies all the files of one directory to another.
The script should do the following
If the source directory does not exist, flash a error message
If destination directory does not exist create it
Once both exist copy all the files from source directory to destination
directory.'
clear
if [ $# -eq 2 ]
then
if [ ! -e $1 ]
then
echo "Source directory does not exists"
else
if [ ! -e $2 ]
then
mkdir $2
echo "Destination directory is created"
fi
cp -rf $1/* $2/
fi
else
echo "Invalid arguments"
fi
-------------------------------------------------------------------------------
pro26.sh
-------------------------------------------------------------------------------
pro27.sh
:'Write a shell script that displays all hidden files in current directory.'
clear
ls -d .[a-z]*
-------------------------------------------------------------------------------
pro28.sh
:'Write a shell script that Combine two files in the third file horizontally and
vertically.'
clear
echo "Enter file name"
read x
echo "Enter file name"
read y
-------------------------------------------------------------------------------
pro29.sh
:'Write a shell script to delete all the spaces from a given file.'
clear
echo "Enter file name"
read fn
cat $fn | tr -d " " > tp.txt
-------------------------------------------------------------------------------
pro30.sh
-------------------------------------------------------------------------------
pro31.sh
:'Write a shell script to search for a given word in all the files given as the
arguments on the command line.'
clear
if [ $# -lt 1 ]
then
echo "invalid argument"
else
echo "entera word:-"
read word
x=0
fi
-------------------------------------------------------------------------------
pro32.sh
:'Write a shell script that display last modified file in the current directory.'
clear
echo "enter file name"
read x
if [ -f $x ]
then
echo "last mofidication is :- \c";ls -l $x | cut -c 38-42
else
echo "$x is not file"
fi
-------------------------------------------------------------------------------
pro33.sh
-------------------------------------------------------------------------------
pro34.sh
else
echo "Invalid arguments"
fi
-------------------------------------------------------------------------------
pro36.sh
:'Write a shell script to check whether the named user is currently logged in or
not.'
if [ $# -eq 1 ]
then
c=`who|cut -d " " -f 1|grep -c $1`
if [ $c -gt 0 ]
then
echo "$1 is currently logged in"
else
echo "$1 is currently does not logged in"
fi
else
echo "Invalid argument"
fi
-------------------------------------------------------------------------------
pro37.sh
:'Write a shell script to display the following menu for a particular file:
i.Display all the words of a file in ascending order.
ii.Display a file in descending order.
iii.Display a file in reverse order.
iv.Toggle all the characters in the file
v.Display type of the file.'
echo "Enter the Filename: "
read fn
echo "i. Display all the words of a file in ascending order."
echo "ii. Display a file in descending order."
echo "iii. Display a file in reverse order."
echo "iv. Toggle all the characters in the file."
echo "v. Display type of the file."
echo "Enter your choice : "
read c
case $c in
i)for i in `cat $fn`
do
echo $i >> ff
done
echo "File in ascending order: "
cat ff | sort
rm -f ff;;
ii)for i in `cat $fn`
do
echo $i >> ff
done
echo "File in decending order: "
cat ff | sort -r
rm -f ff;;
iii)for i in `cat $fn`
do
echo $i >> ff
done
x=`cat ff | wc -l`
echo "Reverse order: "
while [ $x -gt 0 ]
do
s=`cat ff | head -$x | tail -1`
echo $s
x=`expr $x - 1`
done
rm -f ff;;
iv)x=`cat $fn | wc -l`
i=1
while [ $i -le $x ]
do
s=`cat $fn | head -$i | tail -1`
xx=`echo $s | wc -c`
j=1
str=""
while [ $j -le $xx ]
do
ch=`echo $s | cut -c $j`
case $ch in
[A-Z])ch=`echo $ch | tr [A-Z] [a-z]`;;
[a-z])ch=`echo $ch | tr [a-z] [A-Z]`;;
esac
str=$str$ch
j=`expr $j + 1`
done
echo $str >> ff
i=`expr $i + 1`
done;;
v)if [ -f $fn ]
then
echo "$fn is ordinary file"
elif [ -d $fn ]
then
echo "$fn is Directory file"
elif [ -x $fn ]
then
echo "$fn is Executable file"
elif [ -r $fn ]
then
echo "$fn is Readable file"
elif [ -w $fn ]
then echo "$fn is Writable file"
fi;;
esac
-------------------------------------------------------------------------------
pro38.sh
:'Write a shell script to find total no. Of users and finds out how many of them
are currently logged in.'
clear
all=`cat /etc/passwd | wc -l`
h=`who | wc -l`
echo "Total login:-$all"
echo "current login:-$h"
-------------------------------------------------------------------------------
pro39.sh
:'Write a shell script that displays the directory information in the following
format-
Filename Size Date Protection Owner'
clear
x=`ls -l | wc -l`
echo $x
i=2
echo "Filename\t\tSize\tDate\tProtection\tOwner"
while [ $i -le $x ]
do
s=`ls -l | head -$i | tail -1 | tr -s " "`
fn=`echo $s | cut -d " " -f 8`
s1=`echo $s | cut -d " " -f 5`
d1=`echo $s | cut -d " " -f 6`
d2=`echo $s | cut -d " " -f 7`
p=`echo $s | cut -d " " -f 1`
o=`echo $s | cut -d " " -f 3`
echo "$fn\t\t$s1\t$d1 $d2\t$p\t$o"
i=`expr $i + 1`
done
-------------------------------------------------------------------------------
pro40.sh
:'Write a shell script to display five largest files from the current directory'
clear
ls -S | head -5
-------------------------------------------------------------------------------
pro41.sh
-------------------------------------------------------------------------------
pro44.sh
:'Write a shell script to accept any character using command line and list all the
files starting with that character in the current directory.'
clear
for i in `ls`
do
if [ -f $i ]
then
y=`echo $i | cut -c 1`
if [ $y = $1 ]
then
echo $i
fi
fi
done
-------------------------------------------------------------------------------
pro45.sh