#!/bin/bash Paste Students - TXT Marks - TXT Ques1.txt For I in (75..100) Do Grep $i Ques1.txt Done - Sort - Uniq RM Ques1
#!/bin/bash Paste Students - TXT Marks - TXT Ques1.txt For I in (75..100) Do Grep $i Ques1.txt Done - Sort - Uniq RM Ques1
Q.1 Grep the marks of the students who scored above 75 in atleast one subject. Ans. Bash Script is :
#!/bin/bash paste students.txt marks.txt >> ques1.txt for i in {75..100} do grep $i ques1.txt done | sort | uniq rm ques1.txt
Q.2 Grep the marks of all the students whose names begin with an s. Ans. #!/bin/bash paste students.txt marks.txt >> ques1.txt grep S ques1.txt rm ques1.txt Q.3 Grep the marks of all the students whose names begin with consonants. Ans. #!/bin/bash paste students.txt marks.txt >> ques1.txt grep ^[AEIOU] ques1.txt rm ques1.txt
Q.4 Change the results.sh script to accept the input files also as arguments. Ans.
#!/bin/bash # to execute write ./ques4.sh students.txt marks.txt
cut -d " " -f 2- $1 | paste -d " " $2 - | sort > results.txt
Q.5 Write a shell script that will take a filename as input and check if it is executable. Ans. #!/bin/bash # ./ques5.sh filename.txt ex=`ls -l $1 | cut -c4 -` if [ "$ex" != "x" ] then echo "Not Executable" else echo "Executable" fi Q.6 Modify the script in the previous step, to remove the execute permissions, if the file is executable. Ans. #!/bin/bash # ./ques6.sh filename.txt ex=`ls -l $1 | cut -c4 -` if [ "$ex" != "x" ] then echo "Not Executable" else echo "Executable" echo "Removing execute permission" chmod u-x $1 fi
Q.7 Write a shell script to remove all executable files from a directory, when a directory is given as argument. Ans. #!/bin/bash # ./ques7.sh directory for i in `ls $1` do cd $1 ex=`ls -l $i | cut -c4 -` if [ "$ex" != "x" ] then echo "$i not executable retained" else echo "Executable" echo "Removing $i" rm $i fi cd .. done Q.8 List all the years between 2001 and 2099 which have 5 Fridays, Saturdays and Sundays in the month of July. Hint: man cal Ans. #!/bin/bash for i in {2001..2099} do ncal july $i >> tmp2.txt echo $i >> yr1.txt done grep "Fr" tmp2.txt >> tmp1.txt paste yr1.txt tmp1.txt >> pos1.txt grep "Fr 1" pos1.txt | cut -c 1-4 rm tmp1.txt rm tmp2.txt rm yr1.txt rm pos1.txt
Q.9 Generate frequency list of all the commands you have used, and show the top 5 commands along with their count. (Hint: history command will give you a list of
all commands used.)
Q.10 Generate a word frequency list for wonderland.txt. Hint: use grep, tr, sort, uniq (or anything else that you want) Ans. #!/bin/bash # ./ques10.sh you wonderland.txt grep -ion "$1" $2 >> linec.txt echo " Frequency of word $1 in $2 " wc -l linec.txt | cut -d " " -f1 rm linec.txt
Q.11 Print the middle line of the file wonderland.txt. Ans. #!/bin/bash # ./ques11.sh wonderland.txt ex=`wc -l $1 | cut -d " " -f1` ex1=`echo $((ex/2))` cat $1 | sed $ex1'q;d'
Q.12 Write a code that lists the last word of the string in one file and remaining string in another file. E.g.-In Who is Homer Simpson,Simpson should be written in one file and Who is Homer should be written in another file. Ans. #!/bin/bash rm tmp1.txt rm file1.txt rm file2.txt echo "Enter String" read str echo $str >> tmp1.txt ex=`wc -w tmp1.txt | cut -d " " -f1 -` ex1=`echo $(($ex-1))` cut -d " " -f1-$ex1 tmp1.txt >> file1.txt cut -d " " -f$ex tmp1.txt >> file2.txt rm tmp1.txt
paste $1 $2 >> N1.txt fi fi Q.14 Write a script that takes exactly one argument, a directory name. If the number of arguments is more or less than one, print a usage message. If the argument is not a directory, print another message. For the given directory, print the five biggest files and the five files that were most recently modified. Ans.