CS 497C - Quiz 4B - Name: ______________

Date: Friday, December 7 (in class)

Part 1: (20 points - 4 points for each problem)

(2) 1. When you look for a pattern in files ignoring case which one 
       will you use?
      (1) grep -v (2) grep -i (3) grep -l (4) grep -e
(4) 2. Which command has the same result as head -3? 
      (1) tail +3 (2) sed '3d' (3) sed '3p' (4) sed '3q'
(1) 3. If x has the value 10, what is the value of x$x$?
      (1) x10$ (2) 1010 (3) 10x$ (4) x$10
(1) 4. Which special parameter indicates the name of the script?
      (1) $0 (2) $1 (3) $! (4) $* 
(3) 5. What can be put into the first line of your script to make sure a 
       script will use the Bourne shell?
      (1) #!/bin/csh (2) #/bin/sh (3) #!/bin/sh (4) $/bin/sh

Part 2: (30 points - 6 points for each problem)

  1. What is an interval regular expression (IRE)? Give an example of IRE.
    Ans: 1) An IRE is an expression that uses the characters { and } with a
            single or pair of numbers between them.
         2) ch\(m\) - Here, the metacharacter ch can occur m times.
        a\{5,10\}
    
  2. What is the sed command? Describe the format of the sed command. Give an example.
    Ans: 1) sed is a multipurpose tool which combines the work of several
            filters. 
         2) sed options 'address action' file(s)
         3) sed -n '1,$s/Basic/Java/g' foo
    
  3. Frame regular expressions to match lines containing (i) jefferies jeffery jeffeys (ii) hitchen hitchin hitching (iii) dix dicks dickson dixon.
    Ans: (i) jeffe*r*[iy]e*s* (ii) hitch[ei]ng* (iii) di[xc]k*s*o*n*
    
  4. sum and n are two variables with numeric values. Write a script to calculate sum / n with two digital precision after the decimal point.
    Ans: echo "scale=2; $sum / $n" | bc
    
  5. Write a script to read in a number n as an argument and calculate its factorial n! where n! = n x (n - 1) x (n - 2) x .... x 1.
    Ans: #!/bin/sh
         n=$1
         fac=$1
         while [ $n -gt 1 ]
         do
           n=`expr $n - 1`
           fac=`expr $fac \* $n`
         done
         echo "$1! = $fac."