Lab Workbook and Solutions Syed Mansoor Sarwar Robert Koretsky Lab Workbook UNIX: The Textbook Syed Mansoor Sarwar Lahore University of Management Sciences [email protected] Robert Koretsky University of Portland [email protected] August 27, 2004 The commands and shell scripts given in this manual were tested on SunOS and/or FreeBSD. It should work also under Linux 2 Important notes 1. Almost all of the commands and shell scripts in this solutions manual were tested on SunOS and/or FreeBSD. 2. In the very few cases, where a command does not work under this system, it should work under a LINUX system. 3. We have used ~ to denote the home directory and that it does not work under the Bourne shell. Thus when you run commands that use ~ under a Bourne shell, make sure to change it to $HOME. 4. For some of the Problems in Labs 15 and 17, you have to use the expr or @ command, respectively. So, discuss these commands in class before assigning any of these problems as homework. This was not done intentionally, but was rather an oversight on our part. 5. The solutions for the problems in the first 7 labs are tailored for the novice user of a UNIX system, and many times reflect the simplest and most instructive path towards solving the problems as stated and learning. 6. Where no particular question is asked in the problem, and where a user is asked to perform a series of operations on a computer running the UNIX system, a commentary which briefly describes or provides additional references is supplied instead of an explicit answer. 3 Lab 18 (Chapter 18) Learning Objectives 1. 2. 3. 4. 5. To learn how to process numeric data in C shell scripts To learn how to use the here document facility of C shell in a script To learn how to process arrays in C shell scripts To learn how to use the signal/interrupt process facility of Bourne shell To learn how to debug a Bourne shell script Lab Work 1. Log on to your UNIX system. 2. Write a C shell script that takes integer numbers as command line arguments and displays their sum. Use the while control structure and integer arrays. Do appropriate exception handling in your code. Show your script and a few sample runs. % cat lab18p2 #!/bin/csh @ size = $#argv # Size of the input list @ index = 1 # Array index initialized to point to the first element @ sum = 0 # Running sum initialized to 0 while ( $index <= $size ) @ sum = $sum + $argv[$index] # Update the running sum @ index++ # Increment array index by 1 end echo "The sum of the given $#argv numbers is $sum." exit 0 % 3. Modify the script in the above problem so that it reads integers to be added as a here document. % cat ch18p10 #!/bin/csh @ numbers = 0 @ stat = 0 # Read numbers in the Here Document and put them in the 'numbers' array while ( $stat == 0 ) set numbers = `head -1` \ << Input_Data 1 2 3 4 5 6 7 8 9 10 Input_Data @ stat = 1 # Get out of this loop as the data have been read end 4 @ sum = 0 @ index = 1 @ size = $#numbers while ( $size >= $index ) @ sum = $sum + $numbers[$index] @ index++ end clear echo "The sum of numbers in the Here Document is $sum." echo " " exit 0 % 4. Modify the dext script in Section 18.4 so that it takes a list of names as command line arguments. Use the foreach control structure to implement your solution. Do appropriate exception handling. Show your code and a few sample runs. % cat lab18p4 #!/bin/csh if ( $#argv == 0 ) then echo "Usage: $0 name_list” exit 1 endif foreach name ( $argv ) grep -i "$name" \ << Directory_Data John Doe 555.232.0000 [email protected] Jenny Great 444.656.1111 [email protected] David Nice 999.111.3333 [email protected] Jim Davis 777.000.9999 [email protected] Art Pohm 333.000.8888 [email protected] Directory_Data end exit 0 % 5. Write a C shell script that prompts the user for a positive integer number from the keyboard and displays the Fibonacci numbers equal to the number entered by the user. Thus, if the user enters 8, your script displays the first seven Fibonacci numbers. After this, the script prompts the user for input again. The script terminates if user enters 0. You should not be able to terminate the program with <Ctrl-C>. When you press <Ctrl-C>, your script displays a message and continues. Do appropriate exception handling in your code. Show the code and a few sample runs. % cat lab18p5 #!/bin/csh # Intercept ^C and transfer control to the command at interrupt_label: backagain: onintr interrupt sleep 5 # This is show that <^C> is ignored and the desired message is displayed. if ( $#argv != 1 ) then echo "Usage: $0 number" exit 1 5 endif @ size = $argv[1] # Number of Fibonacci numbers to be displayed if ( $size >= 1 ) then @ previous = 1 @ current =1 echo -n "The list of Fibonacci numbers is: " if ( $size == 1 ) then echo "$previous" exit 0 endif if ( $size == 2 ) then echo "$previous $current" exit 0 endif echo -n "$previous $current" @ size = $size - 2 @ temp = 0 while ( $size > 0 ) @ temp = $current # New previous @ current = $current + $previous # The next number @ previous = $temp echo -n " $current" @ size-end endif echo " " exit 0 # Code executed on <^C> interrupt: echo "Nice try -- you cannot terminate me with <^C>\!" goto backagain % 6. Show working of the scripts fs (Section 18.3) and onintr_demo (Section 18.5). Show a few sample runs. No solution needed. 7. Log out. % <Ctrl-D> 6
© Copyright 2026 Paperzz