You are on page 1of 3

Shell Scripts

Write a shell script that accepts a file name, starting and ending line numbers as arguments and
displays all the lines between the given line numbers.

#!/bin/bash
echo " Enter the file name"
read fname
echo “enter starting line number”
read sl
echo “enter ending line number”
read el

if [ -f $fname ]
then
echo "the lines between $sl and $el of given file are"
sed -n $sl,$el\p $fname|sed -e '1d;$d'
else
echo "file doesnt exist"
fi

Write a shell script that deletes all lines containing a specified word in one or more files supplied as
arguments to it.
#!/bin/bash
if [ $# -ne 0 ]
then
echo enter the word
read word
for fname in $*
do
if [ -f $fname ]
then
echo the given input filename is:$fname
grep -v "$word" $fname
else
echo its not a file
fi
done
else
echo "enter atleast one argument as input"
fi
Write a shell script that displays a list of all the files in the current directory to which the user has
read, write and execute permissions.
#!/bin/bash
echo "List of Files which have Read, Write and Execute Permissions in Current Directory"
for file in *
do
if [ -r$fname-a-w$fname-a-x$fname ]
then
echo $file
fi
done
Write a shell script that receives any number of file names as arguments checks if every argument
supplied is a file or a directory and reports accordingly. Whenever the argument is a file, the number
of lines on it is also reported.
#!/bin/bash
for fname in $*
do
if test -f $fname
then
echo $fname "is a ordinary file"
echo "number of lines:"

cat $fname|wc -l

else if test -d $fname


then
echo $fname "is a directory"
fi
fi
done

Write a shell script that accepts a list of file names as its arguments, counts and reports
theoccurrence of each word that is present in the first argument file on other argument files.
#!/bin/bash
if [ $# -ne 2 ]
then
echo "Error : Invalid number of arguments."
exit
fi
str=`cat $1 | tr '\n' ' '`
for a in $str
do
echo "Word = $a, Count = `grep -c "$a" $2`"
done

Write a shell script to list all of the directory files in a directory.


#!/bin/bash
echo “ Enter dir name “
read dir
if [ -d $dir ]
then
cd $dir
echo “ Directory Files in $dir are “
for i in *
do
if [ -d $i ]
then echo "$i"
fi
done
else
echo “ Dir does not exist”
fi
Write a shell script to find factorial of a given integer.
#!/bin/bash
echo "enter the num"
read fact
i=1
count=0
while [ $fact -ne $count ]
do
count=`expr $count + 1`
i=`expr $i \* $count`
done
echo " factorial is $i"

Write an awk script to count the number of lines in a file that do not contain vowels.

BEGIN{
total=0;}
{if($0!~/[aeiouAEIOU]/)
total=total + 1}
END{print "The total lines in a file that do not contain vowels:",total}

Write an awk script to find the number of characters, words and lines in a file.

BEGIN{print " required output is" }


{
len=len+length($0)
words =words+NF
}
END{
print("\n words :\t" words)
print("characters :\t" len)
print("lines :\t" NR)
}

write a shell script to greet the user based on time.


#!/bin/bash
h=`date +%H`
if [ $h -lt 12 ]; then
echo Good morning
elif [ $h -lt 18 ]; then
echo Good afternoon
else
echo Good evening
fi

You might also like