CS 497C - Homework 6 - Name:_________________ DUE: Monday, November 26 (in class) Please show all work on a separate sheet attached to this sheet. (50 points - 5 points for each problem) + (10 bonus points) 1. How do you find out the PID of your login shell? Which is the process that listens on every terminal for a login request? What is the role of th swap area in the control of processes? Ans: 1) Use echo $$ 2) The getty process. 3) The swap are is a special file system that contains the process image when it is no longer running. The kernel recalls the image from the swap area when it's time for it to run again. 2. First create a directory foo and then fill up a shell script with three commands shown below. What happens when you execute this script and why? mkdir foo; cd foo; pwd Ans: 1) This is what happens after executing the script: mkdir: cannot create directory `foo': File exists /usr/users/User22/chang/lecture/cs497c/homework/foo The script run is a sub-shell. First, it fails to create the directory foo because the directory foo already exists. Then it changes to the directory foo and shows the current directory. 2) Because changes made by the child process are not available in the parent, the original directory is restored after the completion of the script. 3. If this pipeline is run from a shell script, who will be the parent and grandparent of all these processes? nl foo | sort -nr | cut -f2- Ans: The sub-shell used to run the script will be the parent, and the login shell will be the grandparent. 4. What is the difference between a process run with & and one run with nohup? Ans: A process run with nohup continues to run even after the user has logged out. When run with only an &, the background process is terminated on logging out. However, this doesn't happen in the C shell and bash. 5. How are the signal numbers 2 and 9 generated? What are their names and how do they differ in function? Ans: The interrupt key generates signal 2, the SIGINT signal. It may or not may not terminate a program. The SIGKILL signal having the number 9 terminates a process unconditionally. It is generated by using the name or number with the kill command; it cannot be generated by pressing a key. 6. Frame regular expressions to match lines containing (i) jefferies jeffery jeffeys (ii) hitchen hitchin hitching (iii) Heard herd Hird (iv) dix dicks dickson dixon (v) Mcgee mcghee magee. Ans: (i) jeffe*r*[iy]e*s* (ii) hitch[ei]ng* (iii) [Hh][ei]a*rd (iv) di[xc]k*s*o*n* (v) [Mm][ac]gh*ee 7. Use command substitution with grep to list the names of the persons from emp.lst who has birthday today. Ans: Use grep `date "+%m/%d"` emp.lst | cut -d\| -f2. The date command extracts the month and day in mm/dd format. 8. Find out the occurrences of three consecutive and identical word characters (like aaa or bbb) using (i) grep and (ii) sed. Ans: Use the tagged regular expression: (i) grep '\([a-zA-Z0-9_]\)\1\1' foo (ii) sed -n '/\([a-zA-Z0-9_]\)\1\1/p' foo 9. Locate lines that are less than 100 characters in length using (i) grep and (ii) sed. Ans: (i) grep -v ".\{100,\}" foo (ii) sed "/.\{100,\}/d" foo 10. What is wrong with this command? How do you correct it? sed 's/compute/calculated/g s/computer/host/g' foo Ans: The first s command converts the compute in computer to calculate as well, leaving nothing for the second s commadn to act on. Reverse the instructions: sed 's/computer/host/g s/compute/calculated/g' foo 11. Write a command sequence to find out the number of occurrences of the word "encryption" in a file. (Note: A word may occur more than once in a line.) Ans: sed 's/encryption/&\ /g' foo | grep -c encryption 12. Invert the names in short list on Page 275 and place a comma after the last name (i.e. last name, first name). The result should look like as below: 2233|harris, charles |g.m. |sales |12/12/52| 90000 9876|johnson, bill |director |production|03/12/50|130000 5678|dylan, robert |d.g.m. |marketing |04/19/43| 85000 2365|woodcock, john |director |personnel |05/11/47|120000 5423|wood, barry |chairman |admin |08/30/56|160000 Use as few commands as possible. Ans: cut -d\| -f1 shortlist > temp1 cut -d\| -f2 shortlist > temp2 cut -d\| -f3- shortlist > temp3 Now, cut the two components of the name from the name field, but offset the first name by one space: awk -F' ' '{printf "%s, %s|%s\n", $2, $1, length($0)}' temp2 > temp2a awk -F'|' '{printf "%-*s\n", $2, $1}' temp2a > temp2 Paste the two names using the comma as delimiter before pasting back all sections: paste -d'|' temp[1-3]