Академический Документы
Профессиональный Документы
Культура Документы
Write a shell script to find whether a given year(4 digits)is leap year or not.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
echo "Enter the year in 4 digits: "
read num
if [ $num -ge 1000 -a $num -le 9999 ]
then
if [ `expr $num % 4` -eq 0 ]
then
echo "The year $num is a Leap year"
else
echo "The year $num is not a Leap year"
fi
else
echo "Invalid year format"
fi
-----------------------------------------------------------------------------------
---------------------------
OUTPUT
Enter the year in 4 digits:
2000
The year 2000 is a Leap year
===========================================================
Script-6
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
echo "Enter a number: "
read num
i=1
sum=0
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
echo "Enter a number: "
read num
i=2
f=0
while [ $i -le `expr $num / 2` ]
do
if [ `expr $num % $i` -eq 0 ]
then
f=1
fi
i=`expr $i + 1`
done
if [ $f -eq 1 ]
then
echo "The number is composite"
else
echo "The number is Prime"
fi
-----------------------------------------------------------------------------------
---------------------------
OUTPUT
Enter a number:
5
The number is Prime
===========================================================
Script-8
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
echo "Enter a number: "
read c
x=$c
sum=0
r=0
n=0
while [ $x -gt 0 ]
do
r=`expr $x % 10`
n=`expr $r \* $r \* $r`
sum=`expr $sum + $n`
x=`expr $x / 10`
done
if [ $sum -eq $c ]
then
echo "It is an Armstrong Number."
else
echo "It is not an Armstrong Number."
fi
-----------------------------------------------------------------------------------
---------------------------
OUTPUT
Enter a number:
153
It is an Armstrong Number.
===========================================================
Script-9
Write a pro. to make a table for a given no.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
i=0
sum=0
echo -e "Please enter the number that you want Table:\c"
read a
for i in 1 2 3 4 5 6 7 8 9 10
do
echo -e "$a * $i =`expr $a \* $i`"
done
-----------------------------------------------------------------------------------
---------------------------
OUTPUT
Please enter the number that you want Table:10
10 * 1 =10
10 * 2 =20
10 * 3 =30
10 * 4 =40
10 * 5 =50
10 * 6 =60
10 * 7 =70
10 * 8 =80
10 * 9 =90
10 * 10 =100
===========================================================
Script-10:
Write a sccript to find a factorial of a given no.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
fact=1
ie=1
-----------------------------------------------------------------------------------
---------------------------
OUTPUT
Enter a number:10
Multilpication of 10 number is 3628800.
===========================================================
Script-11
Write a shell script to find a sum of given no and to check out to see if it is
even
or odd.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
echo "Enter a number: "
read n
sum=0
x=$n
r=0
while [ $x -gt 0 ]
do
r=`expr $x % 10`
sum=`expr $sum + $r`
x=`expr $x / 10`
done
-----------------------------------------------------------------------------------
---------------------------
OUTPUT
Enter a number:11
The sum of 11 is 2 and it is odd
===========================================================
Script-12
case $op in
"*")sum=`expr $op1 \* op2`
echo "Product is $sum";;
"+")sum=`expr $op1 + op2`
echo "Sum is $sum";;
"-")sum=`expr $op1 - $op2`
echo "Difference is $sum";;
"/")sum=`expr $op1 / $op2`
echo "Quotient is $sum";;
*)echo "Invalid Entry"
esac
echo "Do you wish to continue:(Y/N):"
read ans
done
-----------------------------------------------------------------------------------
---------------------------
OUTPUT
Enter operand1:
8
Enter operand2:
4
Enter operation:
+ for addition
- for subtraction
* for multiplication
/ for division
Enter your choice:
/
Quotient is 2
Do you wish to continue:(Y/N):
n
===========================================================
Script-13.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
echo "Enter a number: "
read n
i=1
val=0
cal=0
while [ $i -le $n ]
do
if [ `expr $i % 2` -ne 0 ]
then
val=`expr $i \* $i \* $i`
cal=`expr $cal + $val`
echo -e "+$val \c"
else
val=`expr $i \* $i`
cal=`expr $cal - $val`
echo -e "-$val \c"
fi
i=`expr $i + 1`
done
echo -e "\nSum is $cal"
-----------------------------------------------------------------------------------
---------------------------
OUTPUT
Enter a number:10
+1 -4 +27 -16 +125 -36 +343 -64 +729 -100
Sum is 1005
===========================================================
Script-14.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
echo "Enter a number: "
read num
sum=0
a=0
b=1
echo "Series is:"
while [ $sum -le $num ]
do
a=$b
b=$sum
sum=`expr $a + $b`
echo -e "$b \c"
done
echo -e "\n"
-----------------------------------------------------------------------------------
---------------------------
Enter a number:10
Series is:
0 1 1 2 3 5 8
===========================================================
Script:15.
Display series:1+4+27+256...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
echo "Enter a number: "
read num
val=1
i=1
j=1
echo "The Series is: "
while [ $i -le $num ]
do
while [ $j -le $i ]
do
val=`expr $val \* $i`
j=`expr $j + 1`
done
if [ $i -eq 1 ]
then
echo -e "$val \c"
else
echo -e "+$val \c"
fi
j=1
val=1
i=`expr $i + 1`
done
echo -e "\n"
-----------------------------------------------------------------------------------
---------------------------
OUTPUT
Enter a number: 5
The Series is: 1 +4 +27 +256 +3125
===========================================================
Script:16.
-----------------------------------------------------------------------------------
---------------------------
OUTPUT
Enter a number: 5
The Sum of 1 +4 +9 +16 +25 = 55
===========================================================
Script:17.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
if [ $# -lt 1 ]
then
echo "Invalid Argument"
else
j=0
sum=0
echo -e "The sum of \c"
for i in $*
do
if [ $j -ne -1 ]
then
echo -e "$i, \c"
sum=`expr $sum + $i`
fi
j=`expr $j + 1`
done
echo -e "= $sum \n"
fi
-----------------------------------------------------------------------------------
---------------------------
OUTPUT
The sum of 1, 2, 3, = 6
===========================================================
Script:18.
Display series:1-2+6-24+....
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
echo "Enter a number: "
read num
i=1
x=0
while [ $i -le $num ]
do
j=1
x=1
while [ $j -le $i ]
do
x=`expr $x \* $j`
j=`expr $j + 1`
done
if [ $i -eq 1 ]
then
echo -e "$x \c"
elif [ `expr $i % 2` -eq 0 ]
then
echo -e "-$x \c"
elif [ `expr $i % 2` -ne 0 ]
then
echo -e "+$x \c"
fi
i=`expr $i + 1`
done
echo -e "\n"
-----------------------------------------------------------------------------------
---------------------------
OUTPUT
Enter a number:5
1 -2 +6 -24 +120
===========================================================
Script:19.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
echo "Enter Basic Salary: "
read b
hra=0
da=0
if [ $b -gt 1500 ]
then
hra=500
da=`expr $b \* 98 / 100`
else
hra=`expr $b \* 10 / 100`
da=`expr $b \* 90 / 100`
fi
gs=`expr $b + $hra + $da`
-----------------------------------------------------------------------------------
---------------------------
OUTPUT
Enter Basic Salary:
15000
Gross Salary is: Rs.30200
Details:
Basic: Rs.15000
HRA: Rs.500
DA: Rs.14700
===========================================================
Script:20.
Write a shell script that takes two number s through keyboard and finds the
value od one number raised to the power of another.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
echo "Enter a number: "
read a
i=1
ans=1
while [ $i -le $p ]
do
ans=`expr $ans \* $a`
i=`expr $i + 1`
done
echo "Answer of $a^$p is $ans"
-----------------------------------------------------------------------------------
---------------------------
OUTPUT
Enter a number: 10
Enter Power: 3
Answer of 10^3 is 1000
===========================================================
Script - 21
Write a shell to generate the following patterns.
*
* *
* * *
* * * *
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
echo "Enter the number of Rows:"
read r
i=1
j=1
while [ $i -le $r ]
do
j=1
while [ $j -le $i ]
do
echo -e "* \c"
j=`expr $j + 1`
done
echo -e "\n"
i=`expr $i + 1`
done
-----------------------------------------------------------------------------------
---------------------------
OUTPUT
Enter the number of Rows:
5
*
* *
* * *
* * * *
* * * * *
===========================================================
Script 22
Display Following:
1
2 2
3 3 3
4 4 4
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
echo "Enter number of rows: "
read r
echo "Enter number of columns: "
read c
i=1
j=1
while [ $i -le $r ]
do
j=$c
while [ $j -ge $i ]
do
echo -e " \c"
j=`expr $j - 1`
done
j=1
while [ $j -le $i ]
do
echo -e " $i\c"
j=`expr $j + 1`
done
echo -e "\n"
i=`expr $i + 1`
done
-----------------------------------------------------------------------------------
---------------------------
OUTPUT
2 2
3 3 3
4 4 4 4
===========================================================
Script-23:
Display Following:
|_
||_
|||_
||||_
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
echo "Enter Number of Rows: "
read row
i=1
-----------------------------------------------------------------------------------
---------------------------
OUTPUT
Enter Number of Rows:
4
| _
| | _
| | | _
| | | | _
===========================================================
Script-24
Display Following:
*
**
***
**
*
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
echo "Enter Number of rows: "
read row
i=1
while [ $i -le $row ]
do
j=1
while [ $j -le $i ]
do
echo -e "* \c"
j=`expr $j + 1`
done
echo -e "\n"
i=`expr $i + 1`
done
i=`expr $row - 1`
while [ $i -ge 1 ]
do
j=1
while [ $j -le $i ]
do
echo -e "* \c"
j=`expr $j + 1`
done
echo -e "\n"
i=`expr $i - 1`
done
-----------------------------------------------------------------------------------
---------------------------
OUTPUT
Enter Number of rows:3
*
* *
* * *
* *
*
===========================================================
Script25
Display Following:
1
12
123
1234
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
echo "Enter number of Rows: "
read row
i=1
while [ $i -le $row ]
do
j=1
while [ $j -le $i ]
do
echo -e "$j \c"
j=`expr $j + 1`
done
echo -e "\n"
i=`expr $i + 1`
done
-----------------------------------------------------------------------------------
---------------------------
OUTPUT
Enter number of Rows:
5
1 2
1 2 3
1 2 3 4
1 2 3 4 5
===========================================================
SCRIPT-26
Display Following:
*
* *
* * *
* * * *
* * *
* *
*
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
echo "Enter Number of Rows: "
read row
i=1
while [ $i -le $row ]
do
j=$row
while [ $j -gt $i ]
do
echo -e " \c"
j=`expr $j - 1`
done
j=1
while [ $j -le $i ]
do
echo -e " *\c"
j=`expr $j + 1`
done
echo -e "\n"
i=`expr $i + 1`
done
i=`expr $row - 1`
while [ $i -ge 1 ]
do
j=$row
while [ $j -gt $i ]
do
echo -e " \c"
j=`expr $j - 1`
done
j=$i
while [ $j -ge 1 ]
do
echo -e " *\c"
j=`expr $j - 1`
done
echo -e "\n"
i=`expr $i - 1`
done
-----------------------------------------------------------------------------------
---------------------------
OUTPUT
Enter Number of Rows:5
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
===========================================================
Practical List-03
Script-1:
Write a shell script to read a string through keyboard and check whether it
is
palindrome or not.
***********************************************************************************
**************
echo "Enter a string: "
read s
len=`echo $s | wc -c`
while [ $len -gt 0 ]
do
st=`echo $s | cut -c $len`
str=$str$st
len=`expr $len - 1`
done
if [ $str = $s ]
then
echo "String Palindrome"
else
echo "string is not palindrome"
fi
***********************************************************************************
**************
OUTPUT:
Enter a string:
madam
String Palindrome
***********************************************************************************
**************
***********************************************************************************
***********************************************************************************
****************************
***********************************************************************************
**************
Script-2:
Write a shell script that combine two files in the third file horizontally and
vertically.
***********************************************************************************
**************
echo "Enter Filename1: "
read f1
echo "Enter Filename2: "
read f2
***********************************************************************************
**************
OUTPUT:
Enter Filename1:
f1
Enter Filename2:
f2
Script-3: Write a shell script that displays all the processes in the systemby
every 30 seconds, but five times.
***********************************************************************************
**************
i=1
while [ $i -le 5 ]
do
ps
sleep 30
i=`expr $i + 1`
done
***********************************************************************************
**************
OUTPUT:
Script-4 : Write a shell script to remove the zero sized file from the current
directory
***********************************************************************************
**************
for i in `ls`
do
if [ -s $i ]
then
echo "$i - Non-empty file"
else
echo "$i - Empty file Deleted"
rm $i
fi
done
***********************************************************************************
**************
OUTPUT:
05 - Non-empty file
20 - Non-empty file
h - Empty file Deleted
a - Empty file Deleted
r - Empty file Deleted
***********************************************************************************
**************
***********************************************************************************
***********************************************************************************
****************************
***********************************************************************************
**************
Script-5: Write a shell script to change the suffix of all your *.txt files to .dat
***********************************************************************************
**************
***********************************************************************************
**************
OUTPUT:
Before->
01 05 09 13 17 21 25 29 33 37 db36 f2 status.dat
02 06 10 14 18 22 26 30 34 38 db37 ff test
03 07 11 15 19 23 27 31 35 39 db38 file vfile
04 08 12 16 20 24 28 32 36 a.txt f1 project.dat wordfile
-------
After->
01 05 09 13 17 21 25 29 33 37 db36 f2 status.dat
02 06 10 14 18 22 26 30 34 38 db37 ff test
03 07 11 15 19 23 27 31 35 39 db38 file vfile
04 08 12 16 20 24 28 32 36 a.dat f1 project.dat wordfile
-------
***********************************************************************************
**************
***********************************************************************************
***********************************************************************************
****************************
***********************************************************************************
**************
Script-6: Write a shell script that appends the PID of current shell to the
filenames of all files having extension txt.
***********************************************************************************
**************
***********************************************************************************
**************
OUTPUT:
Before->
01 05 09 13 17 21 25 29 33 37 db36 f2 status.dat
02 06 10 14 18 22 26 30 34 38 db37 ff test
03 07 11 15 19 23 27 31 35 39 db38 file vfile
04 08 12 16 20 24 28 32 36 a.txt f1 project.dat wordfile
-------
After->
01 05 09 13 17
21 25 29 33 37 db36 f2 status.dat
02 06 10 14 18
22 26 30 34 38 db37 ff test
03 07 11 15 19
23 27 31 35 39 db38 file vfile
04 08 12 16 20
24 28 32 36 4289a.txt f1 project.dat wordfile
----------
***********************************************************************************
**************
***********************************************************************************
***********************************************************************************
****************************
***********************************************************************************
**************
Script-7: Write a shell script to delete all spaces from a given file
***********************************************************************************
**************
echo "Enter the Filename: "
read fname
***********************************************************************************
**************
OUTPUT:
***********************************************************************************
**************
***********************************************************************************
**************
***********************************************************************************
**************
Script-8: Write a shell Script that displays the user information from /etc/passwd
for a given user as follows:
User Login Name:
User Id:
User Gourp Id:
If no argument is passed, display or the current user
***********************************************************************************
**************
if [ $# -ne 1 ]
then
user=`who am i | tr -s " " | cut -d " " -f 1`
else
user=$1
fi
cat /etc/passwd | grep $user > file
lname=`cat file | cut -d ":" -f 1`
uid=`cat file | cut -d ":" -f 3`
gid=`cat file | cut -d ":" -f 4`
echo "User Login name: $lname"
echo "User Id: $uid"
echo "User Group id: $gid"
***********************************************************************************
**************
OUTPUT:
User Login name: s92
User Id: 2535
User Group id: 1050
***********************************************************************************
***********************************************************************************
***********************************************************************************
******************************************
***********************************************************************************
**************
Script-9: Write a shell script that will change the current working directory to
given directory.
For example if argument passed is /d1/d2/d3/d4/d5 then at the end of the script,
the user should be in the d5 directory.
***********************************************************************************
**************
***********************************************************************************
**************
OUTPUT:
sh 09 /d1/d3
01 06 11 16 21 26 31 36 d1 f2 test
02 07 12 17 22 27 32 37 db36 ff vfile
03 08 13 18 23 28 33 38 db37 file wordfile
04 09 14 19 24 29 34 39 db38 project.dat
05 10 15 20 25 30 35 4289a.txt f1 status.dat
***********************************************************************************
**************
***********************************************************************************
***********************************************************************************
****************************
***********************************************************************************
**************
***********************************************************************************
**************
if [ $# -eq 2 ]
then
x=`cal $2 07 | wc -l`
x=`expr $x - 1`
cal $2 07 | tail -$x > file
cat file | grep -w "$1" > f
j=0
for i in `cat f`
do
j=`expr $j + 1`
if [ $i = $1 ]
then
break
fi
done
if [ $j -gt 1 -a $j -lt 7 ]
then
echo "Day is Weekday"
else
echo "Day is Weekend"
fi
else
echo "Invalid arguments"
echo "Usage: 10 day month"
fi
***********************************************************************************
**************
OUTPUT:
[s92@popeye TW3]$ sh 10 11 07
Day is Weekday
***********************************************************************************
**************
***********************************************************************************
***********************************************************************************
****************************
***********************************************************************************
**************
Script-11: Write a shell script to search for a given word in all files given as
the arguments on the command line.
On the first occurence of the the word, display the filename and terminate the
script.
***********************************************************************************
**************
if [ $# -lt 1 ]
then
echo "Invalid Argument"
else
echo "Enter a word: "
read word
x=0
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
fi
***********************************************************************************
**************
OUTPUT:
***********************************************************************************
**************
***********************************************************************************
***********************************************************************************
****************************
***********************************************************************************
**************
Script-12: Write a shell script that displays the name of the smallest file in the
current working directory.
It should display all the information about that file in the following format:
Filename:
Owner:
Size:
No. of Links:
***********************************************************************************
**************
***********************************************************************************
**************
OUTPUT:
Filename : 4289a.txt
Owner : s112
Size : 0
Link : 1
***********************************************************************************
**************
***********************************************************************************
***********************************************************************************
****************************
***********************************************************************************
**************
Script-13: Write a shell script that displays only specified lines of the given
files as follows
showlines n file -> displays first n lines of file
showlines n1 n2 file -> displays line numbers then display error
if no. of arguments are not in the correct number thendisplay error
message
***********************************************************************************
**************
if [ $# -eq 2 ]
then
cat $2 | head -$1
elif [ $# -eq 3 ]
then
cat $3 | head -$2 | tail -$1
else
echo "Invalid Arguments"
fi
***********************************************************************************
**************
OUTPUT:
***********************************************************************************
**************
***********************************************************************************
***********************************************************************************
****************************
***********************************************************************************
**************
Script-14: A file Wordfile contains list of words. Write a shell script to search
for those words which are in Wordfile,
in all the files that are passed as an argument to the program. The name of
Wordfile should also be passed as an argument.
***********************************************************************************
**************
if [ $# -ge 2 ]
then
c=0
for i in `echo $*`
do
if [ $c -eq 1 ]
then
for j in `cat $1`
do
x=`cat $i | grep -wc "$j"`
if [ $x -eq 1 ]
then
echo "$j word found in $i"
fi
done
fi
c=1
done
else
echo "Usage: 14 wordfile file1 file2 ... file9"
fi
***********************************************************************************
**************
OUTPUT:
***********************************************************************************
**************
***********************************************************************************
***********************************************************************************
****************************
***********************************************************************************
**************
***********************************************************************************
**************
OUTPUT:
Enter a string:
life
i. Length of string
ii.Reverse of the string
iii. copy one string into another.
Enter your choice:
ii
The Reverse of string is : efil
***********************************************************************************
**************
***********************************************************************************
***********************************************************************************
****************************
***********************************************************************************
**************
Script-16: Write a shell script to display the following menu for a particular
file:
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
c=`echo $s | cut -c $j`
case $c in
[A-Z])c=`echo $c | tr [A-Z] [a-z]`;;
[a-z])c=`echo $c | tr [a-z] [A-Z]`;;
esac
str=$str$c
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
***********************************************************************************
**************************************************************************
OUTPUT:
Enter the Filename:
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.
Enter your choice :
i
File in ascending order:
asdjlasjdsd;jas
askdjaskljdas
is
thdjasdjlasd
unix
***********************************************************************************
**************************************************************************
Script-17: Write a shell script to display the following menu for a particular
file:
i.Count the no. of characters,words,lines.
ii.Count no. of words that has exactly five characters and also display them
***********************************************************************************
**************
***********************************************************************************
**************
OUTPUT:
***********************************************************************************
**************
***********************************************************************************
***********************************************************************************
****************************
***********************************************************************************
**************
Script-18: Write a shell script to accept file and directory as command line
arguments and remove that file from the specified directory.
***********************************************************************************
**************
if [ $# -eq 2 ]
then
cd
if [ -d $2 ]
then
x=`ls $2 | grep -wic "$1"`
if [ $x -eq 1 ]
then
rm -f `pwd`/$2/$1
echo "File $1 removed"
else
echo "File not found"
fi
else
echo "Argument passed is not a directory"
fi
else
echo "Invalid option. Usage: 18 file directory"
fi
***********************************************************************************
**************
OUTPUT:
[s92@popeye TW3]$ sh 18 m 04
Argument passed is not a directory
[s92@popeye TW3]$ sh 18 04 m
File 04 removed
***********************************************************************************
**************
***********************************************************************************
***********************************************************************************
****************************
***********************************************************************************
**************
Script-19: Write a shell script to get a group name and display all the users who
are group member of it.
Also display how many of them are currently active.
***********************************************************************************
**************
***********************************************************************************
**************
OUTPUT:
***********************************************************************************
**************
***********************************************************************************
***********************************************************************************
****************************
***********************************************************************************
**************
Script-20: Write a shell script to get all files of home directory and rename them
if their names start with c
Newname=oldname111
***********************************************************************************
**************
cd
y="111"
c="c"
for i in `ls`
do
if [ -d $i ]
then
x=`echo $i | cut -c 1`
if [ $x = $c ]
then
mv -f $i $i$y
fi
fi
done
***********************************************************************************
**************
OUTPUT:
Previously->
05 add_K dataentry finduser menudriven t1 usercount
06 b dirname g mul test vowel
09 c display list newfile tw1 while_construct
1 calen e2 log pfile TW1
11 check empinfo log2 print tw-2
*234.txt cin loggedin print123 TW2
a d f2.dat loggein2 printseries tw3
a+b d4 file m s TW3
add data filemenu Maildir sub *.txt
After->
05 add_K dataentry finduser menudriven t1 usercount
06 b dirname g mul test vowel
09 c display list newfile tw1 while_construct
1 calen e2 log pfile TW1
11 check empinfo log2 print tw-2
*234.txt cin111 f loggedin print123 TW2
a d f2.dat loggein2 printseries tw3
a+b d4 file m s TW3
add data filemenu Maildir sub *.txt
***********************************************************************************
**************
***********************************************************************************
**************
***********************************************************************************
**************
***********************************************************************************
**************
Script-21: Write a shell script to read a string through keyboard and replace that
string in the file with new word.
***********************************************************************************
**************
while [ $i -le $l ]
do
s=`cat file | head -$i | tail -1`
ss=""
for j in `echo $s`
do
if [ $j = $str ]
then
j=`echo $r`
fi
ss=$ss$sp$j
done
echo $ss >> f
i=`expr $i + 1`
done
cat f > file
rm -f f
***********************************************************************************
**************
OUTPUT:
Enter a string:
hardik
Enter the word to replace with:
HARDIK
***********************************************************************************
**************
***********************************************************************************
***********************************************************************************
****************************
***********************************************************************************
**************
Script-22: Write a shell script that displays the directory information in the
following format-
Filename Size Date Protection Owner
***********************************************************************************
**************
x=`ls -l | wc -l`
i=2
echo -e "Filename\t\tSize\tDate\tProtection\tOwner\n"
while [ $i -le $x ]
do
s=`ls -l | head -$i | tail -1 | tr -s " "`
fn=`echo $s | cut -d " " -f 9`
si=`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 -e "$fn\t\t\t$si\t$d1 $d2\t$p\t$o"
i=`expr $i + 1`
done
***********************************************************************************
**************
OUTPUT:
***********************************************************************************
**************
Script-23: Write a shell script to display the following menu:
i. Change the system prompt to username + current date
ii. No. of ordinary file in the current directory
iii. No. of files which are not accessible by others or group
iv. Display five largest file in the current directory
v. Display all the hidden files in the current directory
***********************************************************************************
**************
echo "Enter : "
echo "i. Change the system prompt to username + current date"
echo "ii. No. of ordinary file in the current directory"
echo "iii. No. of file which are not accessible by others or group"
echo "iv. Display five largest file in the current directory"
echo "v. Display all hidden files in current directory"
echo "Please enter your choice: "
read c
case "$c" in
i)x=`date`
PS1="$LOGNAME $x";;
ii)k=0
for i in `ls`
do
if [ -f $i ]
then
k=`expr $k + 1`
fi
done
echo "Number of ordinary files are: $k";;
iii)x=`ls -l | grep -wc "....\-\-\-\-\-\-"`
echo "No. of files which are not accessible by others or group are: $x";;
iv) echo "The Five largest files in the current directory."
ls -Sl | tr -s " " | head -6 | cut -d " " -f 9;;
v) echo "All hidden files in the current directory are: "
ls -a | tr -s " " | grep -w ^[.] | cut -d " " -f 9;;
*)echo "Invalid option"
esac
***********************************************************************************
**************
OUTPUT:
Enter :
i. Change the system prompt to username + current date
ii. No. of ordinary file in the current directory
iii. No. of file which are not accessible by others or group
iv. Display five largest file in the current directory
v. Display all hidden files in current directory
Please enter your choice:
ii
Number of ordinary files are: 51
***********************************************************************************
**************
***********************************************************************************
**************
***********************************************************************************
**************
***********************************************************************************
**************
Script-24: Write a shell script that takes filenames and comment on their size.
Display appropriate message as follows:
***********************************************************************************
**************
if [ $# -gt 0 ]
then
for i in `echo $*`
do
w=`cat $i | wc -w`
s=`ls -l $i | tr -s "" | cut -d " " -f 5`
l=`cat $i | wc -l`
if [ $s -eq 0 ]
then
echo "$i - File is empty"
elif [ $w -gt 0 -a $w -lt 100 ]
then
echo "$i - File is small"
elif [ $w -ge 100 -a $w -le 1000 ]
then
echo "$i - File is Large"
else
echo "$i - File is Huge"
fi
done
else
echo "Invalid argument"
echo "Usage: 24 File1 file2....."
fi
***********************************************************************************
**************
OUTPUT:
[s92@popeye TW3]$ sh 24
Invalid argument
Usage: 24 File1 file2.....
[s92@popeye TW3]$ sh 24 file
file - File is small
***********************************************************************************
**************
***********************************************************************************
***********************************************************************************
****************************
***********************************************************************************
**************
***********************************************************************************
**************
cal
x=`who am i | tr -s " " | cut -d " " -f 5`
x=`echo $x | cut -d ":" -f 1`
if [ $x -lt 12 ]
then
echo "Good Morning"
elif [ $x -ge 12 -a $x -lt 17 ]
then
echo "Good Afternoon"
else
echo "Good Evening"
fi
echo "Username: $LOGNAME"
echo "HOME directory: $HOME"
tn=`who am i | tr -s " " | cut -d " " -f 6`
echo "Terminal name: $tn"
tn=`who am i | tr -s " " | cut -d " " -f 2`
echo "Machine name: $tn"
n=`who | wc -l`
echo "No. of user logged in : $n they are as follows: "
who | tr -s " " | cut -d " " -f 1
***********************************************************************************
**************
OUTPUT:
February 2007
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28
Good Morning
Username: s92
HOME directory: /home/sybcas112
Terminal name: (aesics84)
Machine name: pts/7
No. of user logged in : 7 they are as follows:
s81
s92
s72
s66
s106
s62
s84
***********************************************************************************
**************
***********************************************************************************
***********************************************************************************
****************************
***********************************************************************************
**************
Script-26: write a shell script to display the following menu for a particular
file:
i. Count the no. of words that are starting with '0' and ending with '*' and
also display them.
ii. Count the no. of occurences of a particular word.
***********************************************************************************
**************
***********************************************************************************
**************
OUTPUT:
Enter the file name:
file
Enter:
i. Count the no. of words thar are starting with '0' and ending with '*' and also
display them
ii. Count the no. of occurences of a particular word.
Enter your choice:
ii
Enter the word to count occurences :
HARDIK
Number of times the word HARDIK is repeated: 2
***********************************************************************************
**************
***********************************************************************************
***********************************************************************************
****************************
***********************************************************************************
**************
***********************************************************************************
**************
***********************************************************************************
**************
OUTPUT:
Enter:
i. Concatenation of two strings
ii. Rename a file
iii. Delete a file
iv. Edits a file
Please enter your choice:
i
Enter string1: jack
Enter string2: back
Concated string is jackback
***********************************************************************************
**************
***********************************************************************************
***********************************************************************************
****************************
***********************************************************************************
**************
Script-28: Write a shell script to accept 2 filenames and check i both exist then
append the contents of the second file into the first file.
***********************************************************************************
**************
***********************************************************************************
**************
OUTPUT:
Enter filename1: f1
Enter filename2: f2
[s92@popeye TW3]$ cat f1
HIHELLP
ASDSAD
Welcome
***********************************************************************************
***********************************************************************************
*****************************************
Script-29: write a shell script to get user name through keyboard. If that user
exists then display all information of that user.
Also check whether that user is active or not.
***********************************************************************************
***********************************************************************************
*****************************************
***********************************************************************************
**************************************************************************
OUTPUT:
***********************************************************************************
***********************************************************************************
*
Script-30: Write a shell script to test whether a user is available or not if yes
then display its home directory and group name.
***********************************************************************************
**************************************************************************
***********************************************************************************
***********************************************************************************
*
OUTPUT:
***********************************************************************************
***********************************************************************************
*
***********************************************************************************
**************************************************************************
OUTPUT:
Enter :
i. Change the system prompt to the current directory
ii. No. of executable files in the directory
iii. Display last modified file in the current directory
iv. Display all subdirectories in current directory
v. Display all attributes of the current directory
Please enter your choice:
v
Attributes of current directory are: drwxr-xr-x
***********************************************************************************
**************************************************************************
Script-33: Write a shell script that
i. changes text to uppercase
ii. Find the reverse of the given number.
***********************************************************************************
***********************************************************************************
*
***********************************************************************************
***********************************************************************************
*
OUTPUT:
Enter :
i. Change text to uppercase
ii. Find reverse of given number
Enter your choice :
i
Enter a string:
hardIk
Converted to uppercase: HARDIK
***********************************************************************************
**************************************************************************
Script-34: Write a shell script to display the calender in the following manner:
***********************************************************************************
***********************************************************************************
*********************
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 argument Usage: 34 m1,m2 or 34 m1-m2"
fi
***********************************************************************************
***********************************************************************************
***************************************************
OUTPUT:
July 7
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
[s92@popeye TW3]$ sh 34 6-7
June 7
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
July 7
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
***********************************************************************************
***********************************************************************************
*************************************************************
***********************************************************************************
***********************************************************************************
*
***********************************************************************************
***********************************************************************************
*************************************************************
OUTPUT:
Enter :
i. Display names of all the users currently logged in
ii. Display the names of the members of a particular group
iii. Display the names of all the who are in the system
iv. Exit
Please enter your choice:
ii
Enter the name of the group:
student
Members of the group faculty are:
HARDIK
himali
nimisha
jinali
chintan
dishant
dhaval
parth
jill
minti
nitesh
kush
aashil
nauka
archit
kushal
nidhi
parth
***********************************************************************************
**************************************************************************
Script-36: Create file which contains information about items in following manner.
Itemname | item code | item price
Write a shell script which allows you to purchse any item from item list and
generates
a printout of a bill.
***********************************************************************************
***********************************************************************************
*
OUTPUT:
OUTPUT:
Enter :
i. To add new employee in file
ii. To get information of any employee by entering name
iii. To display all employees whose basic pay is between 1000 to 3999
Enter your choice:
ii
Enter the name of employee: hardik
Name: hardik
Surname: shah
Year: 2007
Address: 28,Hardi Park Sco,Naranpura
Basic: 5000
Script-38: Write a shell script for simple database management system operation
.Database file contains following fields:
Empno,emp_name,address_emp,emp_age ,emp_designation,emp_salary.
Provide a menu driven facility for
i. View Data
ii. View Record
iii. Add Record
iv. Delete Record
v. Exit
OUTPUT:
Enter :
i. View Data:
ii. View Record
iii. Add Record
iv. Delete Record
v. Exit
Enter your choice:
i
Emp Code: 001
Name: ABC
Address: Ahmedabad
Age: 23
Designation: Manager
Salary: 10000
Script-39: Write a shell script to accept employee name from the user and display
an appropriate message. Assume that an employee is working on a sinle
project. Employee project details are stored in a file project.dat. The
details of the work done on the assigned
Project by the employee are in a file status.dat
The structure:
Project.cat Status.dat
----------- ------------
Emp code Empcode
Empname Project code
Project code Daysworked
Duration
If the duration of the project is the same as the number of days worked on
the project, then display the message <Empname>. Today is the last day to
finish the
project and if the number of days are <= 30, then display the
message<Empname>,Hurry!
Only <days> are remaining to finish your project. If the number of days is
>30 then display the message <Empname>, schedule yourself. You
still have <days> to finis your work.