A ) Accept numbers and perform addition, subtraction, division and multiplication.
ANS:
[mca09113@ALS ~]$ cat f5.sh
a=10
echo "Enter The Value of A: "
read a
b=10
echo "Enter The Value of B: "
read b
c=0
c=`expr $a + $b`
echo "Addition " $c
c=`expr $a - $b`
echo "Subtraction " $c
c=`expr $a / $b`
echo "Division " $c
c=`expr $a \* $b`
echo "Multiplication " $c
OUTPUT:
[mca09113@ALS ~]$ sh f5.sh
Enter The Value of A:
20
Enter The Value of B:
10
Addition 30
Subtraction 10
Division 2
Multiplication 200
B ) Accept the string and checks whether the string is palindrome or not.
ANS:
[mca09113@ALS ~]$ cat f6.sh
l=0
cnt=1
tag=0
echo "Enter a String?"
read str
l=`echo $str |wc -c`
l=`expr $l - 1`
lh=`expr $l / 2`
while [ $cnt -le $lh ]
do
c1=`echo $str|cut -c$cnt`
c2=`echo $str|cut -c$l`
if [ $c1 != $c2 ]
then
tag=1
fi
cnt=`expr $cnt + 1`
l=`expr $l - 1`
done
if [ $tag -eq 0 ]
then
echo "String is Palindrome"
else
echo "String is not Palindrome"
fi
OUTPUT:
[mca09113@ALS ~]$ sh f6.sh
Enter a String?
hemant
String is not Palindrome
[mca09113@ALS ~]$ sh f6.sh
Enter a String?
ava
String is Palindrome
C ) .Accept number and check the number is even or odd, finds the length of the number, sum of the
digit in the number.
ANS:
[mca09113@ALS ~]$ cat f7.sh
echo "Enter number:"
read no
c=`expr $no % 2`
if [ $c -eq 0 ]
then
echo "No is even "
else
echo "No is odd"
fi
cnt=`echo $no | wc -c`
cnt=`expr $cnt - 1`
echo "word count =" $cnt
i=1
while [ $i -le $cnt ]
do
j=`echo $no | cut -c $i`
k=`expr $k + $j`
i=`expr $i + 1`
done
echo "total =" $k
OUTPUT:
[mca09113@ALS ~]$ sh f7.sh
Enter number:
123
No is odd
word count = 3
total = 6
d ). Accept strings and replace a string by another string.
ANS:
echo "Enter any string:"
read str
echo "Your string is :" $str
echo "Enter word that your what replace with other word"
read oldstr
echo "Enter new string"
read newstr
str1=`echo $str | sed s/$oldstr/$newstr/g`
str2=`echo $str | replace $oldstr $newstr`
echo "after replacing " $str1
echo "after replacing " $str2
OUTPUT:
[mca09113@ALS ~]$ sh f8.sh
Enter any string:
hemant
Your string is : hemant
Enter word that your what replace with other word
t
Enter new string
tfofandi
after replacing hemantfofandi
e). Accept filename and displays last modification time if file exists, otherwise display appropriate message.
ANS:
[mca09113@ALS ~]$ cat f9.sh
echo "Enter file name"
read fnm
if [ ! -f $fnm ]
then
echo "File Not Found"
exit 1
fi
dt=`date -r $fnm`
echo "modification date is:" $dt
OUTPUT:
[mca09113@ALS ~]$ sh f9.sh
Enter file name
f6.sh
modification date is: Tue Dec 7
F ). Fetch the data from a file and display data into another file in reverse order.
echo "enter file name for fatching data and store in other file"
ANS:
[mca09113@ALS ~]$ cat f10.sh
echo "enter a file name to do operation "
read fnm
if [ ! -f $fnm ]
then
echo "File not found"
exit 1
fi
echo "enter new file name:"
read newfile
while read -n1 char;
do
str=$char$str
done<$fnm
echo $str>$newfile
echo $newfile "successfully created on" `pwd` " path"
OUTPUT:
[mca09113@ALS ~]$ sh f10.sh
enter a file name to do operation
f6.sh
enter new file name:
hello.txt
hello.txt successfully created on /home/PARAM/mca09113 path
GTU 3:
Write a script to find the global complete path for any file.
ANS:
[mca09113@ALS ~]$ cat gtu3.sh
echo -e "Enter the filename to search in current directory: \c"
read FILE
args=`find . -name $FILE | xargs`
for i in $args
do
if [ -f "$i" ]; then
CPATH=`readlink -f "$i"`
echo $CPATH
fi
done
noargs=${#args}
if [ "$noargs" -eq "0" ]; then
echo "No such a file exists"
fi
OUTPUT:
[mca09113@ALS ~]$ sh gtu3.sh
Enter the filename to search in current directory: gtu3.sh
/home/PARAM/mca09113/gtu3.sh
[mca09113@ALS ~]$ sh gtu3.sh
Enter the filename to search in current directory: f10.sh
/home/PARAM/mca09113/f10.sh
GTU 4:
Write a script to broadcast a message to a specified user or a group of users logged on any terminal
ANS:
[mca09113@ALS ~]$ cat gtu4.sh
who > e.txt
echo "For Broadcasting Messages."
if [ $# = 0 ]
then
echo "Enter the User Name:\c"
read nam
p=`grep $nam e.txt`
if [ "$p" != "" ]
then
write $nam
rm e.txt
exit
else
echo "The user is not logged in"
fi
rm e.txt
exit
fi
if [ "$1" = "all" ]
then
wall
fi
for nam in $*
do
p=`grep $nam e.txt`
if [ "$p" != "" ]
then
write $nam
else
echo "The User $nam is not logged in"
fi
done
OUTPUT:
[mca09113@ALS ~]$ sh gtu4.sh
For Broadcasting Messages.
Enter the User Name:\c
mca09112
The user is not logged in
[mca09113@ALS ~]$ sh gtu4.sh
For Broadcasting Messages.
Enter the User Name:\c
mca09113
Message from mca09113@ALS on pts/2 at
hi
hi
hello
hello
GTU 5:
Write a script to copy the file system from two directories to a new directory in such a way that only the latest file is copied in case there are common files in both the directories.
ANS:
[mca09113@ALS ~]$ cat gtu5.sh
EXIT=n
while [ $EXIT != y ]
do
sleep 1
echo -e "\n"
echo -e " 1. Display PWD
2. Long Listing
3. Change Directory
4. Copy Newest File.
5. Exit
Enter Choice: \c"
read ch
case $ch in
1)
clear
pwd
;;
2)
clear
pwd
ls -l
;;
3)
echo -n "Enter Absolute Path to change directory: "
read apath
cd $apath
if [ $? -eq 0 ]; then
clear
echo "Working Directory Changed successfully to.."
sleep 1
pwd
else
clear
echo "Please check your PATH."
fi
;;
4)
clear
echo "Enter filenames to copy. ( * - for ALL Files, ELSE Separate files by spaces )"
read allfiles
if [ -f $allfiles ]; then
echo "Enter Absolute path, where to copy these files: "
read -e cpath
if [ -d $cpath ]; then
cp -u "$allfiles" $cpath
else
echo "There is no such a directory!"
fi
else
echo "There is/are no such file(s)!"
fi
;;
5)
clear
echo -n "Exiting.."
sleep 1
echo -n "."
sleep 1
echo -n "."
clear
exit
;;
*)
clear
echo "Invalid Choice"
;;
esac
done
i
OUTPUT:
[mca09113@ALS ~]$ sh gtu5.sh
1. Display PWD
2. Long Listing
3. Change Directory
4. Copy Newest File.
5. Exit
Enter Choice:
GTU 6:
wite a script to compare identically named files in two different directories and if they are same, copy one of them in a third directory.
ANS:
[mca09113@ALS ~]$ cat gtu6.sh
echo Enter the name of 1st directory
read dir1
echo Enter the name of 2nd directory
read dir2
echo Enter the name of the directory in which you want to copy the duplicate file
read dir3
cd `find /home/PARAM/mca09113 -type d -name $dir1`
for fl1 in *
do
echo $fl1
cd `find /home/PARAM/mca09113 -type d -name $dir2`
for fl2 in *
do
if [ $fl1 == $fl2 ]
then
cd ..
cp ./$dir2/$fl2 ./$dir3
cd `find /home/PARAM/mca09113 -type d -name $dir2`
rm $fl2
cd `find /home/PARAM/mca09113 -type d -name $dir1`
rm $fl1
fi
done
cd ..
mv ./temp1/* ./$dir2/$fl2
done
OUTPUT:
[mca09113@ALS ~]$ sh gtu6.sh
Enter the name of 1st directory
f1.sh
Enter the name of 2nd directory
f8.sh
Enter the name of the directory in which you want to copy the duplicate file
office
college
cp: cannot stat `./f8.sh/college': No such file or directory
rm: cannot remove `college': Is a directory
rm: cannot remove `college': Is a directory
mv: cannot stat `./temp1/*': No such file or directory
GTU 7
Write a script to delete zero sized files from a given directory .
ANS:
[mca09113@ALS ~]$ cat gtu7.sh
while true; do
read -e -p "Enter the Absolute Path: " path || exit
[[ -d $path ]] && break
echo "Invalid Directory!"
done
args=`find "$path" -empty -print0 | xargs -0`
for i in $args
do
if [ -f "$i" ]; then
rm -i "$i"
fi
done
OUTPUT:
[mca09113@ALS ~]$ sh gtu7.sh
Enter the Absolute Path: /home/param/mca09113
Invalid Directory!
GTU 8:
Write a script to display the name of those files (in the given directory) which are having multiple links
ANS:
[mca09113@ALS ~]$ cat g8.sh
echo "Enter Absolute path of directory"
read path
if test -d $path; then
cd $path
for i in *
do
for j in *
do
if test "$i" != "$j"; then
if test "$i" -ef "$j"; then
echo "$i" >> $$.temp
fi
fi
done
done
cat $$.temp | uniq
rm $$.temp
cd -
else
echo "Check your path."
fi
OUTPUT:
[mca09113@ALS ~]$ sh g8.sh
Enter Absolute path of directory
/home/param/mca09113
Check your path.
GTU 9:
Write a script to display the name of all executable files in the given directory.
ANS:
[mca09113@ALS ~]$ cat g9.sh
i=0
ls > test1
set `wc -l test1`
l=$1
while [ $l -ne 0 ]
do
line=`head -$l test1 | tail -1`
if [ -x $line ]
then
i=`expr $i + 1`
fi
l=`expr $l - 1`
done
echo -e "\t\t\tTotal no. of executable files : "
echo $i
OUTPUT:
[mca09113@ALS ~]$ sh g9.sh
Total no. of executable files : 4
GTU 10:
Write a script to display the date, time and a welcome message (like Good Morning etc.). The time should be displayed with “a.m.” or “p.m.” and not in 24 hours notation.
ANS:
[mca09113@ALS ~]$ cat g10.sh
time=`date +%H%M%S`
d1=`date +%d/%m/%y`
t1=`date +%I:%M:%S`
echo "date:"$d1
if [ $time -ge 000000 -a $time -lt 120000 ]
then
echo "time:"$t1" a.m"
else
echo "time:"$t1" p.m"
fi
if [ $time -lt 120000 -a $time -gt 60000 ]
then
echo "GooD MorninG"
else
if [ $time -eq 120000 ]
then
echo "GooD NooN"
else
if [ $time -gt 120000 -a $time -lt 180000 ]
then
echo "GooD AfterNooN"
else
if [ $time -gt 180000 -a $time -lt 210000 ]
then
echo "GooD EveninG"
else
echo "GooD NighT"
fi
fi
fi
fi
OUTPUT:
[mca09113@ALS ~]$ sh g10.sh
date:
time:
GooD AfterNooN
GTU 11:
Write a script to display the directory in the descending order of the size of each file.
ANS:
[mca09113@ALS ~]$ cat g11.sh
echo -n "Enter directory name : "
read dname
ls $dname > file
echo "No of files in directory : $(grep [^*$] file-c)"
rm -f file
ls | sort -r find . -size +1000c -print
or
echo Enter Directory
read direc
cd $direc
ls -Sl
OUTPUT:
[mca09113@ALS ~]$ sh g11.sh
Enter directory name : hm
grep: file-c: No such file or directory
No of files in directory :
sort: invalid option -- e
Try `sort --help' for more information.
g11.sh: line 12: or: command not found
Enter Directory
hm1
total 20
drwxr-xr-x 3 mca09113 Domain Users 4096 Aug 13
drwxr-xr-x 2 mca09113 Domain Users 4096 Aug 13
-rw-r--r-- 1 mca09113 Domain Users 26 Aug 9
-rw-r--r-- 1 mca09113 Domain Users 26 Aug 9
-rw-r--r-- 1 mca09113 Domain Users 26 Aug 9
GTU 12:
Write a script to implement the following commands:
Tree (of DOS)
which (of UNIX)
ANS:
[mca09113@ALS ~]$ cat 12g.sh
echo "Enter Dir Name for Structure .."
read dir1
for i in `find $dir`
do
if [ -d $i ]
then
echo "+" $i ;
elif [ -f $i ]
then
echo "-----" $i
fi
done
OUTPUT:
[mca09113@ALS ~]$ sh 12g.sh
Enter Dir Name for Structure ..
college
+ .
GTU 13:
Write a script for generating a mark sheet after reading data from a file.
File contains student roll no, name , marks of three subjects.
ANS:
count=1
total=0
per=0
rem1=0
wc -l s > temp
echo "************************************************************"
>>s.out
echo " Students Result Are As Follows....."
>> s.out
echo "************************************************************"
>> s.out
echo "Rno NAME SUB1 SUB2 SUB3 TOTAL PERCENTAGE GRADE RESULT"
>> s.out
echo "************************************************************"
>> s.out
while [ $count -le `awk '{print $1}' temp` ]
do
let total=0
head -$count s > temp1
tail -1 temp1 > temp2
rollno=`awk '{print $1}' temp2`
name=`awk '{print $2}' temp2`
sub1=`awk '{print $3}' temp2`
sub2=`awk '{print $4}' temp2`
sub3=`awk '{print $5}' temp2`
let total=$total+`awk '{print $3}' temp2`
let total=$total+`awk '{print $4}' temp2`
let total=$total+`awk '{print $5}' temp2`
echo "STUDENT NAME IS :" `awk '{print $2}' temp2`
echo "TOTAL IS :" $total
let per=`expr $total / 3`
let rem1=`expr $total % 3`
if test $rem1 -ne 0
then
let per=$per+1
fi
echo "PERCENTAGE IS :" $per
if [ $per -ge 70 ]
then
grade="A+"
elif test $per -ge 60
then
grade="A"
elif test $per -ge 50
then
grade="B"
elif test $per -ge 45
then
grade="C"
fi
echo "GRADE IS :" $grade
let count=$count+1
echo $rollno $name " " $sub1 " " $sub2 " " $sub3 " " $total "
" $per " " $grade
>> s.out
echo "-----------------------------------------------------"
>> s.out
done
rm temp
rm temp1
rm temp2
OUTPUT:
GTU 14:
Write a script to make following file and directory management operations menu based:
Display current directory, List directory, Make directory, Change directory, Copy a file , Rename a file , Delete a file , Edit a file
ANS:
[mca09113@ALS ~]$ cat g14.sh
clear
choice=y
while [ "$choice" = "y" ]
do
echo " MAIN MENU "
echo "***************************"
echo "1 - DISPLAY CURRENT DIRECTORY"
echo "2 - LIST DIRECTORY"
echo "3 - MAKE DIRECTORY"
echo "4 - CHANGE DIRECTORY"
echo "5 - COPY A FILE"
echo "6 - RENAME A FILE"
echo "7 - DELETE A FILE"
echo "8 - EDIT A FILE"
echo "9 - EXIT"
echo "ENTER THE CHOICE :- "
read choice
case $choice in
1) echo "DISPLAYING CURRENT DIRECTORY"
pwd
;;
2) echo "\nLIST THE CURRENT DIRECTORY"
ls -l
read
;;
3) echo "\nMAKE A NEW DIRECTORY"
echo "\nENTER THE DIRECTORY NAME :-\c"
read dirname
mkdir "$dirname"
read dirname
;;
4) echo "\nCHANGE THE CURRENT DIRECTORY"
echo "\nENTER THE DIRECTORY TO WHICH YOU WANT TO
read dirname
pwd
cd ..
;;
5) echo "\nCOPY THE GIVEN FILE TO THE GIVEN DESTINAT
echo "\nENTER THE FILE TO COPY :- \c"
read file
echo "\nENTER THE DESTINATION TO COPY :- \c"
read destin
cp "$file" "$destin"
;;
6) echo "\nRENAMING THE GIVEN FILE"
echo "\nENTER THE FILE TO RENAME :- \c"
read file1
echo "\nENTER THE NEW NAME :- \c"
read file2
mv "$file1" "$file2"
;;
7) echo "\nDELETING THE GIVEN FILE"
echo "\nENTER THE FILE TO DELETE :- \c"
read file
rm "$file"
;;
8) echo "\nEDITING A GIVEN FILE"
echo "\nENTER FILE TO EDIT :- \c"
read file
vi "$file"
:q
;;
9) exit
;;
*) echo "Invalid Choice ....."
;;
esac
echo -e "Do u want to continue.....? [y/n]"
read choice
case $choice in
Y|y) choice=y;;
N|n) choice=n;;
*) choice=y;;
esac
done
OUTPUT:
[mca09113@ALS ~]$ sh g14.sh
MAIN MENU
***************************
1 - DISPLAY CURRENT DIRECTORY
2 - LIST DIRECTORY
3 - MAKE DIRECTORY
4 - CHANGE DIRECTORY
5 - COPY A FILE
6 - RENAME A FILE
7 - DELETE A FILE
8 - EDIT A FILE
9 - EXIT
ENTER THE CHOICE :-
1
DISPLAYING CURRENT DIRECTORY
/home/PARAM/mca09113
Do u want to continue.....? [y/n]
GTU 15:
Write a script which reads a text file and output the following
Count of character, words and lines.
File in reverse.
Frequency of particular word in the file.
Lower case letter in place of upper case letter.
ANS:
[mca09113@ALS ~]$ cat g15.sh
echo -e "\n Enter Filename : "
read filen
echo "--------------------------------------------------------------"
echo " MENU "
echo "--------------------------------------------------------------"
echo " a) Count the no. of char.s, words, lines."
echo " b) Display a file in reverse."
echo " c) Count the no. of occurrences of a particular word."
echo " d) Convert Lower case to UppeCase"
echo "--------------------------------------------------------------"
echo -e "\n Enter Your Choice : "
read c
case "$c" in
a) echo -e "\n Total lines,words,char.s " ; wc $filen ;;
b) rev $filen
read buf;;
c) echo -e "\nEnter word to find :"
read w
for i in `cat $filen`
do
echo $i >> f1.txt
done
echo -e "Total no. of words= \c" ;grep -c $w f1.txt
grep $w f1.txt
rm f1.txt;;
*) echo "Invalid choice"
esac
OUTPUT:
[mca09113@ALS ~]$ sh g15.sh
Enter Filename :
gtu7.sh
--------------------------------------------------------------
MENU
--------------------------------------------------------------
a) Count the no. of char.s, words, lines.
b) Display a file in reverse.
c) Count the no. of occurrences of a particular word.
d) Convert Lower case to UppeCase
--------------------------------------------------------------
Enter Your Choice :
a
Total lines,words,char.s
12 47 254 gtu7.sh
GTU 16:
Write a shell script to check whether the named user is currently logged in or not
ANS:
clear
echo -e "Enter The User name : "
read name
(who | grep $name ) &&
clear
echo "The User " $name &&
echo " Is Currently Logged In " || echo "is logged out"
OUTPUT:
[mca09113@ALS ~]$ sh g16.sh
Enter The User name :
mca09113
mca09113 pts/3 2010-12-08
The User mca09113
Is Currently Logged In
GTU 17:
Write a Script for Simple Database Management System Operation.
Database File Contains Following Fields.
EMP_NO ,EMP_NAME, EMP_ADDRESS, EMP_AGE, EMP_GENDER, EMP_DESIGNATION, EMP_BASIC_SALARY, Provide Menu Driven Facility For, VIEW RECORD BASED ON QUERY, ADD RECORD, DELETE RECORD, MODIFY RECORD., COUNT TOTAL NUMBER OF RECORDS EXIT
ANS:
[mca09113@ALS ~]$ cat 17g.sh
clear
i="y"
echo "Enter name of database "
read db
while [ $i = "y" ]
do
clear
echo "1.View the DataBase "
echo "2.View Specific Records "
echo "3.Add Records "
echo "4.Delete Records "
echo "5.Exit "
echo "Enter your choice "
read ch
case $ch in
1)cat $db;;
2)echo "Enter id :"
read id
l=`wc -l $db`
j=1
while [ $l -gt $j ]
do
$dbid=`cat $db | cut -f1 -d"," | head -n $i | tail -n 1`
if [ $id = $dbid ]
then
echo `head -n $i | tail -n 1`
break
fi
done
;;
3)echo "Enter new std id "
iread tid
echo "Enter new name:"
read tnm
echo "Enter designation "
read des
echo "Enter college name"
read college
echo "$tid $tnm $des $college">>$db;;
4)echo "Delete Records Where Id is"
read id
grep -v "$id" $db >dbs1
echo "Record is deleted"
cat dbs1;;
5)exit;;
*)echo "Invalid choice ";;
esac
echo "Do u want to continue ?"
read i
if [ $i != "y" ]
then
exit
fi
done
OUTPUT:
[mca09113@ALS ~]$ sh 17g.sh
Enter name of database
hm1
1.View the DataBase
2.View Specific Records
3.Add Records
4.Delete Records
5.Exit
Enter your choice
1
cat: hm1: Is a directory
Do u want to continue ?
GTU 18:
Write A Script To Perform Following String Operations Using Menu:
COMPARE TWO STRINGS.
JOIN TWO STRINGS.
FIND THE LENGTH OF A GIVEN STRING.
OCCURRENCE OF CHARACTER AND WORDS
E. REVERSE THE STRING.
ANS:
[mca09113@ALS ~]$ cat g18.sh
while true
do
echo ---------------------------------------
echo 1...COMPARE TWO STRINGS
echo 2...JOIN TWO STRINGS
echo 3...FIND THE LENGTH OF A GIVEN STRING
echo 4...OCCURRENCE OF CHARACTER AND WORDS
echo 5...REVERSE THE STRING
echo ---------------------------------------
echo Enter Choice:
read ch
echo ---------------------------------------
case $ch in
1)
echo Enter String1:
read str1
echo Enter String2:
read str2
if [ $str1 = $str2 ]
then
echo String is equal
else
echo String is not equal
fi
;;
2)
echo Enter String1:
read str1
echo Enter String2:
read str2
str3=$str1$str2
echo Join String: $str3
;;
3)
length=0
echo Enter String1:
read str1
length=`expr $str1 | wc -c`
length=`expr $length - 1`
echo Length: $length
;;
4)
echo Enter String:
read str
echo Enter Word to find
read word
echo $str | cat > str1.txt
grep -o $word str1.txt | cat > str2.txt
count=`grep -c $word str2.txt`
echo Count: $count
;;
5)
echo Enter String1:
read str
len=`expr $str | wc -c`
len=`expr $len - 1`
while [ $len -gt 0 ]
do
rev=`expr $str | cut -c $len`
ans=$ans$rev
len=`expr $len - 1`
done
echo Reverse String: $ans
;;
*)
echo Wrong Choice
;;
esac
echo Do u want to continue?
read ans
if [ $ans = n ]
then
exit
fi
done
OUTPUT:
[mca09113@ALS ~]$ sh g18.sh
---------------------------------------
1...COMPARE TWO STRINGS
2...JOIN TWO STRINGS
3...FIND THE LENGTH OF A GIVEN STRING
4...OCCURRENCE OF CHARACTER AND WORDS
5...REVERSE THE STRING
---------------------------------------
Enter Choice:
1
---------------------------------------
Enter String1:
jay somnath
Enter String2:
jay somnath
g18.sh: line 22: [: too many arguments
String is not equal
Do u want to continue?
n
GTU 19:
Write a script to calculate gross salary for any number of employees
Gross Salary =Basic + HRA + DA.
HRA=10% and DA= 15%.
ANS:
OUTPUT:
GTU 20:
Write a script to check whether a given string is palindrome or not.
ANS:
[mca09113@ALS ~]$ cat g20.sh
echo Enter String
read str
length=0
cnt=1
flag=0
length=`expr $str | wc -c`
length=`expr $length - 1`
temp=`expr $length / 2`
while test $cnt -le $temp
do
rev=`expr $str | cut -c $cnt`
ans=`expr $str | cut -c $length`
if [ $rev != $ans ]
then
flag=1
fi
cnt=`expr $cnt + 1`
length=`expr $length - 1`
done
if [ $flag -eq 0 ]
then
echo String is palindrome
else
echo String is not palindrome
fi
OUTPUT:
[mca09113@ALS ~]$ sh g20.sh
Enter String
jay somnath
expr: syntax error
String is palindrome
GTU 21:
Write a script to check whether a given number is palindrome or not.
ANS:
[mca09113@ALS ~]$ cat g21.sh
clear
echo "ENTER NUMBER :="
read number
rem=0
i=10
revnumber=0
for((;number>0;))
do
let revnumber=" revnumber * 10 "
let rem=" number % 10 "
let number=" number / 10 "
let revnumber=" revnumber + rem "
done
echo -e "\nREVERSE NUMBER :-\c" & echo $revnumber
OUTPUT:
[mca09113@ALS ~]$ sh g21.sh
ENTER NUMBER :=
12345
REVERSE NUMBER :-54321
GTU 22:
Write a script to display all words of a file in ascending order
ANS:
[mca09113@ALS ~]$ cat g22.sh
echo " Enter File Name"
read fname
sort $fname
or
echo -e "\n enter File name "
read fn
for i in `cat $fn`
do
echo $i >> f2.txt
done
sort f2.txt
rm f2.tx
OUTPUT:
[mca09113@ALS ~]$ sh g22.sh
Enter File Name
GTU 23:
Write a script to display all lines of a file in ascending order.
ANS:
echo " Enter File Name"
read fname
sort $fname
OUTPUT:
[mca09113@ALS ~]$ sh g23.sh
Enter File Name
myfile.txt
hi friends
how are you
thankyou
GTU 24:
Write a script to display the last modified file
ANS:
while true;
do
read -e -p "Enter Directory: " path || exit
[[ -d $path ]] && break
echo "Invalid Path, Try Again!"
done
cd $path
ls * -dpltr | grep -v '/$' | tail -n1
cd $OLDPWD
OUTPUT:
[mca09113@ALS ~]$ sh g24.sh
Enter Directory: hm1
-rw-r--r-- 1 mca09113 Domain Users 26 Aug 9
GTU 25:
Write a shell script to add the statement #include
ANS:
OUTPUT:
GTU 26:
Write a script that behaves both in interactive and non-interactive mode. When no arguments are supplied, it picks up each C program from current directory and lists the first 10 lines. It then prompts for deletion of the file. If the user supplies arguments with the script, then it works on those files only
ANS:
[mca09113@ALS ~]$ cat g26.sh
N=$#
ext=C
if test "$N" -eq "0"; then
while true; do
read -e -p "Enter Path: " path || exit
[[ -d $path ]] && break
echo "Invalid Path, Try Again!"
done
path=${path%/}
for i in $path/*.C
do
if [ "$i" != $path/'*.C' ]; then
mv "$i" "${i/.C/}".c
clear
fi
done
for i in $path/*."$ext"
do
if [ "$i" != "$path"/'*.c' ]; then
clear
echo "File is $i"
head -n10 "$i" | nl
sleep 1
rm -i "$i"
else
echo "There are no matching \"C\" files to Prompt in this directory."
sleep 2
clear=no
fi
done
if test "$clear" != "no"; then
clear
echo "Remaining C files in the Directory..."
ls -1 $path/*.c
fi
else
for i in $path/*.C
do
if [ "$i" != $path/'*.C' ]; then
mv "$i" "${i/.C/}".c
clear
fi
done
for i in $*
do
clear
i="${i/.c/}"
i="$(pwd)/$i.c"
if [ -f "$i" ]; then
echo "File name is $i"
head -n10 "$i" | nl
sleep 1
rm -i "$i"
else
echo "There is no such a file with name: \"$i\" in current working directoy"
sleep 3
fi
done
clear
echo "Remaining C files in the Directory..."
ls -1 *.c
sleep 1
fi
OUTPUT:
[mca09113@ALS ~]$ sh g26.sh
Enter Path: hm1
File is hm1/*.C
head: cannot open `hm1/*.C' for reading: No such file or directory
rm: cannot lstat `hm1/*.C': No such file or directory
Remaining C files in the Directory...
hm1/t1.c
0 comments:
Post a Comment