0% found this document useful (0 votes)
537 views

Exercises ShellScript

The document provides examples of shell scripts that take command line arguments or filenames and perform various tasks like summing numbers, counting subdirectories, creating folders, finding empty subfolders, removing duplicate lines, and grepping/counting strings.

Uploaded by

subash pradhan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
537 views

Exercises ShellScript

The document provides examples of shell scripts that take command line arguments or filenames and perform various tasks like summing numbers, counting subdirectories, creating folders, finding empty subfolders, removing duplicate lines, and grepping/counting strings.

Uploaded by

subash pradhan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Exercises:

2.1. Write a shell script sum.sh that takes an unsp


ecified number of command line arguments (up
to 9) of ints and finds their sum. Modify the code
to add a number to the sum only if the number
is greater than 10
Ans:
#! /bin/sh
sum=0
for var in "$@"
do
if [ $var –gt 10 ]
then
sum=`expr $sum + $var`
fi
done
printf "%s\n" $sum
% sh 2.1.sh 2 4 5 -- run the script as
2.2. Write a shell script takes the name a path (eg
: /afs/andrew/course/15/123/handin), and counts
all the sub directories (recursively).
Ans:
#! /bin/sh
ls –R $1 | wc –l
run the script as:
% sh 2.2.sh /afs/andrew/course/15/123/handin
2.3. Write a shell script that takes a file where e
ach line is given in the format
F08,guna,Gunawardena,Ananda,SCS,CSD,3,L,4,15
123 ,G ,,
Creates a folder for each user name (eg: guna) an
d set the permission to rw access for user only
(hint: use fs sa)
Ans:
#! /bin/sh
cat $1 |
while read line
do
userid=`echo $line | cut –d”,” –f2`
mkdir $userid
fs sa $userid $userid rw
done
2.4 Write a shell script that takes a name of a fol
der as a command line argument, and produce a
file that contains the names of all sub folders wit
h size 0 (that is empty sub folders)
#! /bin/sh
ls $1 |
while read folder
do
if [ -d $1/$folder ]
then
files=`ls $1/$folder | wc –l`
if [ $files –eq 0 ]
then
echo $folder >> output.txt
fi
fi
done
2.5 Write a shell script that takes a name of a fol
der, and delete all sub folders of size 0
#! /bin/sh
ls $1 |
while read folder
do
files=`ls $folder | wc –l`
if [ files –eq 0 ]
then
rmdir $folder
fi
done
2.6 write a shell script that will take an input fi
le and remove identical lines (or duplicate lines
from the file)
#! /bin/sh
cat $1 | sort |
while read line
do
if [ $prev!=$line ]
then
echo $line >> sorted.txt
fi
prev=$line
done
[prosod01@acsayul33542 Nihar]$ grep -c "subash" abc.txt
4
[prosod01@acsayul33542 Nihar]$ cat abc.txt|tr -s ' ' '\n'|grep -c "subash"
6
[prosod01@acsayul33542 Nihar]$ cat abc.txt|tr -s ' ' '\n'|grep -ic "subash"
8
[prosod01@acsayul33542 Nihar]$ vi abc.txt
[prosod01@acsayul33542 Nihar]$ cat abc.txt|tr -s '|' '\n'|grep -ic "subash"
3
[prosod01@acsayul33542 Nihar]$ cat abc.txt|tr -s '|' '\n'|grep -i "subash"
subash
subash
subash
[prosod01@acsayul33542 Nihar]$ vi abc.txt
[prosod01@acsayul33542 Nihar]$ cat abc.txt|tr -s '|' '\n'|grep -i "subash"
Subash
subash
subash
subash
Subash
subash
[prosod01@acsayul33542 Nihar]$

You might also like