0% found this document useful (0 votes)
13 views20 pages

SPP

The document contains a series of shell scripts designed to perform various tasks, including file manipulation, user interaction, and system information retrieval. Each script addresses specific commands such as sorting files, displaying hidden files, and checking file types, among others. The scripts are structured to prompt user input and execute commands based on the provided options.

Uploaded by

sachipatel2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views20 pages

SPP

The document contains a series of shell scripts designed to perform various tasks, including file manipulation, user interaction, and system information retrieval. Each script addresses specific commands such as sorting files, displaying hidden files, and checking file types, among others. The scripts are structured to prompt user input and execute commands based on the provided options.

Uploaded by

sachipatel2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

pro3.

sh

:'Write a shell script to execute following commands


1. Sort file abc.txt and save this sorted file in xyz.txt
2. Give an example of : To execute commands together without affecting result of
each other.
3. How to print “this is
a three –line
1. Text message”
4. Which command display version of the UNIX?
5. How would u get online help of cat command?'
clear
echo "1. sort file and save this sorted file in anothor file"
echo "2. execute command without affecting result of each other"
echo "3. how to print this is
a three -line
1. Text message"
echo "4. version of unix"
echo "5. online help of cat command"
read c
case $c in
1) echo "Enter Source File"
read srf
echo "Enter Destination File"
read def
sort $srf >> $def;;
2)who am i;cal;;
3)echo -e "\t\tthis is\n\t\t\ta three - line\n\t\t\t\t1. Text message";;
4)uname -r;;
5)man cat;;
*)echo "invalid option";;
esac

-------------------------------------------------------------------------------

pro4.sh

:'Write a shell script to execute following commands


1. How would u display the hidden files?
2. How delete directory with files?
3. How would user can do interactive copying?
4. How would user can do interactive deletion of files?
5. Explain two functionality of “mv” command with example?'
clear
echo "1. hidden file"
echo "2. delete directory with file"
echo "3. interactive copying"
echo "4. interactive deletion of file"
echo "5. two functionality of mv command"
read ch
case $ch in
1)ls -d .[a-z]*;;
2)echo "Enter Directory Name"
read d
rm -r $d;;
3)echo "Enter soucrce Name"
read src
echo "Enter Destination Name"
read dst
cp -i $src $dst;;
4)echo "Enter File Name"
read dfile
rm -i $dfile;;
5)echo "1. rename file"
echo "2. move file"
read x
case $x in
1)mv abc.txt ABCD.txt;;
2)mv ABC.txt pdpica;;
esac
*)echo "invalid option"
esac
-------------------------------------------------------------------------------

pro5.sh

:'Write a shell script to execute following commands


1. Create a file called text and store name,age and address in it.
2. Display the contents of the file text on the screen.
3. Delete the directories mydir and newdir at one shot.
4. Sort a numeric file?
5. Change the permissions for the file newtext to 666.'
clear
echo "1. create file and store in name,age,address"
echo "2. display content of the file"
echo "3. Delete the directories mydir and newdir at one shot"
echo "4. Sort a numeric file"
echo "5. Change the permissions for the file newtext to 666"
echo "enter your choice"
read c
case $c in
1)echo "enter name"
read nm
echo "enter age"
read age
echo "enter address"
read add
echo -e "$nm\t$age\t$add" >> name.txt;;
2)echo "enter file name"
read fn
cat $fn;;
3)rm -r mydir newdir;;
4)sort -n no.txt;;
5)chmod 666 newtext.txt;;
*)echo "invalid input"
esac

-------------------------------------------------------------------------------

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

:'Write a shell script that displays all subdirectories in current working


directory.'
clear
d=`pwd`
c=0
for i in `ls $d`
do
if [ -d $i ]
then
c=`expr $c + 1`
echo "Directory:-$i"
fi
done
echo "directory:-$c"
-------------------------------------------------------------------------------

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

:'Write a shell script to accept a directory-name and display its contents. If


input is not given then HOME directorys contents should be listed. (Make use of
command line argument)'
clear
if [ $# -eq 0 ]
then
ls -l $HOME
else
ls -l $1
fi
-------------------------------------------------------------------------------

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

cmp $x $y > error.txt

total=`wc -l error.txt | cut -d " " -f 1`

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

:'Write a shell script that displays the following menu


List home directory
Date
Print working directory
Users logged in
Read the proper choice. Execute corresponding command. Check for invalid choice.'
clear
echo "
1.List home directory
2.Date
3.Print working directory
4.Users logged in"
read c
case $c in
1)ls /home;;
2)date;;
3)pwd;;
4)who am i;;
*)echo "Invalid Choice";;
esac

-------------------------------------------------------------------------------

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

cat $x > vtmp.txt


cat $y >> vtmp.txt
echo "Vertical"
cat vtmp.txt
paste -d " " $x $y > htmp.txt
echo "Horizontal"
cat htmp.txt

-------------------------------------------------------------------------------

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

:'Write a shell script to find a given date fall on a weekday or a weekend.'


clear
if [ $# -eq 1 ]
then
d=$( date --date $1 +%w )
if [ $d -gt 5 ]
then
echo "weekend"
else
echo "weekDay"
fi
else
echo "Invalid arguments"
echo "Usage: year-month-day"
fi

-------------------------------------------------------------------------------

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

for i in `echo $*`


do
if [ -f $i ]
then
x=`cat $i | grep -c "$word"`
if [ $x -ne 0 ]
then
echo "word found in file : $i"
break
fi
fi
done

-------------------------------------------------------------------------------

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

:'Write a script to display the permissions of the particular file.'


clear
echo "Enter file name"
read x
echo "$x file permission is:- \c";ls -l $x | cut -c 2-10

-------------------------------------------------------------------------------

pro34.sh

:'Write a shell script to display the calendar in the following manner:


i. Display the calendar of months m1 and m2 by "CAL m1, m2" command file.
ii. Display the calendar of the months from m1 to m2 by "CAL m1-m2" command
file.'
if [ $# -eq 1 ]
then
y="-"
z=","
l=`echo $1 | wc -c`
i=1
c=0
while [ $i -le $l ]
do
x=`echo $1 | cut -c $i`
if [ $x = $y ]
then
c=1
break
elif [ $x = $z ]
then
c=2
break
fi
i=`expr $i + 1`
done
if [ $c -eq 1 ]
then
first=`echo $1 | cut -d "-" -f 1`
second=`echo $1 | cut -d "-" -f 2`
while [ $first -le $second ]
do
cal $first 07
first=`expr $first + 1`
done
elif [ $c -eq 2 ]
then
first=`echo $1 | cut -d "," -f 1`
second=`echo $1 | cut -d "," -f 2`
cal $first 07
cal $second 07
else
echo "Argument not entered correctly"
fi

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

:'Write a shell script that toggles contents of the file'


clear
echo "Enter file name"
read fn

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`
echo $ch
case $ch in
[A-Z])ch=`echo $ch | tr [:upper:] [:lower:]`;;
[a-z])ch=`echo $ch | tr [:lower:] [:upper:]`;;
esac
str=$str$ch
j=`expr $j + 1`
done
echo $str >> ff
i=`expr $i + 1`
done

-------------------------------------------------------------------------------

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

:'Create a file called student containing roll-no, name and marks.


a. Display the contents of the file sorted by marks in descending order
b. Display the names of students in alphabetical order ignoring the case.
c. Display students according to their roll nos.
d. Sort file according to the second field and save it to file "names".
e. Display the list of students who scored between 70 and 80.'
clear
echo "1. Display the contents of the file sorted by marks in descending order
2. Display the names of students in alphabetical order ignoring the case.
3. Display students according to their roll nos.
4. Sort file according to the second field and save it to file "names".
5. Display the list of students who scored between 70 and 80."
read c
case $c in
1) cat student.txt | cut -f 3 | sort -n -r;;
2) cat student.txt | cut -f 2 | sort -f;;
3) cat student.txt | cut -f 1 | sort -n;;
4) cat student.txt | cut -f 2 | sort >> names.txt;;
5) for i in `cat student.txt | cut -f 3`
do
if [ $i -ge 70 -a $i -le 80 ]
then
echo $i
fi
done;;
*) echo "Invalid character"
esac
-------------------------------------------------------------------------------

You might also like