usn: subjectcode:13mca17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · web view1a. write a...
TRANSCRIPT
![Page 1: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/1.jpg)
USN: SUBJECTCODE:13MCA17
1a. Write a shell script that takes a valid directory name as an argument
and recursively descend all the sub-directories, finds the maximum length
of any file in that hierarchy and writes this maximum value to the standard
output
SOURCE CODE
echo "Enter directory name"
read dir1
if [ ! -d $dir1 ];then
echo "Directory not found"
exit
fi
large=0;
for file1 in `find $dir1 -type f`
do
size1=`stat -c %s $file1`
echo "size of the file $file1 is $size1"
if [ $size1 -gt $large ];then
large=$size1
lar_file=$file1
fi
done
echo "Largest file is:$lar_file"
echo "And its size is:$large"
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 2: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/2.jpg)
USN: SUBJECTCODE:13MCA17
Output 1:
[reddy@localhost ~]$ sh 1a.sh
enter the valid directory name:
ram
enter a valid directory name!!!!
Output 2:
[reddy@localhost ~]$ sh 1a.sh
enter the valid directory name:
kanasu
the file kanasu/1a.sh size is 392
the file kanasu/file2 size is 49
the file kanasu/file1 size is 49
the file kanasu/file3 size is 43
the file kanasu/file4 size is 20
the large size of file kanasu/file4 is 392
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 3: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/3.jpg)
USN: SUBJECTCODE:13MCA17
1b. Write a shell script that accepts a path name and creates all the components in that path name as directories. For example, if the script is named mpc, then the command mpc a/b/c/d should create directories a, a/b, a/b/c, a/b/c/d.
SOURCE CODE
if [ $# -lt 1 ];then
echo "NO arguments passed"
exit
else
echo $1 | tr '/' ' '>tmp_file
for i in `cat tmp_file`
do
mkdir $i
cd $i
done
echo "All directories have been created"
echo "To view all directories execute ls -R command"
fi
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 4: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/4.jpg)
USN: SUBJECTCODE:13MCA17
Output1:
[reddy@localhost kanasu]$ sh 1b.sh
NO arguments passed
Output2:
[reddy@localhost kanasu]$ sh 1b.sh a/b/c/d
All directories have been created
To view all directories execute ls -R command
[reddy@localhost kanasu]$ ls -R
.:
1a.sh 1b.sh a file1 file2 file3 file4 tmp_file
./a:
b
./a/b:
c
./a/b/c:
d
./a/b/c/d:
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 5: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/5.jpg)
USN: SUBJECTCODE:13MCA17
2a. Write a shell script that accepts two file names as arguments ,checks if
the permissions for these files are identical and if the permissions are
identical, output common permissions and otherwise output each file name
followed by its permissions.
SOURCE CODE
if [ $# -eq 0 ];then
echo "No arguments passed"
exit
fi
if [ $# -lt 2 ] || [ $# -gt 2 ];then
echo "Please enter proper input"
exit
fi
if [ ! -e $1 ] || [ ! -e $2 ];then
echo "File does not exists"
exit
fi
p1=`ls -l $1 | cut -d " " -f1`
p2=`ls -l $2 | cut -d " " -f1`
if [ $p1 == $p2 ];then
echo "Permissions of both the files are identical"
echo "Identical permissions are:$p1"
else
echo "Permissions of the files are not identical"
echo "Permissions of file one are:$p1"
echo "Permissions of file two are:$p2"
fi
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 6: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/6.jpg)
USN: SUBJECTCODE:13MCA17
Output1:
[reddy@localhost kanasu]$ sh 2a.sh
No arguments passed
Output2:
[reddy@localhost kanasu]$ sh 2a.sh file1 file2
Permissions of both the files are identical
Identical permissions are:-rw-rw-r--.
Output3:
[reddy@localhost kanasu]$ sh 2a.sh file1 file5
Permissions of the files are not identical
Permissions of file one are:-rw-rw-r--.
Permissions of file two are:-rwxrwxrwx.
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 7: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/7.jpg)
USN: SUBJECTCODE:13MCA17
2b. Write a shell script which accepts valid log-in names as arguments and
prints their corresponding home directories,if no argumrnts are
specified,print a suitable error message.
SOURCE CODE
if [ $# -eq 0 ];then
echo "No argument in command line"
exit
else
for file in $*
do
if grep $file /etc/passwd
then
x=`grep $file /etc/passwd | cut -d ":" -f 6`
echo "The valid username is :$x"
else
echo "You have not entered a valid username"
fi
done
fi
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 8: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/8.jpg)
USN: SUBJECTCODE:13MCA17
Output1:
[reddy@localhost kanasu]$ sh 2b.sh
No argument in command line
Output2:
[reddy@localhost kanasu]$ sh 2b.sh kanasu
You have not entered a valid username
Output3:
[reddy@localhost kanasu]$ sh 2b.sh reddy
reddy:x:500:500:Hanumantha Reddy:/home/reddy:/bin/bash
The valid username is :/home/reddy
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 9: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/9.jpg)
USN: SUBJECTCODE:13MCA17
3a. Create a script file called file-properties that reads a file name entered
and outputs its properties.
SOURCE CODE
echo "Enter a file:"
read f1
if [ ! -e $f1 ];then
echo "File not exist"
exit
else
a=`ls -l $f1 | cut -d " " -f1`
b=`ls -l $f1 | cut -d " " -f2`
c=`ls -l $f1 | cut -d " " -f3`
d=`ls -l $f1 | cut -d " " -f4`
e=`ls -l $f1 | cut -d " " -f5`
f=`ls -l $f1 | cut -d " " -f 6-8`
g=`ls -l $f1 | cut -d " " -f9`
echo "File permissions are:$a"
echo "File link number is/are:$b"
echo "File owner is:$c"
echo "File group owner is:$d"
echo "File size is:$e"
echo "File modification date is:$f"
echo "File name is:$g"
fi
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 10: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/10.jpg)
USN: SUBJECTCODE:13MCA17
Output1:
[reddy@localhost kanasu]$ sh 3a.sh
Enter a file:
file1
File permissions are:-rw-rw-r--.
File link number is/are:1
File owner is:reddy
File group owner is:reddy
File size is:49
File modification date is:Dec 25 14:59
File name is:file1
Output2:
[reddy@localhost kanasu]$ sh 3a.sh
Enter a file:
output1
File not exist
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 11: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/11.jpg)
USN: SUBJECTCODE:13MCA17
3b. Write shell script to implement terminal locking (similar to the lock
command). It should prompt the user for a password. After accepting the
password entered by the user , it must prompt again for the matching
password for confirmation and if match occurs, it must lock the keyword
until a matching password is entered again by the user , Note that the
script must be written to disregard BREAK, control-D. No time limit need
be implemented for the lock duration.
SOURCE CODE
stty -echo
echo "Enter the Password:"
read pass1
echo "Confirm the Password:"
read pass2
if [ $pass1 == $pass2 ];then
echo "The terminal is locked"
while true
do
echo "Enter the Password:"
read pass3
if [ $pass3 == $pass2 ];then
echo "The terminal is Unlocked"
stty echo
exit
else
echo "Try again with correct Password"
fi
done
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 12: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/12.jpg)
USN: SUBJECTCODE:13MCA17
else
echo "Both the passwords are not matching"
fi
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 13: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/13.jpg)
USN: SUBJECTCODE:13MCA17
Output1:
[reddy@localhost kanasu]$ sh 3b.sh
Enter the Password:
Confirm the Password:
The terminal is locked
Enter the Password:
The terminal is Unlocked
Output2:
[reddy@localhost kanasu]$ sh 3b.sh
Enter the Password:
Confirm the Password:
The terminal is locked
Enter the Password:
Try again with correct Password
Enter the Password:
The terminal is Unlocked
Output3:
[reddy@localhost kanasu]$ sh 3b.sh
Enter the Password:
Confirm the Password:
Both the passwords are not matching
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 14: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/14.jpg)
USN: SUBJECTCODE:13MCA17
4a. Write a shell script that accept one or more filenames as argument and
convert them to uppercase, provided they exist in current directory.
SOURCE CODE
if [ $# -eq 0 ];then
echo "No arguments passed in command line"
exit
else
for arg in $*
do
if [ -f $arg ];then
newfile=`echo $arg | tr "[a-z]" "[A-Z]"`
if [ -f $newfile ];then
echo "$arg file already existS"
else
echo "$arg converted into uppercase as:$newfile"
fi
else
echo "$arg file does not exists"
fi
done
fi
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 15: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/15.jpg)
USN: SUBJECTCODE:13MCA17
Output1:
[reddy@localhost kanasu]$ sh 4a.sh
No arguments passed in command line
Output2:
[reddy@localhost kanasu]$ sh 4a.sh file1 file2
file1 converted into uppercase as:FILE1
file2 converted into uppercase as:FILE2
Output3:
[reddy@localhost kanasu]$ sh 4a.sh file1 file2 file3 file4
file1 converted into uppercase as:FILE1
file2 converted into uppercase as:FILE2
file3 converted into uppercase as:FILE3
file4 converted into uppercase as:FILE4
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 16: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/16.jpg)
USN: SUBJECTCODE:13MCA17
4b. Write a shell script that displays all the links to a file specified as the
first argument to the script. The second argument, which is optional, can
be used to specify in which the search is to begin. If this second is not
present, the search is to begin in current working directory. IN either case,
the starting directory as well as all its subdirectories at all levels must be
searched. The script need not include any error checking.
SOURCE CODE
if [ $# -eq 2 ]
then
if [ -d $2 ]
then
cd $2
n=0
x=`ls -il $1|cut -d " " -f1`
for link in `find . -inum $x`
do
echo "$link"
((n=n+1))
done
else
echo "$2 is not a directory"
fi
echo "In `pwd` directory the number of links $n"
elif [ $# -eq 1 ]
then
n=0
x=`ls -il $1|cut -d " " -f1`
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 17: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/17.jpg)
USN: SUBJECTCODE:13MCA17
for link in `find . -inum $x`
do
echo "$link"
done
fi
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 18: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/18.jpg)
USN: SUBJECTCODE:13MCA17
Output1:
[reddy@localhost kanasu]$ sh 4b.sh file1
./file1
Output2:
[reddy@localhost kanasu]$ ln file1 temp
[reddy@localhost kanasu]$ sh 4b.sh file1
./file1
./temp
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 19: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/19.jpg)
USN: SUBJECTCODE:13MCA17
5a. Write a shell script that accepts as filename as argument and display its
creation time if file exist and if it does not send output error message.
SOURCE CODE
if [ $# -eq 0 ];then
echo "No arguments passed"
exit
fi
if [ ! -e $1 ];then
echo "File does not exists"
exit
else
x=`ls -lu $1 | cut -d " " -f 5-8`
echo "File creation time is:$x"
fi
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 20: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/20.jpg)
USN: SUBJECTCODE:13MCA17
Output1:
[reddy@localhost kanasu]$ sh 5a.sh
No arguments passed
Output2:
[reddy@localhost kanasu]$ sh 5a.sh file6
File does not exists
Output3:
[reddy@localhost kanasu]$ sh 5a.sh file1
File creation time is:49 Dec 25 15:38
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 21: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/21.jpg)
USN: SUBJECTCODE:13MCA17
5b. Write a shell script to display the calendar for current month with
current date replaced by * or ** depending on whether the date has one
digit or two digits.
SOURCE CODE
a=`date +%e`
if [ $a -lt 10 ];then
cal | sed "s/$a/*/"
else
cal | sed "s/$a/**/"
fi
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 22: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/22.jpg)
USN: SUBJECTCODE:13MCA17
Output1:
[reddy@localhost kanasu]$ sh 5b.sh
December 2013
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 **
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Output2:
[reddy@localhost kanasu]$ sh 5b.sh
December 2013
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
* 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 23: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/23.jpg)
USN: SUBJECTCODE:13MCA17
6a. Write a shell script to find a file/s that matches a pattern given as command line argument in the home directory, display the contents of the file and copy the file into the directory ~/mydir
SOURCE CODE
d=`ls -x $* | tr -s " " " "`
echo " The Contents of file are "
for x in $d
do
cat $x
cp $x ~/mydir
echo " $x Copied "
done
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 24: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/24.jpg)
USN: SUBJECTCODE:13MCA17
Output:
[reddy@localhost kanasu]$ sh 6a.sh reddy reddy1
The Contents of file are
i am from belgaum
reddy Copied
i am hanumantha reddy
reddy1 Copied
[reddy@localhost kanasu]$ vi reddy
[reddy@localhost kanasu]$ vi reddy1
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 25: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/25.jpg)
USN: SUBJECTCODE:13MCA17
6b. Write a shell script to list all the files in a directory whose filename is at
least 10 characters.(use expr command to check the length).
SOURCE CODE
for x in ‘ls’
do
len=`expr length $x`
if [ $len –ge 10 ] ; then
echo “ expr length $x “ $x
fi
done
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 26: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/26.jpg)
USN: SUBJECTCODE:13MCA17
Output1:
[reddy@localhost kanasu]$ sh 6b.sh
No arguments in command line
Output2:
[reddy@localhost kanasu]$ sh 6a.sh file1
File exist:file1
Content of the copied file are
Reddy
Manjunath
Output3:
[reddy@localhost kanasu]$ sh 6a.sh file9
File does not exists
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 27: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/27.jpg)
USN: SUBJECTCODE:13MCA17
7a. Write a shell script that gets executed displays the message either
“Good Morning” or “Good Afternoon” or “Good Evening” depending
upon time at which the user logs in.
SOURCE CODE
time=`who am I | tr -s ‘ ‘ | cut -d ” ” -f 4 | cut -c 1,2`
if [ $time -le 12 ] ; then
echo “Good Morning $LOGNAME”
elif [ $time -gt 12 -a $time -lt 16 ] ; then
echo “Good Afternoon $LOGNAME”
else
echo “Good Evenini $LOGNAME”
fi
Output1:
[reddy@localhost kanasu]$ sh 7a.sh
“Good Evenining reddy”
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 28: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/28.jpg)
USN: SUBJECTCODE:13MCA17
7b. Write a shell script that accept a list of filenames as its argument, count and reportOccurrence of each word that is present in the first argument file on other argument Files.
SOURCE CODE
if [ $# -lt 2 ]
then
echo "Invalid number of arguments.Enter atleast two arguments"
exit
fi
str=` cat $1 | tr '\n' ' ' `
set $*
shift
for i in $*
do
echo "file name" $i
echo "---------"
for a in $str
do
echo "word=$a", count=` grep -c "$a" $i `
done
done
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 29: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/29.jpg)
USN: SUBJECTCODE:13MCA17
Output1:
[reddy@localhost kanasu]$ sh 7b.sh aa bb
file name bb
---------
word=a, count=2
word=b, count=2
word=c, count=2
word=d, count=2
word=a, count=2
word=b, count=2
word=c, count=2
word=d, count=2
Output2:
[reddy@localhost kanasu]$ sh 7b.sh x x1
file name x1
---------
word=a, count=1
word=b, count=1
word=c, count=1
word=d, count=1
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 30: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/30.jpg)
USN: SUBJECTCODE:13MCA17
8a. Write a shell script that determine the period for which a specified user is working on system and display appropriate message.
SOURCE CODE
echo "Enter Login name of the user"
read name
# get name and timing of logged in user from who cmd
userinfo=`who | grep -i "$name" | grep "pts"`
if [ $? -ne 0 ];
then
echo "$name is not Logged in"
exit
fi
hrs=`echo "$userinfo" | tr -s " " | cut -c ‘24-25’`
min=`echo "$userinfo" | tr -s " " | cut -c ‘27-28’`
curhr=`date | tr -s " " | cut -c 12-13` #get current time(hrs)
curmin=`date | tr -s " " | cut -c 15-16` #get Cur time(min)
hour=`expr $curhr - $hrs`
minutes=`expr $curmin - $min`
echo "$name is working since $hour hours and $minutes minutes"
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 31: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/31.jpg)
USN: SUBJECTCODE:13MCA17
Output1:
[reddy@localhost kanasu]$ sh 8a.sh
Enter Login name of the user
reddy
reddy is working since 2 hours and 17 minutes
Output2:
[reddy@localhost kanasu]$ sh 8a.sh
Enter Login name of the user
reddy
reddy is working since 2 hours and 19 minutes
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 32: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/32.jpg)
USN: SUBJECTCODE:13MCA17
8b. Write a shell script that reports the logging in of a specified user within one minute after he/she log in. The script automatically terminate if specified user does not log in during a specified period of time.
SOURCE CODE
echo "enter the login name of the user"read nameperiod=0until who | grep -w "$name"dosleep 5 #Pause the script for 5 secperiod=`expr $period + 1`if [ $period -gt 1 ];thenecho "$name has not login since 1 minute"exitfidoneecho "$name has now logged in "echo "enter the login name of the user"read nameperiod=0
until who | grep -w "$name"dosleep 5 #Pause the script for 5 secperiod=`expr $period + 1`
if [ $period -gt 1 ];thenecho "$name has not login since 1 minute"exitfidoneecho "$name has now logged in "
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 33: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/33.jpg)
USN: SUBJECTCODE:13MCA17
Output1:[reddy@localhost kanasu]$ sh 8b.sh
enter the login name of the user
reddy
reddy tty1 2013-12-26 19:32 (:0)
reddy pts/0 2013-12-26 19:33 (:0.0)
reddy has now logged in
enter the login name of the user
reddy
reddy tty1 2013-12-26 19:32 (:0)
reddy pts/0 2013-12-26 19:33 (:0.0)
reddy has now logged in
Output2:[reddy@localhost kanasu]$ sh 8b.shenter the login name of the userabcdabcd has not login since 1 minute
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 34: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/34.jpg)
USN: SUBJECTCODE:13MCA17
9a. Write a shell script that accepts the file name, starting and ending line number as an argument and display all the lines between the given line number.
SOURCECODE
if [ $# -lt 3 ]
then
echo "The number of arguments in command line is less than 3"
echo -e "Enter three arguments in the form of 'filename startline endline'\n"
exit
else
sed -n "$2,$3p" $1
echo "This is the section between lines $2 to $3 of the file:$1"
fi
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 35: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/35.jpg)
USN: SUBJECTCODE:13MCA17
Output1:
[reddy@localhost kanasu]$ sh 9a.sh repeat 1 4
eat
sat
ran
sat
This is the section between lines 1 to 4 of the file:repeat
Output2:
[reddy@localhost kanasu]$ sh 9a.sh repeat 1 4
The number of arguments in command line is less than 3
Enter three arguments in the form of 'filename startline endline'
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 36: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/36.jpg)
USN: SUBJECTCODE:13MCA17
9b. Write a shell script that folds long lines into 40 columns. Thus any line
that exceeds 40 characters must be broken after 40th, a "\" is to be
appended as the indication of folding and the processing is to be continued
with the residue. The input is to be supplied through a text file created by
the user.
SOURCECODE
echo "Input Filename"
read file
if [ ! -f $file ];then
echo "File not exists"
exit
fi
for ((i=1;i<8;i++))
do
x=`sed -n "$i"p $file | wc -c`
if [ $x -gt 40 ]; then
z=`sed -n "$i"p $file | fold -w 40`
echo "$z\\"
else
a=`sed -n "$i"p $file`
echo "$a"
fi
done
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 37: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/37.jpg)
USN: SUBJECTCODE:13MCA17
Output1:
[reddy@localhost kanasu]$ sh 9b.sh
Input Filename
aa
a
b
c
d
a
b
c
[reddy@localhost kanasu]$ cat aa
a
b
c
d
a
b
c
d
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 38: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/38.jpg)
USN: SUBJECTCODE:13MCA17
10a. Write an awk script that accepts date argument in the form of dd-mm-
yy and displays it in the form if month, day and year. The script should
check the validity of the argument and in the case of error, display a
suitable message.
SOURCECODE
BEGIN {
printf("enter date in the form(mm-dd-yy) : ")
getline str < "/dev/tty"
split(str,arr,"-")
if(arr[1]<=0||arr[1]>12)
printf("\n Invalid Month\n ")
else
if(arr[2]<=0 || arr[2]>31)
printf("\n Invalid date\n ")
else
system(" date +%A%t%B%t%Y -d" arr[3] "-" arr[1] "-" arr[2])
}
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 39: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/39.jpg)
USN: SUBJECTCODE:13MCA17
Output1:
[reddy@localhost kanasu]$ awk -f 10a.awk
enter date in the form(mm-dd-yy) : 12-12-2013
Thursday December 2013
Output2:
[reddy@localhost kanasu]$ awk -f 10a.awk
enter date in the form(mm-dd-yy) : 06-22-1992
Monday June 1992
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 40: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/40.jpg)
USN: SUBJECTCODE:13MCA17
10b. Write an awk script to delete duplicated line from a text file. The order of the original lines must remain unchanged.
SOURCE CODE
BEGIN {
print "Program Start"
}
{
if (data[$0]++==0)
lines[++count]=$0
}
END {
for ( i=1; i<count; i++)
print lines[i]
print "Program End"
}
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 41: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/41.jpg)
USN: SUBJECTCODE:13MCA17
Output1:
[reddy@localhost kanasu]$ awk -f 10b.awk aa
Program Start
a
b
c
Program End
Output2:
[reddy@localhost kanasu]$ awk -f 10b.awk bb
Program Start
a
b
c
d
e
f
Program End
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 42: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/42.jpg)
USN: SUBJECTCODE:13MCA17
11a. Write an awk script to find out total number of books sold in each discipline as well as total book sold using associate array down table as given below.
Electrical 34
Mechanical 67
Electrical 80
Computer Science 43
Mechanical 65
Civil 98
Computer Science 64
SOURCE CODE
BEGIN {
fs=" "
printf "The student book details are"
}
{
tot+=$2
books[$1]=books[$1]+$2
}
END
{
printf "\n Sub name \t no. of books sold \n"
for (i in books)
printf "%s \t %d \n" , i , books[i]
printf "Total books sold=%d\n" , tot }
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 43: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/43.jpg)
USN: SUBJECTCODE:13MCA17
Output1:
[reddy@localhost kanasu]$ awk -f 11a.awk book_details
The student book details are
Sub name no. of books sold
Civil 98
Computer_Science 107
Mechanical 132
Electrical 114
Total books sold=451
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 44: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/44.jpg)
USN: SUBJECTCODE:13MCA17
11b. Write an awk script to compute gross salary of an employee accordingly to rule given below.
If basic salary is < 10000 then HRA=15% of basic & DA=45% of basic
If basic salary is>=10000 then HRA=20% of basic & DA=50% of basic
SOURCE CODE
BEGIN {
print "\n\t Salary statement of Employees for the month of May 2008\n"
print "Sl.No.","NAME","\t\t","Designation","\t","Basic","\t","DA","\t","HRA","\t","Gross"
slno=1
}
{
if( $5 > 10000)
{
da=0.45*$5
hra=0.15*$5
}
else
{
da=0.50*$5
hra=0.20*$5
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 45: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/45.jpg)
USN: SUBJECTCODE:13MCA17
}
printf("%2d\t%-15s%-12s%8d %8.2f %8.2f% 8.2f\n",slno++,$2,$3,$5,da,hra,$5+hra+da)
}
END {
printf ("\nProgram End\n")
}
MCA Department, PG Studies, VTU, Belgaum Page NO -
![Page 46: USN: SUBJECTCODE:13MCA17hanumanthareddygn.weebly.com/uploads/3/8/6/9/3869… · Web view1a. Write a shell script that takes a valid directory name as an argument and recursively descend](https://reader036.vdocument.in/reader036/viewer/2022062600/5a6faf697f8b9aa7538b5086/html5/thumbnails/46.jpg)
USN: SUBJECTCODE:13MCA17
Output1:
[reddy@localhost kanasu]$ cat > emp.lst
1 John SoftwareEngineer 12-09-1985 20000
2 Mike HardwareEngineer 03-11-1993 18000
3 Rahul Mechanical 11-12-2011 13000
[reddy@localhost kanasu]$ awk -f 11b.awk emp.lst
Salary statement of Employees for the month of May 2008
Sl.No. NAME Designation Basic DA HRA Gross
1 John SoftwareEngineer 20000 9000.00 3000.00 32000.00
2 Mike Hardware Engineer 18000 8100.00 2700.00 28800.00
3 Rahul Mechanical 13000 5850.00 1950.00 20800.00
Program End
MCA Department, PG Studies, VTU, Belgaum Page NO -