You are on page 1of 8

Part-A Q1.

a) List all the files and sub directories of the directory /bin.

Ans: The command for listing all the files and sub directories of the directory /bin is cd bin | ls l b) List all the files including hidden files in your current directory. Ans: The command for including all the hidden files in our current directory is ls -a. c) List all the files starting with letter r in your current directory. Ans: The command for listing all the files starting with letter r in your current directory is ls al r* d) List all the files having three characters in their names, from your current directory. Ans: The file having three characters in their names from tha current directory is ls *abc*.
e) List all the files with extension .doc in your current directory. Ans: The command for listing all the files with extension .doc in your current directory is ls *.doc.

Q2. a) Using one single command, display the output of who and pwd commands. Ans: The command that display the output of who and pwd commands together is who ; pwd. b) Display the system date in following format: Today is Friday, 17 May 96 Ans: echo Today is date c) Display the following text message on the monitor screen. Deposited $100 to you account. Ans: echo Deposited \$100 to you account. d) Display the following message on the monitor. The long listing of my home dir is (Hint: Use ls l and pwd commands) Ans: echo The long listing of my home dir pwd is ls l .

Q3.

Create

the file employee.txt having colon (: ) separated fields. The fields of the record are: enumber, ename, eposition, esal, edoj, edept. And now answer the following: a. List all the employees along with a row number b. Sort the file as per the names c. List top three salaried employees d. Remove duplicate records from the file e. List dept. no along with no. of employees working in each dept. f. Sort the file in descending order of salary

Q4. Accept a file name and a number (x). Display x lines from the top of the file. Check if the file exists and is readable. The value of x should not exceed the total number of lines in the files. Display suitable messages in case an error is encountered. Ans: echo enter the file name that we have to display
read a if [ -e $a ] then echo enter the nos. of lines read b x = wc l $a if [ $b le $x ] then head -$b $a else echo entered line is not found fi else echo the file does not exist fi

Q5. Write a menu based script which displays the following options :

Accept roll number, name, marks in sub1, sub2 and sub3. Calculate total, percentage, grade & class and enter the record in a file student. The marks are out of 100 in each subject. Allow the user to enter any number of records.

Grade is found out as follows : Percentage < 35 >= 35 & < 50 >= 50 & < 60 >= 60 & < 75 >= 75 Grade Fail Third Second First Distn

If the student secures < 35 in any one of the subjects, then class = fail. Otherwise class=pass.

Ans: q=y
c=1 while [ $q = 'y' ] do echo "enter the name of student $c.. " read name echo "enter the roll no of student " read roll echo "enter the marks of three subjects out of 100 read sub1 sub2 sub3 echo "records of $name having roll no. $roll are as follows... " >> student echo "records of $name having roll no. $roll are as follows..." if [ $sub1 -le 100 ] && [ $sub2 -le 100 ] && [ $sub3 -le 100 ] | bc

then if [ $sub1 -lt 35 ] || [ $sub2 -lt 35 ] || [ $sub3 -lt 35 ] then class="fail" echo "class is $class " >> student echo "class is $class " else class=pass echo "class is $class " >> student echo "class is $class " fi tot=`expr $sub1 + $sub2 + $sub3` echo "total is $tot " >> student echo "total is $tot " per=`expr $tot / 3` echo "percentage is $per % " >>student echo "percentage is $per % " if [ $per -lt 35] then grade="fail" echo "grade is $grade " >> student echo "grade is $grade " elif [ $per -ge 35 ] && [ $per -lt 50 then grade="third" echo " grade is $grade " >> student echo " grade is $grade" elif [ $per -gt 60 ] && [ $per -lt 75 ]

then grade="first" echo "grade is $grade " >> student echo "grade is $grade " else grade="distn" echo " grade is $grade " >> student echo " grade is $grade " fi else echo "incorrect data " c=`expr $c - 1` #decrease one in the counter for the incorrect data fi echo " continue..[y/n].. " read q c=`expr $c + 1` #increase one in the counter done echo "bye bye.. "

Part-B Q6. Accept roll number from the user at the command line. Search it in the student file. If it is present, then display name, percentage, grade and class of the student. If the roll number is not present, then display a message not found. Allow the user to enter any number of queries. Ans: a=$#
for (( i=1;i<=$a;i++ ))

do if grep -w $i student then grep A 4 $i student echo echo continue read else echo record not found fi done

Q7. Write a shell script which will accept three nos. from the keyboard and finds the largest of them. Ans: function greater() { echo the nos. are $1 $2 $3 if [ $1 gt $2 ] && [ $1 gt $3 ] then echo the greatest no. is $1 if [ $2 gt $1 ] && [ $2 gt $3 ] then echo the greatest no. is $2 if [ $3 gt $1 ] && [ $3 gt $2 ] then echo the greatest no. is $3 fi fi fi }

greater $1 $2 $3

Q8. Write a shell script to copy one file to another line by line. Ans: echo enter the file that we need to copy read f1 echo enter the file to which the data will be copy read f2 x=2 for((j=1;j<=$x;j++)) do head -$j $f1 >> $f2 read done echo the data successfully copied from the file Q9. Write a shell script which will calculate the factorial of an integer entered from the keyboard. Ans: function fact() { i=1 for((j=1;j<=$i;j++)) do i=$[ $i*$j ] done echo the factorial is $i } fact $1 Q10. Write a script which when executed checks out whether it is a working day or not? (Note: Working day Mon-Fri)

Ans: x=$1 case $x in monday) echo its a working day;; tuesday) echo its a working day;;
wednesday) echo its a working day;; thursday) echo its a working day;; friday) echo its a working day;; *) echo its a non-working day;; esac

You might also like