CS 497C - Homework 7 - Name:_________________ DUE: Friday, December 7 (in class) Please show all work on a separate sheet attached to this sheet. (hint: Self-Test and Exercises of Chapter 11, 18, 19) (50 points - 5 points for each problem) + (5 bonus points) 1. (1) What is a socket? (2) What is FQDN? Ans: (1) A socket consists of a set of four numbers - the IP addresses and port numbers of the client and server. (2) A fully qualified domain name. It consists of a string of domains separated by dots. 2. (1) Name some common networking daemons and their functions. (2) How do you use talk to communicate with user brenda on host kirk? (3) When using ftp, how do you change your directory on the lcoal machine? Ans: (1) sendmail for handling mail, httpd for Web service and inetd for ftp and telnet. (2) Use talk brenda@kirk. (3) Use lcd bar. 3. (1) How is a TCP/IP network different from a telephone network? (2) Why is TCP termed a reliable protocol? Ans: (1) Unlike the telephone system, TCP/IP breaks up data into packets which take different routes to reach the destination. Lost packets are also resent. There is no dedicated connection between sender and receiver. (2) TCP monitors transmission of data with an elaborate system of timeouts and retransmission facilities. These ensure that a missing or corrupted segment will make reassembly impossible. 4. (1) How will you find out the port number finger uses? (2) If talk throws up a message Your party is refusing messages, what could be the cause? (3) You copied a graphics file with ftp, and the file appears to be corrupted. What could be the possible reason? Assume the original file is not corrupted. Ans: (1) Look up /etc/services and you'll find that finger uses the port number 79. (2) talk won't work if the mesg setting is set to n. (3) You did not use ftp's binary command before starting the transfer, and the machine's default transfer mode is not binary. 5. (1) If a script calls itself, what do you think will happen? (2) If a script is run as foo -l -t bar[1-3], what are the values of $# and $*? Does it make any difference if the options are combined? Ans: (1) The shell will continuously spawn sub-shells till the system is able to handle so many of them. (2) $# is 5 and $* expands to the string -l -t bar1 bar2 bar3 provided all the files exist. Combining the options brings down $# to 4 and changes $* to -lt bar1 bar2 bar3. 6. (1) What is the exit status of a command? What is its normal value, and where is the value stored? (2) Can you use sh < foo.sh instead of sh foo.sh to execute the script? Ans: (1) The exit status is an integer that represents the success or failure of a command. If has the value 0 when the command executes successfully and is stored in the parameter $? (2) Yes, the shell accepts standard input; in fact, that's what it uses most of the time. 7. If x has the value 5, and you reassign it with x="expr $x + 10", what is the new value of x? What would have been the value if single quotes were used? Ans: x has the value expr 5 + 10 where the expression is enclosed within double quotes. If single quotes had been used, then it would have been expr $x + 10. You should actually be using the backquotes here to obtain the value 15. 8. (1) What could be the problem with this command? set `grep -c "A HREF" catalog.html` (2) What will this statement do? filename=${1:-emp.lst} Ans: (1) If grep fails to find the pattern A HREF, then set will operate with no arguments and display all shell variables on the terminal. (2) If a script is invoked without any argument, $1 would be null and filename would set to emp.lst. 9. (1) Why won't the exit command, when placed in a shell script like this, terminate the script? How do you get it over. (statements; exit) (2) Why isn't the exit statement used inside a function to return control to the calling program? Ans: (1) The statements inside () are executed in a sub-shell. To be able to terminate the script, use {} instead. (2) A shell function always executes in the current shell. An exit statement will terminate the shell itself and may log the user out. 10. (1) You have to run a job at night and need to have both the output and error message in the same file. How will you run the script? (2) A script containg the statement while [${count:=1} -lt 50] is not executing the loop at all. What could be he possible reason? Ans: (1) Append the stream merging symbols 2>&1 to every statement inside the script that produces output and have the likelihood of producing error messages. Redirect the script to a file on execution. (2) count may have already been set in the script before starting the loop. This loop will work only if count has been previously undefined or set to null. 11. There are at least six syntactical mistakes in this program. Locate them. (Line numbers are shown on left.) 1 ppprunning = yes 2 while $ppprunning = yes ; do 3 echo -e " Internet Menu\n 4 1. Dial out 5 2. Exit 6 Choice: 7 read choice 8 case choice in 9 1) if [ -z "$ppprunning" ] 10 echo "Enter your username and password" 11 else 12 chat.sh 13 endif ; 14 *) ppprunning=no 15 endcase 16 done Ans: (1) There are eight mistakes. (i) line 1 - Spaces not permitted around =. (ii) line 2 - test operators [] missing in while's control command. (iii) line 6 - quotes not closed with echo. (iv) line 8 - $ missing in choice. (v) line 9 - ; then missing. (vi) line 13 - fi required instead of endif. (vii) line 13 - ;; required instead of ; (viii) line 15 - esac required instead of endcase. (2) The correct code is shown as below: ppprunning=yes while [ $ppprunning = yes ] ; do echo -e " Internet Menu\n 1. Dial out 2. Exit Choice:" read choice case $choice in 1) if [ -x "$ppprunning" ] ; then echo "Enter your username and password" else chat.sh fi ;; *) ppprunning=no esac done