Bhavik and Parth
Bhavik and Parth
Bhavik and Parth
clear
read fileName
str=`find $fileName`
path=`pwd`
3 Write a shell script to execute following commands Sort file abc.txt and save this sorted file
inxyz.txt Give an example of : To execute commands together without affecting result of
eachother. How to print “thisis a three –line 1. Textmessage” Which command display version
of theUNIX?
sort bhavik.txt; ls
echo “this is
a three-line
Text message”
man cat
4 Write a shell script to execute following commands How would u display the hiddenfiles? How
delete directory withfiles? How would user can do interactivecopying? How would user can do
interactive deletion offiles? Explain two functionality of “mv” command withexample?
echo “1. How would u display the hidden files”
echo “2. How delete directory with files”
echo “3. How would user can do interactive copying”
echo “4. How would user can do interactive deletion of files”
esac
5 Write a shell script to execute following commands Create a file called text and store name,age
and address init. Display the contents of the file text on thescreen. Delete the directories mydir
and newdir at oneshot. Sort a numericfile? Change the permissions for the file newtext to666.
echo “1. Create a file called text and store name,age and address in it.”
echo “2. Display the contents of the file text on the screen.”
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 ch
case $ch in
1) echo “Create a file called text and store name,age and address in it.”
echo “Enter the filename”
read fn
cat > $fn ;;
2) echo “Display the contents of the file text on the screen.”
cat $fn ;;
3) echo “Delete the directories mydir and newdir at one shot.”
rmdir mydir newdir ;;
4) echo “Sort a numeric file”
sort -n filename ;;
5) echo “Change the permissions for the file newtext to 666.”
chmod 666 newtext ;;
*) echo “Invalid choice” ;;
esac
6 Write shell script that accept filename and displays last modification time if file exists, otherwise
display appropriate message.
if [ -e $fn ]
then
ls -l $fn | cut -d “ “ -f8 #change the column number for desired output
else
echo “File does not exist”
fi
7 Write a shell script to display the login names that begin with ‘s’.
who | grep ^s
8 Write a shell script to remove the zero sized file from the current directory
for i in *
do
if [ ! -s $i ]
then
rm $i
echo " $i removed "
fi
done
9 Write a shell script to display the name of all the executable file from the current directory.
for i in *
do
if [ -x $i ]
then
countx=`expr $countx + 1`
fi
echo “Number of executable files are $countx”
10 Write a shell script that will display welcome message according to time
d=`date +"%H"`
if [ $d -lt 12 ]
then
echo “Good Morning”
elif [ $d -gt 12 -a $d -lt 14 ]
then
echo “Good Afternoon”
else
echo “Good Evening”
fi
11 Write a shell script to find number of ordinary files and directory files.
for i in *
do
if [ -d $i ]
then
countd=`expr $countd + 1`
fi
if [ -f $i ]
then
countf=`expr $countf + 1`
fi
done
echo “Number of directories are $countd ”
echo “Number of Ordinary files are $countf”
12 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 thefile. If it is not
an ordinary file then script should display the message: “ File does not exist or is not ordinary,
cannot display.“
if [ -f $1 ]
then
echo “Its an ordinary file”
cat $1
else
echo “File does not exist or is not ordinary file”
fi
13 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“
echo “enter the filename”
read fn
if [ -d $fn ]
then
echo “Its a directory”
ls $fn
else
echo “Its not a directory”
fi
14 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: “Fileexists” If
the file does not exists and is not executable then the script should display the message: “File does
not exist or is notexecutable.”
if [ -e $fn -a -x $fn ]
then
echo “file exists and is executable”
else
echo “file does not exist or is not executable”
fi
15 Write a shell script that displays all subdirectories in current working directory.
echo “List of Directories. ”
for i in *
do
if [ -d $i ]
then
echo $i
fi
16 Write a shell script that calculates the number of ordinary and directory files in your current
working directory.
for i in *
do
if [ -d $i ]
then
countd=`expr $countd + 1`
fi
if [ -f $i ]
then
countf=`expr $countf + 1`
fi
done
echo “Number of directories are $countd ”
echo “Number of Ordinary files are $countf”
17 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.
echo “enter the first filename”
read fn1
echo “enter the second filename”
read fn2
if [ -f $fn1 -a -f $fn2 ]
then
echo “Both file exists”
cat $fn2 >> $fn1
else
echo “Files does not exist”
fi
18 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.”
echo “Displaying the contents of file $1”
cat $1
echo “Displaying the contents page wise”
cat $1 | more
echo “Copying the files”
cp $1 $2
c=`echo $?`
if [ $c -eq 0 ]
then
echo “File copied successfully”
else
echo “Files not copied successfully”
fi
19 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 tothe 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.
echo “1. Calendar of the current month and year.”
echo “2. Display “Good Morning/Good Afternoon/Good Evening” according to the current login time.”
echo “3. User name, Users home directory.”
echo “4. Terminal name, Terminal type.”
echo “5 Machine name.”
echo “6. No. of users who are currently logged in; List of users who are currently logged in.”
echo “enter your choice”
read ch
case $ch in
1) echo “Calendar of current month is”
cal ;;
2) d=`date +"%H"`
if [ $d -lt 12 ]
then
echo “Good Morning”
elif [ $d -gt 12 -a $d -lt 16 ]
then
echo “Good Afternoon”
else
echo “Good Evening”
fi
3) echo “Username is $USER”
echo “Users Home directory is $HOME” ;;
4) echo “Terminal details”
tty;;
5) echo “Machine name is”
uname -m ;;
6) echo “The number of users logged in are”
who | wc -l
*) echo “Invalid choice”
esac
20 Write a shell script that displays the following menu and acts accordingly 1. Concatenates
twostrings 2. Renames afile 3. Deletes afile. 4. Copy the file to specific location
if [ $# ]
then
ls $1
else
ls $HOME
fi
sh filename.sh dir1
23 Write a shell script to get all files of home directory and rename them if their names start with c.
Newname = oldname111
24 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.
if [ $# ]
then
if [ -d $1 ]
then
if [ -d $2 ]
then
cp -R $1 $2
else
mkdir $2
echo "Directory created $2"
cp -R $1 $2
fi
else
echo “source directory does not exist”
fi
else
echo "Please provide command line arguments"
fi
26 Write a shell script that displays the following menu ▪ List homedirectory ▪ Date ▪ Print
workingdirectory ▪ Users loggedin Read the proper choice. Execute corresponding command. Check
for invalid choice.
ls .[a-z]*
28 Write a shell script that Combine two files in the third file horizontally and vertically.
d=`date +”%u”`
if [ $d –eq 7 ]
then
echo “it is weekend”
else
echo “it is a weekday”
fi
31 Write a shell script to search for a given word in all the files given as the arguments on the
command line.
if [ $c -gt 0 ]
then
echo “User is currently logged in ”
else
echo “User is not currently logged in”
fi
43 Write a shell script for the performing the write and mail.
44 Write a shell script to accept any character using command line and list all the files starting with
that character in the current directory.
ls | grep ^$1
echo “1. Display the contents of the file sorted by marks in descending order”
echo “2. Display the names of students in alphabetical order ignoring the case.”
echo “3. Display students according to their roll nos.”
echo “4. Sort file according to the second field and save it to file ‘names’.”
echo “5. Display the list of students who scored between 70 and 80”
case $ch in
1) sort -k5 -r $fn ;;
2) sort -k3 -i $fn ;;
3) sort $fn ;;
4) sort -k3 $fn > names ;;
5) awk ‘{ if( $5 > 70 && $5 < 80 ) print $5 }’ $fn ;;
*) echo “Invalid Choice”
esac