Homework Questions and Answers #2

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
 

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 
 

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”
 

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
 

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 “it’s a working day”;;

        tuesday) echo “it’s a working day”;;
          wednesday) echo “it’s a working day”;;
         thursday) echo “it’s a working day”;;
         friday) echo “it’s a working day”;;
         *) echo “it’s a non-working day”;;
         esac

Linux Tips

See Also

Have a Linux Problem
Linux Forum - Do you have a Linux Question?

Linux Books
Linux Certification, System Administration, Programming, Networking Books

Linux Home: Linux System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.