UNIX for Intermediate Users Developed by: User Liaison Section, D-7131 [Name and numbers removed at author's request] Revision Date: TABLE OF CONTENTS I. INTRODUCTION........................................................................ ii A. Audience..................................................................... ii B. Course Objectives............................................................ ii C. Course Handout Conventions...................................................iii 1. THE FILE CALLED .profile AND PROCESSES.............................................. 1 1.1 HOME........................................................................ 1 1.2 PATH........................................................................ 2 1.3 INGRES Environment Variables................................................ 2 1.4 ING_HOME.................................................................... 3 1.5 TERM_INGRES................................................................. 3 1.6 ING_EDIT.................................................................... 3 1.7 Processes................................................................... 4 1.8 Executing a Command......................................................... 4 1.9 Process Identification...................................................... 5 1.10 Interrupt Handling......................................................... 7 2. COMPILING "C" PROGRAMS............................................................... 10 2.1 "C": Sample Program with a Main and Two Functions in One ............................................................ 10 2.2 "C": Compiling a Program.................................................... 12 2.3 "C": Renaming the Executable Module......................................... 13 2.4 "C": Giving a Name to the Output File....................................... 14 2.5 "C": Producing an Assembly Listing.......................................... 15 2.6 "C": Main and Two Functions in Three Separate Source Files.............................................................. 16 2.7 "C": Compiling but Not Producing an Executable Module.................................................................... 17 3. COMPILING FORTRAN PROGRAMS......................................................... 18 3.1 FORTRAN: Sample Program a Main and Two Subroutines............................................................... 18 3.2 FORTRAN: Compiling a Program................................................ 19 3.3 FORTRAN: Renaming the Executable Module..................................... 20 3.4 FORTRAN: Giving a Name to the Output File................................... 21 3.5 FORTRAN: Producing an Assembly Listing...................................... 22 3.6 FORTRAN: Main and Two Subroutines in Three Separate Source Files........................................ 23 3.7 FORTRAN: Compiling But Not Producing an Executable Module.................................................................... 24 3.8 FORTRAN: Compiling Object Files to Produce an Executable Module....................................... 25 4. COMPILING COBOL PROGRAMS............................................................ 26 4.1 COBOL: Sample Program with a Main and Two Subroutines............................................................... 26 4.2 COBOL: Compiling a Program.................................................. 27 4.3 COBOL: Running a Program.................................................... 28 Workshop 2-4..................................................................... 30 5. UNIX TOOLS.......................................................................... 34 5.1 The make Utility............................................................ 34 p: A Pattern Matching Filter............................................................ 37 5.2.1 More on Regular Expressions........................................ 38 5.2.2 Closure............................................................ 42 5.2.3 Some Nice grep Options ................................ 43 5.2.4 Summary of Regular Expression Characters........................... 44 5.3 sed: Edit a File to Standard Output......................................... 45 5.4 awk: A Pattern Matching Programming Language................................ 49 5.5 sort: Sort a File........................................................... 53 5.6 Archiver and Library Maintainer............................................. 56 5.7 Creating an Archive File with Object Modules................................ 57 5.8 Verifying the Contents of the Archive File.................................. 57 5.9 Removing Duplicate Object Files............................................. 58 5.10 Compiling Main and Archive Files........................................... 58 Workshop 5....................................................................... 59 6. UNIX UTILITIES PART I - DISPLAY AND MANIPULATE FILES................................ 63 7. UNIX UTILITIES PART II - DISPLAY AND ALTER STAUTS................................... 73 8. UNIX UTILITIES PART III - MISCELLANEOUS............................................. 85 9. ADVANCED FEATURES OF FTP............................................................ 90 9.1 Initializing FTP on UMAX.................................................... 91 9.2 Multiple File Transfers..................................................... 92 9.3 Auto Login Feature.......................................................... 93 9.4 Macros...................................................................... 95 9.5 Filename Translation........................................................ 96 9.6 Aborting Transfers.......................................................... 97 9.7 More Remote Computer Commands............................................... 98 Workshop 10...................................................................... 99 APPENDIX A - sh.........................................................................101 APPENDIX B - ftp........................................................................116 APPENDIX C - C Compiler.................................................................128 APPENDIX D - FORTRAN Compiler...........................................................137 APPENDIX E - lint.......................................................................147 APPENDIX F - cb.........................................................................151 APPENDIX G - ar.........................................................................152 INDEX...................................................................................157 I. INTRODUCTION A. Audience This course is for individuals who need to use utilities and advanced features of the UNIX operating system. B. Course Objectives Upon successful completion of this course the student will be able to: 1. Compile C, FORTRAN, and COBOL programs. 2. Create processes to run in the background 3. Use advanced features of FTP such as: multiple file transfers, auto logins, macros, globbing, filename translation, aborting transfers, and other remote computer commands. 4. Use UNIX utility programs such as grep, sed, awk, sort, and others. 5. Use the make utility. 6. Understand processes, including structure, executing a command, process identification, exit status, plus . (dot) and exec processing. C. Course Handout Conventions There are several conventions used in this handout for consistency and easier interpretation: 1. Samples of actual terminal sessions are single-lined boxed. 2. User entries are shown in bold print and are underlined. exit 3. All keyboard functions in the text will be bold. (Ret) Backspace Tab Ctrl-F6 Print (Shift-F7) Go to DOS (1) NOTE: (Ret) indicates the Return or Enter key located above the right Shift key. 4. Examples of user entries not showing the computer's response are in dotted-lined boxes. 5. Command formats are double-lined boxed. 6. Three dots either in vertical or horizontal alignment mean continuation or that data is missing from diagram. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ ³ ³ Multimax, Nanobus, and UMAX are trademarks of ³ ³ Encore Computer Corporation. ³ ³ ³ ³ ³ ³ Annex is a trademark of XYLOGICS, Inc. ³ ³ ³ ³ ³ ³ UNIX and Teletype are registered trademarks of ³ ³ AT&T Bell Laboratories ³ ³ ³ ³ ³ ³ Ethernet is a trademark of Xerox Corporation ³ ³ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ 1. UNIX PROCESSES AND A FILE CALLED .profile 1.1 Processes A process is the execution of a command by UNIX. Processes can also be executed by the operating system itself. Like the file structure, the process structure is hierarchical. It contains parents, children, and even a root. A parent can fork (or spawn) a child process. That child can, in turn, fork other processes. The first thing the operating system does to begin execution is to create a single process, PID number 1. PID stands for Process Identification. This process will hold the same position as the root directory in the file structure. This process is the ancestor to all processes that each user works with. It forks a process for each terminal. Each one of these processes becomes a Shell process when the user logs in. 1.2 Process Identification The UNIX operating system assigns a unique process identification number (PID) to each process. It will keep the same PID as long as the process is in existence. During one session, the same process is always executing the login Shell. When you execute another command, a new process is forked and a new PID is assigned to that process. When that child process is finished, you are returned to the login process, which is running the Shell, and that parent process has the same PID as when you logged in. The Shell stores the PID in Shell variable called $$. The PID can also be shown with the process status (ps) command. The format for ps is as follows: ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: ps [options] º º º º See on-line manual for options º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ With no options given the ps command will give you certain information about processes associated with the controlling terminal. The output consists of a short listing containing the process id, terminal id, cumulative execution time, and the command name. Otherwise, options will control the display. Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $echo $$ ³ ³ 8347 ³ ³ $ps ³ ³ PID TTY TIME COMMAND ³ ³ 8347 rt021a0 0:03 ksh ³ ³ 8376 rt021a0 0:06 ps ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ The PID numbers of the Shell are the same in the sample session because the Shell will substitute its own PID number for $$. The Shell makes the substitution before it forks a new process to execute the echo command. Therefore, echo will display the PID number of the process that called it, not the PID of the process that is executing it. The -l option will display more information about the processes. Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $ps -l ³ ³ F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME COMD ³ ³ f0000 S 115 8347 309 2 30 20 1009000 140 94014 rt021a0 0:03 ksh ³ ³ f0000 O 115 8386 8347 16 68 20 1308000 72 rt021a0 0:01 ps ³ ³ $ps -l ³ ³ F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME COMD ³ ³ f0000 S 115 8347 309 1 30 20 1009000 140 94014 rt021a0 0:03 ksh ³ ³ f0000 O 115 8387 8347 26 73 20 1146000 72 rt021a0 0:01 ps ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ 1.3 Executing a Command When you give a command to the Shell, it will fork a process to execute the command. While the child process is executing the command, the parent will go to sleep. Sleeping means that the process will not use any CPU time. It remains inactive until it is awakened. When the child process has finished executing the command, it dies. The parent process, which is running the Shell, wakes up and prompts you for another command. When you request a process to run in the background (by ending the command line with an ampersand character (&), the Shell forks a child process that is allowed to run to completion. The parent process will report the PID of the child process and then prompt you for another command. The child and parent are now independent processes. 1.4 The . (dot) and exec Commands There are two ways to execute a program without forking a new process. The . (dot) command will execute the script as part of the current process. When the new script has finished executing, the current process will continue to execute the original script. The exec command will execute the new script in place of (overlays) the original script and never returns to the original script. The . (dot) command will not execute compiled files (binary) and it does not require execute permission on the script file that is being executed. The exec command does require access permission to either a binary program or a shell script. Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $ls -l prog2 ³ ³ -rw-r--r-- 1 teacher class 22 Jan 18 10:30 prog2 ³ ³ $cat prog2 ³ ³ echo 'prog2 PID =' $$ ³ ³ $cat dot_example ³ ³ echo $0 'PID=' $$ ³ ³ . prog2 ³ ³ echo 'This line is executed' ³ ³ $dot_example ³ ³ dot_example PID= 6942 ³ ³ prog2 PID = 6942 ³ ³ This line is executed ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ The exec command will overlay the sh and control will never return to the calling script. Let's look at another example with a call to prog2 using exec instead of . (dot): Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $ls -l prog2 ³ ³ -rwxr-xr-x 1 teacher class 22 Jan 18 10:30 prog2 ³ ³ $cat prog2 ³ ³ echo 'prog2 PID =' $$ ³ ³ $cat exec_example ³ ³ echo $0 'PID=' $$ ³ ³ exec prog2 ³ ³ echo 'This line is never executed' ³ ³ $exec_example ³ ³ exec_example PID= 6950 ³ ³ prog2 PID = 6950 ³ ³ $ ³ ³ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Background Processing When a program is running in background you do not have to wait for it to finish before starting another program. This is useful because you can start long/large jobs and then continue to do another task on your terminal. To run a program in background simply type an ampersand character (&) at the end of the command line before the (Ret) key. The Shell will return the PID of the background process and then give you another system prompt. Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $ls -l | lp & ³ ³ [1] 21334 ³ ³ $request id is mt_600-2736 (standard input) ³ ³ ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ If the background task sends output to standard output and you fail to redirect it, the output will appear on your terminal even if you are running another program at the time. It is necessary to use the kill command to stop a process that is running in background the (DEL) key or its equivalent will not work. Exit Status When a process stops executing for any reason, it will return an exit status to the parent process. This exit status is also referred to as a condition code or return code.The Shell stores the exit status in a Shell variable called $?. By convention, a non-zero exit status means that it has a false value and the command failed. On the other hand, a zero status indicates true and the command was successful. It is possible for you to specify the exit status when you exit a script. This is done by specifying the number to be used as the exit status using the exit command. The following script is an example: Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $cat exit_example ³ ³ echo 'This program returns an exit status' ³ ³ echo 'of 7.' ³ ³ exit 7 ³ ³ $exit_example ³ ³ This program returns an exit status ³ ³ of 7. ³ ³ $echo $? ³ ³ 7 ³ ³ $echo $? ³ ³ 0 ³ ³ $ ³ ³ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ This script will display the message and then exit with an exit code of 7. The exit status is stored in the Shell variable called $?. The second echo command above displays the exit status of the first echo command. Since it completed successfully it has a value of zero. 1.4 Interrupt Handling A signal is a report to a process about a condition. UNIX uses these signals to report bad system calls, broken pipes, illegal instructions, and other conditions. There are three signals that are useful when programming in the Shell. They are the terminal interrupt signal (number 2), the kill signal (number 9) and the software termination signal (number 15). You can use the trap command to capture a signal and then take whatever action you specify. It can close files or finish other processing that needs to be done, display a message, terminate execution immediately, or ignore the signal. ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: trap ['commands'] signal_numbers º º º º See online man pages for details º º º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ The signal_numbers are the numbers corresponding to the signals that will be trapped by the trap command. There must be at least one number present. The 'commands' portion of the command is optional. If it is not present, the command resets the trap to its initial condition, which is to exit the program. When the commands is present the Shell executes the commands when it catches one of the signals. After executing the commands, the Shell continues executing the script where it left off. You can interrupt a program you are running in the foreground by pressing the Delete key. When you press this key a signal (number 2), a terminal interrupt, to the program. The Shell will terminate the execution of the program if the program does not trap the signal. The following example demonstrates the trap command that will trap the signal and return an exit status of 1. Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $cat inter ³ ³ trap 'echo PROGRAM INTERRUPTED; exit 1' 2 ³ ³ while (true) ³ ³ do ³ ³ echo 'Program running' ³ ³ done ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ The first line of inter sets up a trap for signal number 2, the terminal interrupt. When the signal is caught, the Shell will execute the commands between the two single quote marks. In this example, the echo command will display PROGRAM INTERRUPTED. The exit command will then return control to the Shell and a system prompt is displayed. If the exit were missing, control would revert to the while loop after displaying the message. You can send a software termination to a background process using the kill command without a signal number. However, a trap command can be set to catch this signal (number 15). A kill signal can be sent to kill a process with a signal number 9 and the Shell cannot catch a kill signal. The file called .profile The BourneShell declares and initializes variables that determine such things as your home directory, what directories the Shell will look in when you give commands, how often to look for mail, your system prompt, and many other things. We will look at some of these Shell variables and their functions. You can assign new values to these variables from the command line or by executing the contents of a file called .profile. The BourneShell executes the commands in this file in the same environment as the Shell each time the user logs in. The .profile must be in the user' home directory. Each user has a different .profile. It usually specifies the terminal type and establishes terminal characteristics and other housekeeping functions as required by the user. 1.5 HOME The first BourneShell variable that we will look at is the HOME variable. By default, the home directory is the current working directory after you login. The system administrator determines your home directory when you establish an account and places that information in the /etc/passwd file. When you login, the BourneShell gets that pathname and assigns it to the HOME variable. When you enter a cd command with no argument, the utility takes the name of the directory from the HOME variable and makes it the current working directory. If you change the HOME variable to another directory pathname, the utility will make the new directory the current working directory. Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $echo $HOME ³ ³ /user0/rharding ³ ³ $cd ³ ³ $pwd ³ ³ /user0/rharding ³ ³ $HOME=/user0/rharding/eng ³ ³ $cd ³ ³ $pwd ³ ³ /user0/rharding/eng ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ This example shows how the value of the HOME variable affects the cd utility. The cd command will use the value of the HOME variable as the pathname for the current working directory. 1.6 PATH This BourneShell variable will describe the directories that will be searched looking for the program that you want to execute. The BourneShell looks in several directories for a file that has the same name as the command that you entered. The PATH variable controls this search path. Normally, the first directory searched is the current working directory. If the program is not found, the search continues in the /bin and then the /usr/bin directory. Generally, these directories contain executable programs. If the program is not found in one of these directories, the BourneShell reports that the program can't be found (or executed). The PATH variable lists the pathnames in the order in which the search will proceed. The pathnames are separated by a colon (:). If nothing (null string) precedes the colon, that indicates to start the search at the current working directory. Example: ................................................................. . $PATH=:/user0/rharding/bin:/bin:/usr/bin . . $ . ................................................................. This PATH variable indicates to start the search for the program at the current working directory, then look in the directory /user0/rharding/bin, then /bin, and finally /usr/bin. If each user has a unique path specified, each user can execute a different program by giving the same command. The search for the program stops when it is satisfied; thus, you can use the same name for your own programs as the standard UNIX utilities. To do this, simply put your program in one of the first directories that the BourneShell searches. 1.7 INGRES Environment Variables There are some environment variables that need to be in the .profile that set up INGRES. The following examples are given as general guidelines, not actual entries to be made in your .profile. 1.8 ING_HOME This is the INGRES home directory. This variable is valid for version 5 of INGRES. This variable is set up in the following manner. Example: ................................................................. . $ING_HOME=/user5/ingres . ................................................................. Notice that this environment variable is all capital letters. This is a requirement in UNIX. 1.9 TERM_INGRES If this variable is not set, INGRES will use the default terminal type defined by the TERM variable in UNIX. It is not required but difficulty in using the main INGRES menu can be experienced if it is not used. Example: ................................................................. . $TERM_INGRES=vt100f . ................................................................. 1.10 ING_EDIT This variable defines the editor to use any time a user enters a command that requires the use of an editor. The default is to use the 'ed' editor. Example: ................................................................. . $ING_EDIT=/usr/bin/vi . ................................................................. Workshop 1 This workshop will reinforce your understanding of the material presented in this chapter. Login using the username and the password given to you by the instructor. Each student is to complete the entire workshop. DESK EXERCISES 1. What is the name of the file that is executed from your home directory every time you log in? 2. What does the Shell variable HOME represent? 3. What does the Shell variable PATH represent? 4. What is a UNIX process? 5. When a command is given to the Shell it will fork a child process to execute the command. True/False 6. What is a process identification number (PID)? Continue on the next page 7. What is the purpose of the trap command? COMPUTER EXERCISES 8. Logon 9. What is the PID of your process? 10. Edit the .profile to include your home directory in the path. 11. Modify the .profile so every time you login a listing of the files in your current working directory (HOME) is displayed. 12. Send a long listing of all the files in the current working directory to the default printer and do it it the background. 13. Logoff NOTES ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ 2. COMPILING "C" PROGRAMS This chapter will examine compiling source code programs in three high level languages "C", FORTRAN, and COBOL. The second part of the chapter will look at the archive and library maintainer. The archive allows you to create a library of object modules. These files are used by the link editor. 2.1 "C": Sample Program with a Main and Two Functions in One File Based on the command line options, cc compiles, assembles, and loads C language source code programs. It can also assemble and load assembly language source programs or merely load object programs. ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: cc [options] file-list º º º º (See Appendix E for a complete list of options) º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ When using the cc utility, the following conventions are observed: 1. A filename with the extension of .c indicates a C language source program. 2. A filename with an extension of .s indicates an assembly language source program. 3. A filename with an extension of .o indicates an object program. The cc utility will take its input from the file or files you specify on the command line. Unless you use the -o option, it will store the executable program in a file called a.out. Sample C Language Source Code Program: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $cat hello.c ³ ³ main () ³ ³ { ³ ³ printf ("Hello from main!\n\n"); ³ ³ printf ("Calling function1!\n\n"); ³ ³ funct1(); ³ ³ printf ("\t Back from function1!\n\n"); ³ ³ printf ("Calling function2!\n\n"); ³ ³ funct2(); ³ ³ printf ("\t Back from funct2!\n\n"); ³ ³ printf ("That's all!\n\n"); ³ ³ } ³ ³ funct1() ³ ³ { ³ ³ printf ("\t\t Hello from function1!\n\n); ³ ³ } ³ ³ funct2() ³ ³ { ³ ³ printf ("\t\t Hello from function2!\n\n); ³ ³ } ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ 2.2 "C": Compiling a Program To compile the previous example program into an executable module, enter the following command at the command line. Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $cc hello.c ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Without any options, cc accepts C source code and assembly language programs that follow the conventions outlined above. It will compile, assemble, and load these programs to produce an executable called a.out. The cc utility puts the object code in files with the same base filename (everything before the period) as the source but with a filename extension of .o. The a.out stands for assembly output. This is the default. Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $cc hello.c ³ ³ $a.out ³ ³ Hello from main! ³ ³ ³ ³ Calling function1! ³ ³ ³ ³ Hello from function1! ³ ³ ³ ³ Back from function1! ³ ³ ³ ³ Calling function2! ³ ³ ³ ³ Hello from function2! ³ ³ ³ ³ Back from function2! ³ ³ ³ ³ That's all! ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ NOTE: The a.out file that was created by the cc utility has the following permissions: user - read, write, and execute group - read and execute other - read and execute It is not necessary for you to change the permissions using the chmod command because the cc utility set the execute permissions for you. 2.3 "C": Renaming the Executable Module You can rename the executable module using the mv command. The file permissions will be the same as before the file is renamed. Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $mv a.out hello ³ ³ $hello ³ ³ Hello from main! ³ ³ ³ ³ Calling function1! ³ ³ ³ ³ Hello from function1! ³ ³ ³ ³ Back from function1! ³ ³ ³ ³ Calling function2! ³ ³ ³ ³ Hello from function2! ³ ³ ³ ³ Back from function2! ³ ³ ³ ³ That's all! ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ 2.4 "C": Giving a Name to the Output File It is possible to have the output sent to a file you specify instead of a.out by using the following command. ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: cc -o output source º º º º output - the name of the executable file º º º º source - the name of the C source code file º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ The -o option tells cc to tell the link editor to use the specified name for the output instead of the default a.out. NOTE: It is not necessary for the -o option to appear after the cc command. The filename that appears after the -o is the name of the output file. For example, cc source -o output is the same as cc -o output source. Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $cc -o hello.c ³ ³ $hello ³ ³ Hello from main! ³ ³ Calling function1! ³ ³ ³ ³ Hello from function1! ³ ³ ³ ³ Back from function1! ³ ³ ³ ³ Calling function2! ³ ³ ³ ³ Hello from function2! ³ ³ ³ ³ Back from function2! ³ ³ ³ ³ That's all! ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ 2.5 "C": Producing an Assembly Listing This option causes cc to compile C programs and leave the corresponding assembly language source programs in a file with filename extensions of .s. ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: cc -S hello.c º º º º -S = Compile only º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $cc -S hello.c ³ ³ $ls -C ³ ³ example.f hello hex.c octal.c ³ ³ hello.c hello.s multiply.c ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ 2.6 "C": Main and Two Functions in Three Separate Source Files This is the same C program that we have seen before, except it is now in three files rather than one as before. The three files are main.c, funct1.c, and funct2.c. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $cat main.c ³ ³ main () ³ ³ { ³ ³ printf ("Hello from main!\n\n"); ³ ³ printf ("Calling function1!\n\n"); ³ ³ funct1(); ³ ³ printf ("\t Back from function1!\n\n"); ³ ³ printf ("Calling function2!\n\n"); ³ ³ funct2(); ³ ³ printf ("\t Back from funct2!\n\n"); ³ ³ printf ("That's all!\n\n"); ³ ³ } ³ ³ $cat funct1.c ³ ³ funct1() ³ ³ { ³ ³ printf ("\t\t Hello from function1!\n\n); ³ ³ } ³ ³ $cat funct2.c ³ ³ funct2() ³ ³ { ³ ³ printf ("\t\t Hello from function2!\n\n); ³ ³ } ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ 2.7 "C": Compiling but Not Producing an Executable Module Using the previous program, the following command will compile but not produce an executable module. ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: cc -c main.c funct1.c funct2.c º º º º -c = Compile, but do not load object files. This option º º causes cc to compile and/or assemble source code º º programs and leave the corresponding object programs º º in files with filename extensions of .o. º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $cc -c main.c funct1.c funct2.c ³ ³ main.c: ³ ³ funct1.c: ³ ³ funct2.c: ³ ³ $ls a.out ³ ³ a.out not found ³ ³ $ls -C *.o ³ ³ funct1.o funct2.o main.o ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ The -c options causes the compilation system to suppress the link edit phase. This produces an object file or files, in this example (main.o funct1.o funct2.o), that can be link edited at a later time with the cc command with no options. 3. COMPILING FORTRAN PROGRAMS 3.1 FORTRAN: Sample Program a Main and Two Subroutines There are several conventions for use with the FORTRAN compiler. They are: 1. The name of the file containing the FORTRAN source code must end with .f. 2. The compiler is invoked with f77. 3. Several options are available with the compiler. (-c, -o, -p, -S) 4. Preconnections are made for stdin (unit5) and stdout (unit6). This is the FORTRAN source code example to be used in the following discussions of the FORTRAN compiler. Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $cat hello.f ³ ³ program calling ³ ³ write(6,100) ³ ³ 100 format (' Hello from main!',/) ³ ³ write(6,110) ³ ³ 110 format(' Calling subroutine1!',/) ³ ³ call sub1 ³ ³ write(6,120) ³ ³ 120 format(t15' Back from subroutine1!',/) ³ ³ write(6,130) ³ ³ 130 format(' Calling subroutine2!',/) ³ ³ call sub2 ³ ³ write(6,140) ³ ³ 140 format(t15' Back from subroutine2!',/) ³ ³ write(6,150) ³ ³ 150 format(' That's all, folks!') ³ ³ end ³ ³ subroutine sub1 ³ ³ write(6,200) ³ ³ 200 format(t20,' Hello from subroutine1!',/) ³ ³ end ³ ³ subroutine sub2 ³ ³ write(6,210) ³ ³ 210 format(t20,' Hello from subroutine2!',/) ³ ³ end ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ 3.2 FORTRAN: Compiling a Program The FORTRAN compiler is invoked with the following command: ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: f77 º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ To compile the above program into an executable program, use the following command at the command line. Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $f77 hello.f ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Without any options, f77 accepts FORTRAN source code and assembly language programs that follow the conventions outlined above. It will compile, assemble, and load these programs to produce an executable called a.out. The f77 utility outputs the object code into files with the same base filename (everything before the period) as the source but with a filename extension of .o. The a.out stands for assembly output. This is the default. Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $f77 hello.f ³ ³ $a.out ³ ³ Hello from main! ³ ³ ³ ³ Calling function1! ³ ³ ³ ³ Hello from function1! ³ ³ ³ ³ Back from function1! ³ ³ ³ ³ Calling function2! ³ ³ ³ ³ Hello from function2! ³ ³ ³ ³ Back from function2! ³ ³ ³ ³ That's all! ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ NOTE: The a.out file that was created by the f77 utility has the following permissions: user - read, write, and execute group - read and execute other - read and execute It is not necessary for you to change the permissions using the chmod command because the f77 utility set the execute permissions for you. 3.3 FORTRAN: Renaming the Executable Module You can rename the executable module using the mv command. The file permissions will be the same as before the file is renamed. Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $mv a.out hello ³ ³ $hello ³ ³ Hello from main! ³ ³ ³ ³ Calling function1! ³ ³ ³ ³ Hello from function1! ³ ³ ³ ³ Back from function1! ³ ³ ³ ³ Calling function2! ³ ³ ³ ³ Hello from function2! ³ ³ ³ ³ Back from function2! ³ ³ ³ ³ That's all! ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ 3.4 FORTRAN: Giving a Name to the Output File It is possible to have the output sent to a file you specify instead of the default, a.out, by using the following command. ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: f77 -o output source º º º º output - the name of the executable file º º º º source - the name of the Fortran source code file º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ The -o option tells the f77 utility to tell the link editor to use the specified name for the output instead of the default a.out. NOTE: It is not necessary for the -o option to appear after the f77 command. The filename that appears after the - o is the name of the output file. For example, f77 source -o output is the same as f77 -o output source. Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $f77 -o hello.f ³ ³ $hello ³ ³ Hello from main! ³ ³ Calling function1! ³ ³ ³ ³ Hello from function1! ³ ³ ³ ³ Back from function1! ³ ³ ³ ³ Calling function2! ³ ³ ³ ³ Hello from function2! ³ ³ ³ ³ Back from function2! ³ ³ ³ ³ That's all! ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ 3.5 FORTRAN: Producing an Assembly Listing This option causes f77 to compile Fortran programs and leave the corresponding assembly language source programs in a file with filename extensions of .s. ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: f77 -S hello.f º º º º -S = Compile only º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $f77 -S hello.f ³ ³ $ls -C ³ ³ example.f hello hex.c octal.c ³ ³ hello.c hello.s multiply.c ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ The file hello.s contains the assembly listing. 3.6 FORTRAN: Main and Two Subroutines in Three Separate Source Files Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $cat main.f ³ ³ program calling ³ ³ write(6,100) ³ ³ 100 format (' Hello from main!',/) ³ ³ write(6,110) ³ ³ 110 format(' Calling subroutine1!',/) ³ ³ call sub1 ³ ³ write(6,120) ³ ³ 120 format(t15' Back from subroutine1!',/) ³ ³ write(6,130) ³ ³ 130 format(' Calling subroutine2!',/) ³ ³ call sub2 ³ ³ write(6,140) ³ ³ 140 format(t15' Back from subroutine2!',/) ³ ³ write(6,150) ³ ³ 150 format(' That's all, folks!') ³ ³ end ³ ³ $cat sub1.f ³ ³ subroutine sub1 ³ ³ write(6,200) ³ ³ 200 format(t20,' Hello from subroutine1!',/) ³ ³ end ³ ³ $cat sub2.f ³ ³ subroutine sub2 ³ ³ write(6,210) ³ ³ 210 format(t20,' Hello from subroutine2!',/) ³ ³ end ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ 3.7 FORTRAN: Compiling But Not Producing an Executable Module Using the above program, the following command will compile but not produce an executable module. ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: f77 -c main.f sub1.f sub2.f º º º º -c = Compile, but do not load object files. This option º º causes f77 to compile and/or assemble source code º º programs and leave the corresponding object programs º º in files with filename extensions of .o. º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $f77 -c main.f sub1.f sub2.f ³ ³ main.f: ³ ³ MAIN: calling: ³ ³ sub1.f: ³ ³ sub1: ³ ³ sub2.f: ³ ³ sub2: ³ ³ $ls a.out *.o ³ ³ a.out not found ³ ³ funct1.o ³ ³ funct2.o ³ ³ hello.o ³ ³ main.o ³ ³ sub1.o ³ ³ sub2.o ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ The -c options causes the compilation system to suppress the link edit phase. This produces an object file or files, in this example (main.o sub1.o sub2.o), that can be link edited at a later time with the f77 command with no options. 3.8 FORTRAN: Compiling Object Files to Produce an Executable Module The command to produce an executable nodule from several object files is done in the following manner: ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: f77 obj_1 obj_2 obj_3 º º º º obj_1 through obj_n - the object files º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $f77 main.o sub1.o sub2.o ³ ³ $ls -C ³ ³ funct1.o funct2.o hello.o main.o sub1.o sub2.o a.out ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ 4. COMPILING COBOL PROGRAMS 4.1 COBOL: Sample Program with a Main and Two Subroutines Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $cat teacher.cob ³ ³ identification division. ³ ³ program-id. teacher. ³ ³ environment division. ³ ³ configuration section. ³ ³ data division. ³ ³ working-storage section. ³ ³ procedure division. ³ ³ begin section. ³ ³ begin-it. ³ ³ display " Hello from main!". ³ ³ display " Calling subroutine1!". ³ ³ perform subroutine1. ³ ³ display " Back from subroutine1!". ³ ³ display " Calling subroutine2!". ³ ³ perform subroutine2. ³ ³ display " Back from subroutine2!". ³ ³ display " That's all, folks!". ³ ³ stop run. ³ ³ subroutine1 section. ³ ³ sub1. ³ ³ display " Hello from subroutine1!". ³ ³ subroutine2 section. ³ ³ sub2. ³ ³ display " Hello from subroutine2!". ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ 4.2 COBOL: Compiling a Program ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: cobol source_filename º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ Three files are created by the compiler. They are identified by the same filename as the source code but with a different extension. They have the extensions .IDY, .INT, and .LST. NOTE: These extensions are uppercase characters. UNIX is case sensitive. Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $cobol teacher.cob ³ ³ $ls teacher* ³ ³ teacher.IDY ³ ³ teacher.INT ³ ³ teacher.LST ³ ³ teacher.cob ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ 4.3 COBOL: Running a Program ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $cbrun teacher.INT ³ ³ Hello from Main! ³ ³ Calling subroutine1! ³ ³ Hello from subroutine1! ³ ³ Back from subroutine1! ³ ³ Calling subroutine2! ³ ³ Hello from subroutine2! ³ ³ Back from subroutine2! ³ ³ That's all, folks! ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ NOTES ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ Workshop 2 through 4 This workshop will reinforce your understanding of the ideas presented in this chapter. Login using the username and password given to you by the instructor. Each student is to complete the entire workshop. DESK EXERCISES 1. "C": What is the command to compile, assemble, and load source code programs? 2. "C": What is the filename extension that indicates a source code program? An assembly language program? An object code file? 3. "C": What is the default filename assigned to the executable file? 4. "C": What command can be used to rename the executable file produced by the cc compiler? What are the file protections associated with the executable? 5. "C": What option will produce an assembly listing? What is the filename extension of this file? Continue on the next page 6. "C": What command will compile the source code program but will not load object files but will keep the object files in files with extensions of .o? 7. FORTRAN: What is the command to invoke the compiler? 8. FORTRAN: What is the filename extension for source code programs? 9. FORTRAN: What is the name of the default executable file? 10. FORTRAN: How can you change the permissions on the executable module so anyone can execute it? 11. FORTRAN: What option on the call to the compiler will allow you to specify the name of the executable file? Continue on the next page 12. FORTRAN: What option on the call to the compiler will produce an assembly listing? What is the filename extension of this file? 13. FORTRAN: What option will produce object modules but not produce an executable module? 14. FORTRAN: What command will produce an executable module from several object modules? 15. COBOL: What is the command to call the compiler? 16. COBOL: What are the three files created by the compiler? What are the filename extensions? 17. COBOL: Which of the three files that have been created are used to run the program? Continue on the next page COMPUTER EXERCISES 18. Copy the following files from the directory teacher: main.c funct1.c funct2.c Are these programs "C", FORTRAN, or COBOL? Compile these three files creating an executable file called main1.exe and then execute it. What are the file protections? Why? 19. Now append the three files into one file. Use output redirection. Compile the file creating the executable file called main2.exe. Execute main2.exe. 20. Copy the following files from teacher into your home directory: main.f sub1.f sub2.f Compile these three files creating an executable file called main1.exe. Execute main1.exe 21. Now append the three files into one file. Compile the file creating the executable file called main2.exe. Execute main2.exe. 22. COBOL: Copy teacher.cob from teacher. Compile and run. 5. THE make UTILITY The make utility is used to keep a set of executable programs current. This is based on the modification times of the programs and the source code that each program is dependent upon. The utility will look at the dependency lines in a file called makefile in the current working directory. These dependency lines indicate relationships between files, specifying a target file that is dependent on one or more prerequisite files. If you modified any of the prerequisite files more recently than the target file, make will update the target file based on construction commands that follow the dependency lines. ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: make [options] [target_files] º º º º See the online man pages for a detailed list of options º º º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ The target_files refer to targets on dependency lines in the file called makefile. If you do not specify a target_file, make will update the first dependency line it finds in makefile. The makefile has the following construction: target: prerequisite_list tab construction_commands The dependency line is composed of target and the prerequisite_list, separated by a colon. The construction_commands must start with a tab character and must follow the dependency line. The target is the name of the file that is dependent on the files in the prerequisite_list. The construction_commands are shell commands that construct the target, these are usually compile commands. The make utility will execute the construction_commands when the modification time of one or more of the files in the prerequisite_list is more recent than the target. Sample makefile: payroll: sales.c salary.c cc sales.c salary.c -o payroll In the example, the target is called payroll. It is dependent on sales.c and salary.c. If the modification time of either of these is more recent than payroll, the construction_commands will be executed. In this case, the source code programs are compiled and stored in payroll. In the previous example, to get the update to occur simply type make. Example: ................................................................. . $make . ................................................................. Since no target was specified, the first dependency line is the one that make will attempt to execute. Each of the prerequisites on one dependency line can be a target on other dependency lines. This nesting of specifications can continue, creating a complex hierarchy that can specify a large system of programs. Sample makefile: form: size.o length.o cc size.o length.o -o form size.o: size.c form.h cc -c size.c length.o: length.c form.h cc -c length.c form.h: num.h table.h cat num.h table.h > form.h Notice that form is dependent on two object files, size.o and length.o. These two object files are, in turn, dependent upon their respective source code programs and the header file, form.h. The header file is dependent upon two other header files. Note that the construction_commands for form.h can use any shell command, in this case cat creates the header file. This makefile can be quite difficult to write, especially if there are a number of interdependencies. The make utility can rely upon implied dependencies and construction_commands to make your job of writing the makefile easier. If you do not include a dependency line for a file, make assumes that object program files are dependent on compiler or assembler source code files. If a prerequisite for a target file is .o and .o is not a target with its own prerequisites, make will search for one of the following files in the current working directory. Filename Type of file .c C source code .f FORTRAN source code .s Assembler source code If you do not include a construction_command for one of the files listed, make will create a default construction_command line that will call the appropriate compiler or assembler to create the object file. grep: A PATTERN MATCHING FILTER The grep utility can search through a file to see if it contains a specified string of characters. The utility will not change the file it searches but displays each line that contains the string. The format for the string is as follows. ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: grep [options] limited_regular-expression [file] º º º º Use the man command for a complete list of options º º º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ The grep utility searches files for a pattern and displays all lines that contain the pattern. It uses limited-regular- expressions (these are expressions that have string values that use a subset of all the possible alphanumeric and special characters) like those used with ed to match the patterns. Be careful using the characters $, *, [, ^, |, (, ), and \ in the regular expression because they will be evaluated by the Shell. It is good practice to enclose the regular expression in single quotes. This will prevent the Shell from evaluating these special characters. The grep utility will assume standard input if no files are given. Normally, each line found in the file will be displayed to standard output. Sample session: ................................................................. . $grep 'disc' memo . ................................................................. This command will search the file "memo" for the string "disc". It will include words like discover and indiscreet because they contain the characters "disc". The single quote marks are not necessary, and for this example, they wouldn't have made any difference. They do allow you to include spaces in the search pattern. 5.0.1 More on Regular Expressions The grep command can be best understood by a discussion of regular expressions. Let's create a database of phone numbers called phone.lis and then use regular expressions to search through the database. Here is as listing of the contents of phone.lis Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $cat phone.lis ³ ³ Smith, Joan 7-7989 ³ ³ Adams, Fran 2-3876 ³ ³ StClair, Fred 4-6122 ³ ³ Jones, Ted 1-3745 ³ ³ Stair, Rich 5-5972 ³ ³ Benson, Sam 4-5587 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ The format for the records in this database is: Last name, First name #-#### Using the database (phone.lis) above. What grep command would we use to search through the database and get all the records that had a person whose name contains an "S". An alphabetic character represents itself. Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $grep S phone.lis ³ ³ Smith, Joan 7-7989 ³ ³ StClair, Fred 4-6122 ³ ³ Stair, Rich 5-5972 ³ ³ Benson, Sam 4-5587 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ This grep command searched for the string "S" and then listed all the lines in phone.lis that matched. A single . (dot) is used to represent any single character. Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $grep .S phone.lis ³ ³ Benson, Sam 4-5587 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ A $ represents the end of the line. Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $grep 5$ phone.lis ³ ³ Jones, Ted 1-3745 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ A ^ represents the beginning of the line Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $grep ^S phone.lis ³ ³ Smith, Joan 7-7989 ³ ³ StClair, Fred 4-6122 ³ ³ Stair, Rich 5-5972 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Regular expressions must get to grep in order for them to be evaluated properly. Let's say we want to get the records of employees that have a phone number that begins with a "4". What does the following expression do? Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $grep 4 phone.lis ³ ³ StClair, Fred 4-6122 ³ ³ Jones, Ted 1-3745 ³ ³ Benson, Sam 4-5587 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Why did we get the record of Ted Jones? The tab character was evaluated by the Shell and so the search was actually made looking for a "4". This is the same as if we had entered $grep 4 phone.lis. We must prevent the Shell from evaluating these characters, this is done with the \ (backslash) character as shown in the next example. Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $grep \4 phone.lis ³ ³ StClair, Fred 4-6122 ³ ³ Benson, Sam 4-5587 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Now it worked properly. It searched for a character followed by the number 4. The [] (left and right brackets) are used to identify a range of characters. Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $grep \[AF] phone.lis ³ ³ Adams, Fran 2-3876 ³ ³ StClair, Fred 4-6122 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Why do [] need to be quoted? In the previous example the search makes a match on "A" or "F". A - (dash) can indicate inclusion. For example, we want to make a match on a phone number that has a 1, 2, 3, or 4. How can this be done? Here's an example: Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $grep \[1-4] phone.lis ³ ³ Adams, Fran 2-3876 ³ ³ StClair, Fred 4-6122 ³ ³ Jones, Ted 1-3745 ³ ³ Stair, Rich 5-5972 ³ ³ Benson, Sam 4-5587 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ A ^ character looks for all characters NOT inside the [] brackets. For example, [^0-9] matches all non-digits [^a-zA-Z] matches all non-alphabetic characters NOTE: \, *, and $ lose their metacharacter meanings inside the []. Also the ^ character is special only if it appears first. What is the following command searching for? Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $grep '[^789]$' phone.lis ³ ³ Adams, Fran 2-3876 ³ ³ StClair, Fred 4-6122 ³ ³ Jones, Ted 1-3745 ³ ³ Stair, Rich 5-5972 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ 5.0.2 Still More Regular Expressions The * (asterisk) represents zero or more of the characters preceding the asterisk. A* represents 0 or more As. AA* represents 1 or more As. [0-9]*$ 0 or more digits at the end of a line (last four digits in a phone number) .* represents 0 or more of any character. How would you write a grep command using regular expressions to find the last name starting with an "S" and the first name with an "F"? ^S Begins with an "S" .*,F Any number of characters before ,F Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $grep ^S.\*,F phone.lis ³ ³ StClair, Fred 4-6122 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ NOTE: The * (asterisk) was quoted so the Shell didn't try to evaluate it. It is very desirable to quote the entire string to keep the Shell from doing an expansion or substitution. It also increases readability of the regular expression as in the following example. Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $grep '^S.*, F' phone.lis ³ ³ StClair, Fred 4-6122 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ 5.0.3 Some Nice grep Options The grep provides several options that modify how the search is performed. -c Report count of matching lines only -v Print those lines that don't match the pattern. What will these lines print? Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $grep -c '[J-Z]' phone.lis ³ ³ 5 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Why did we get this result? Let's analyze the command. In English, this command could be interpreted to mean "Tell me how many records in the file "phone.lis" contain a letter from the set J through and including Z." Look at the phone.lis file and see that five records fit this restriction. So the answer is 5. Now look at another example and see what this one does. Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $grep -v '[J-Z]' phone.lis ³ ³ Adams,Fran 2-3876 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Why is this the only record that was found? The -v option says to select records that don't match the pattern. This is the same pattern as the previous example and therefore it selects records that don't match the pattern. The "Adams" record is the only one that doesn't make a match. It doesn't have a character from the set J through and Z. 5.0.4 Summary of Regular Expression Characters ^ Beginning of the line $ End of the line * 0 or more preceding characters . Any single character [...] A range of characters [^...] Exclusion range of characters sed: EDIT A FILE TO STANDARD OUTPUT UNIX provides a method of editing streams of data. It is the sed utility. The name of this utility is derived from Stream EDitor. This is not the same as the vi editor. The vi editor edits text in a file. The sed utility edits text in a stream. In order to edit a character stream two things are required. First, the line to edit must be identified (regular expressions) and second, how to edit the line. ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: sed [-n] [-e script] [-f sfile] [files] º º º º Details in on-line man pages º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ The sed utility copies the named files (standard input default) to the standard output, edited according to a set (script) of commands. The -f options cause the script to be taken from file "sfile". The general form is: $sed /address/instruction NOTE: If no address is specified, all lines are chosen to edit. 'sed' addresses can be line numbers or regular expressions. Example: line numbers 2,4 2,$ ($ represents the last line) textual address /regular-expression/ NOTE: Forward slashes enclose textual addresses The sed instructions indicate what editing function to perform. Here some useful sed instructions: s substitute d delete NOTE: Most sed command lines contain spaces or metacharacters and they should be quoted to protect them from the Shell. There are many more editing commands provided by sed. The following is a sample sed command to edit the records in the database file that we are already familiar with; namely, phone.lis. Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $sed /s/Smith/Smythe/ phone.lis ³ ³ Smythe, Joan 7-7989 ³ ³ Adams, Fran 2-3876 ³ ³ StClair, Fred 4-6122 ³ ³ Jones, Ted 1-3745 ³ ³ Stair, Rich 5-5972 ³ ³ Benson, Sam 4-5587 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ sed is an editor. It simply copies the standard input to the standard output, editing the lines that match the indicated address. The original file is not changed. Here's another example of a sed command. Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $sed '2,4 s/2$/3/' phone.lis ³ ³ Smith, Joan 7-7989 ³ ³ Adams, Fran 2-3876 ³ ³ StClair, Fred 4-6123 ³ ³ Jones, Ted 1-3745 ³ ³ Stair, Rich 5-5972 ³ ³ Benson, Sam 4-5587 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ What does this sed command do? If you read command in English it reads like this: On lines 2 through 4 substitute the 2 at the end of the line with a 3. Notice that the phone number for StClair, Fred changed from 4-6122 to 4-6123. The number for Stair, Rich didn't change because it was outside the range. The sed utility can also be use to delete parts of a line of data. This is done by substituting nothing for the parts you want to delete. It looks like this: Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $sed 's/^.*, //' phone.lis ³ ³ Joan 7-7989 ³ ³ Fran 2-3876 ³ ³ Fred 4-6122 ³ ³ Ted 1-3745 ³ ³ Rich 5-5972 ³ ³ Sam 4-5587 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Reading this command it means: Substitute from the beginning of the line followed by any number of characters followed by a comma with the null string (nothing). This has the effect of removing the text. Here's a delete command and how it's used. Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $sed d phone.lis ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Why is there no output? Well, it read standard input and did the editing function on all the selected lines. Since no lines were specified all lines were selected to be edited. The editing was to delete the line. Question: Has the original file been destroyed? Multiple commands are allowed in sed. Each instruction is applied to each input line. Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $sed '/Stair/d ³ ³ >/Adams/d' phone.lis ³ ³ Smith, Joan 7-7989 ³ ³ StClair, Fred 4-6122 ³ ³ Jones, Ted 2-1136 ³ ³ Benson, Sam 4-5587 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ The records for Adams and Stair have both been removed from the database. NOTE: The > character is the BourneShell secondary prompt. awk: A PATTERN MATCHING PROGRAMMING LANGUAGE Suppose you wanted to change the format of the database phone.lis to be the first name followed by the last name. There is no easy way to do this with sed. Fortunately, UNIX not only provides a stream editor (sed) but it also has a formatting tool. The formatting tool in UNIX is called awk. This tool is named after authors who wrote it Alfred V. Aho, Peter J. Weinberger, and Brian W. Kerninghan so it really doesn't have any meaning. The awk utility is a pattern scanning and processing language. It will search one or more files for a specified pattern and then performs an action, such as writing to standard output or incrementing a counter when it finds a match. You can use awk to generate reports or filter text. It works equally well with numbers or text. The authors designed it to be easy to use and sacrificed execution speed toward this end. While the sed utility allows us to change the text in a stream, awk allows us to easily rearrange, add, or delete text in a stream. The awk takes advantage of many constructs from the C programming language. It has the following features: flexible format conditional execution looping statements numeric variables string variables regular expressions C's printf The awk will take its input from the files you specify on the command line or from standard input. The following is the format for awk: ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command format: awk [-Fc] [prog] [files] º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ The awk will scan each line of file for lines that match a set of patterns specified by prog. With each pattern in prog there can be an associated action to be performed when the line is found. The set of patterns may appear literally as prog, or in a file specified as -f file. The prog string should be enclosed in single quotes to protect it from the Shell. Files are read in order and if there are none specified the standard input is read. Each line is matched against the pattern portion of every pattern-action statement. The associated action is performed for each matched pattern. An input line is made up fields separated by white space. $1, $2.. define the fields. $0 refers to the whole line. A pattern-action statement has the form: pattern {action} A missing action means print the line; a missing pattern always makes a match. A statement can be one of the following: if (conditional) statement [else statement] while (conditional) statement for (expression;conditional;expression) statement break continue {[statement]...} variable=expression print [expression-list] [>expression] printf format [,expression-list][>expression] next # skip remaining pattern on this input line exit # skip the rest of the input Statements are terminated by semicolons, new lines (Ret), or right braces. Let's look at the syntax for awk in a little simpler manner. awk 'commands' [filename] An awk program (commands) consists of a optional pattern to match and an action to perform if a match is found on the current line. This syntax looks like this: awk '/pattern/{action}' [filename] The pattern used is a regular expression enclosed in forward slashes. If no pattern is listed, the action will be performed for every line. An action can contain several commands. There can be multiple patterns and actions. awk '/pattern1/{action1} /pattern2/{action2}' [filename} One of awk's commands is print. It puts the current line on standard output. Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $awk '{print}' phone.lis ³ ³ Smith, Joan 7-7989 ³ ³ Adams, Fran 2-3876 ³ ³ StClair, Fred 4-6122 ³ ³ Jones, Ted 1-3745 ³ ³ Stair, Rich 5-5972 ³ ³ Benson, Sam 4-5587 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ The awk splits every input line at whitespace and keeps track of the number of fields on each line and counts the number of lines read. Each field is identified by its field number and a $. $1 Identifies the first field $2 Identifies the second field . $0 Identifies the entire line NF Identifies the number of fields on the line NR Identifies the number of lines that have been read Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $awk '{print NR,$1}' phone.lis ³ ³ 1 Smith, ³ ³ 2 Adams, ³ ³ 3 StClair, ³ ³ 4 Jones, ³ ³ 5 Stair, ³ ³ 6 Benson, ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ To change the order of the names in phone.lis, use awk. The comma in the print command tells awk to separate each field with a space. Without the comma, the output would have no spacing. Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $awk '{print $2, $1 ""$3}' phone.lis ³ ³ Joan Smith, 7-7989 ³ ³ Fran Adams, 2-3876 ³ ³ Fred StClair, 4-6122 ³ ³ Ted Jones, 1-3745 ³ ³ Rich Stair, 5-5972 ³ ³ Sam Benson, 4-5587 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ sort: SORT A FILE The sort utility sorts line of all the named files together and writes the result to standard output. The standard input is used if - is used as a file name or no input files are specified. Comparisons are based one or more sort keys extracted from each line of input. There is only one key by default, that's the entire line, and ordering is lexicographic by bytes in machine collating sequence. ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command format: sort [-cmu][-ooutput][-ykmem][-zrecsz] º º [-dfiMnr][-btx][+pos][-pos2][files] º º º º See on-line manual for options etc. º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ The easiest way to use sort is to add it at the end af a pipeline. What does the following command line accomplish: Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $grep '[45]' phone.lis | sed 's//73/' | sort ³ ³ Benson, Sam 734-5587 ³ ³ StClair, Fred 734-6122 ³ ³ Stair, Rich 735-5972 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ The grep command will select only those records that have a 4 of a 5 in the phone number, those records are then sent to sed which will add "73" just after the tab character, then the records are sent to sort and put in alphabetical order. Notice that there is a problem here, should StClair come before Stair in an alphabetical listing? The answer is NO. Why did this happen? It occurred because of the collating sequence for the default sort. This can be fixed by specifying some options on the call to the sort utility. Here are some options for sort. Let's see if we can determine how to remedy the problem discovered in the default sort. sort options: -f Fold lower case into upper case -r Reverse the sort from highest to lowest -b Ignore leading blank spaces -d Dictionary sort - ignore nonalphanumeric characters -m Merge two sorted files together -n Sort the list as numbers not digit characters Notice the -f options folds lower case into upper case. This option will make the sort for our problem work correctly. Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $grep '[45]' phone.lis|sed 's//73/'|sort -f ³ ³ Benson, Sam 734-5587 ³ ³ Stair, Rich 735-5972 ³ ³ StClair, Fred 734-6122 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ The sort can also be directed to use only a portion of the line as a sorting key versus the entire line. The utility will automatically break each line into fields at whitespace delimiters. You can use a character other than whitespace by using the -t option. The fields are set up like this: 0 1 2 /----|/---|/-------------| Adams, Fran 2-3876 In order to sort by the second field, here is the sort command to enter. Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $sort +1 phone.lis ³ ³ Adams, Fran 2-3876 ³ ³ StClair, Fred 4-6122 ³ ³ Smith, Joan 7-7989 ³ ³ Stair, Rich 5-5972 ³ ³ Benson, Sam 4-5587 ³ ³ Jones, Ted 1-3745 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Here's a sample of a sort on the 3rd field. Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $sort +2 phone.lis ³ ³ Jones, Ted 1-3745 ³ ³ Adams, Fran 2-3876 ³ ³ Benson, Sam 4-5587 ³ ³ StClair, Fred 4-6122 ³ ³ Stair, Rich 5-5972 ³ ³ Smith, Joan 7-7989 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ A sort can also be performed by a character position within a field. Sample session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $sort +2.4 phone.lis ³ ³ StClair, Fred 4-6122 ³ ³ Benson, Sam 4-5587 ³ ³ Jones, Ted 1-3745 ³ ³ Adams, Fran 2-3876 ³ ³ Stair, Rich 5-5972 ³ ³ Smith, Joan 7-7989 ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ NOTE: The first character of a field is the delimiter for that field. 5.1 ARCHIVER AND LIBRARY MAINTAINER This command will maintain groups of files combined into a single archive file. The main use of ar is to create and update library files as used by the link editor. It can also be used for any other similar purpose. The file header consists of printable ASCII characters. If the archive consists of printable characters, then the entire archive is also printable. ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: ar key [posname] afile [name]... º º º º Unlike command options, the command key is required. The key,º º usually a - sign, is formed with one of the following letters º º drqtpmx. Arguments to the key are made from one or more of º º the following set, vuaibcis. See Appendix I for a complete º º list of command keys. º º º º posname is an archive member name used as a reference for º º positioning other files in the archive. º º º º afile is the name of the archive. º º º º name[s] are the constituent files in the archive. º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ To illustrate how to create and use an archive file, we will use the "C" program called main.c and the two functions, funct1.c and funct2.c. First, create the object files that we intend to put into the archive file. Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $cc -c main.c funct1.c funct2.c ³ ³ main.c: ³ ³ funct1.c: ³ ³ funct2.c: ³ ³ $ls -C *.o ³ ³ funct1.o funct2.o main.o ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Remember the -c option will not produce an executable module, but it does create the object modules. These object modules are file files that we will place into an archive. 5.2 Creating an Archive File with Object Modules In this call to ar, we will use the r command key which will replace the named files in the archive. The v option will give a verbose file-by-file description of the making of the new archive file. Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $ar rv functs.a funct1.o funct2.o ³ ³ a - funct1.o ³ ³ a - funct2.o ³ ³ ar: creating functs.a ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ The name of the new archive file is functs.a. The files that have been added to that archive are funct1.o and funct2.o. The file protections for the new archive file are rw-r--r--. 5.3 Verifying the Contents of the Archive File The key command to list the table of contents is t. The t command will print a table of contents of the archive file. When the v option is used with the t command it will give a long listing of all information about the files. Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $ar tv functs.a ³ ³ rw-r--r-- 115/ 200 448 Sep 27 09:56 1990 funct1.o ³ ³ rw-r--r-- 115/ 200 448 Sep 27 09:56 1990 funct2.o ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ This output shows that there are two members in this archive file, namely, funct1.o and funct2.o. The protections of these files is: owner - read and write group - read other - read The fields are, left to right, the file protections, owner, group, size (in bytes), creation date and time, and finally the name of the constituent. 5.4 Removing Duplicate Object Files Once the archive has been created and verified, the object files in your directory can be deleted. This can be accomplished with the rm command. Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $rm funct?.o ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ The question mark (?) is a wildcard that stands for any single character. The files funct1.o and funct2.o no longer exist in your subdirectory. 5.5 Compiling Main and Archive Files Now that the object files, funct1.o and funct2.o, are in the archive file functs.a you, can link them with main.o in the following manner. Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $cc -o new_hello main.o functs.a ³ ³ $ls -la new_hello ³ ³ -rwxr-xr-x 1 teacher class 17570 Sep 27 12:58 new_hello ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Workshop 5 This workshop will reinforce your understanding of the ideas presented in this chapter. Login using the username and password given to you by the instructor. Each student is to complete the entire workshop. DESK EXERCISES 1. What does the UNIX utility grep do? 2. What do the following regular expressions represent? ^Ba .* BB* J* [0-9]*$ 3. What does the UNIX utility sed do? Continue on the next page 4. What does the UNIX utility awk do? 5. What does the UNIX utility sort do? 6. What is the main use for the UNIX utility ar? Continue on the next page COMPUTER EXERCISES Use the phone.lis database file to answer the following questions. 7. "I want to find all the phone numbers that begin with a 4 and end with a 2" 8. "I can't remember the name but I believe the last name starts with an S and the first name with an F" 9. Find all the people with 3 character first names. 10. Write a grep command that finds all the phone numbers that don't begin with a 4, 5, or 6. 11. Write a grep command that finds all entries beginning with J-Z and ending with a 2 or 5. 12. Put a 23 in front of every phone number. (Hint: sed) 13. Replace the first name with the person's first initial and a period. Continue on the next page 14. Task: A new phone system has been installed and people with phone extensions beginning with 4 or 5 now have a new prefix: 73. Create a file of only the people with the new phone numbers. 15. Print out the phone list showing last name and first name in the following format and sorted by last name. First name Last name That's enough, don't you think? 6. UNIX UTILITIES PART I - DISPLAY AND MANIPULATE FILES Problem: I want to know what the differences are between two sorted files. Solution: comm command The formal form for the comm command is as follows: ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: comm [ - [ 123 ] ] file1 file2 º º º º Details in on-line man pages º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ This command will display a line-by-line comparison of two sorted files. The output is divided into three columns. The first column shows the lines only found in the first file, the second shows the lines only found in the second file, and the third column shows the lines common to both. Sample session: ................................................................. . $comm comm_file1 comm_file2 . ................................................................. Problem: I want to store and retrieve files in an archive format to create backups, transport files to another compatible system or create archives. Solution: cpio command The formal form for the cpio utility is as follows: ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: cpio -o[options] º º º º cpio -i[options] [patterns] º º º º cpio -p[options] directory º º º º º º See on line man pages for details on options º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ The cpio utility has three functions. It can copy one or more files into a single archive file, retrieve files from a previously created archive file, or it can copy directories. The three major options are: -o (out) This option will cause cpio to read standard input to get pathnames of plain files. It combines these along with header info into a single archive file that it copies to standard output. -i (in) This option will read standard input (which must have been created with the -o option). It extracts files based on patterns you provide as arguments. -p (pass) This option causes cpio to read its standard input to obtain a list of filenames. It copies these files to a directory you specify. Problem: I have two files and I want to know the differences between them. Solution: diff command The formal form for the diff command is as follows: ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: diff [options] file1 file2 º º º º See online man pages for details º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ This command will display the differences between two files on a line-by-line basis. The differences are displayed as commands you can use to make the two files equal. Sample session: ................................................................. . $diff diff_file1 diff_file2 . ................................................................. Problem: I can't remember the name of a file but I know it is in a specific subdirectory and I do know some of its attributes. Solution: find command The formal form for the find command is as follows: ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: find directory_list expression º º º º See online man pages for details º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ The directory_list contains the pathnames of a directory or directories that find will search. The expression contains one or more search criteria. The utility will test each of the files in the directory_list to see if meets the criteria described by the expression. Sample session: ................................................................. . $find . -name 'm* ' -print . ................................................................. Problem: I want a file that exists in another users directory to appear in my directory listing. Solution: Create a link to that file using the ln command The formal form for the ln command is as follows: ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: ln existing_file new_link º º º º ln existing_file_list directory º º º º See the online man pages for details º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ The existing_file is the pathname to the file you want to make a link to. The new_link is the pathname to the new link. The second format allows existing_file_list entries which are pathnames that you want links to, they will appear in directory. Problem: I want to see contents of a file displayed in octal format. Solution: Use the od command to display the file in the selected format. The formal form for the od command is as follows: ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: od [options] filename º º º º See online man pages for details º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ This command is useful for viewing executable (object) files and text files with embedded nonprinting characters. The dump can be shown in octal (default) or hexadecimal or character or decimal. The name od is short for octal dump. Sample session: ................................................................. . $od -c memo . ................................................................. Problem: I want to print and format the contents of a specific file. Solution: pr command. The formal form for the pr command is as follows: ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: pr [options] file_list º º º º See the online man pages for details º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ This command will break up files into pages, usually before printing. Each page will have a header with the name of the file, date, time, and page number. Usually the output if pr is piped to lp so the file can be printed. Sample session: ................................................................. . $pr memo | lp . ................................................................. Problem: I just wrote a memo and I want to check for mis-spelled words. Solution: spell command The formal form for the spell command is as follows: ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: spell [options] file_list º º º º See online man pages for details º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ The spell command will display all words that are not in the dictionary or that can be derived from those words. You can specify more than one file but only one list of misspelled words will be shown. Sample session: ................................................................. . $spell memo . ................................................................. Problem: I want to write a file to tape and later retrieve it back into my directory. Solution: tar command The formal form for the tar command is as follows: ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: tar key[options] [file_list] º º º º See online man pages for details º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ This command can create, add to, list, or retrieve files from an archive file. The archive file is usually stored on tape. The name tar is short for tape archive. Problem: How many lines are in this file? How many words are in this file? How many characters are in this file? Solution: wc command The formal form for the wc utility is as follows: ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: wc [-lwc] filename º º º º See online man pages for details º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ Sample session: ................................................................. . $wc memo . ................................................................. 7. UNIX UTILITIES PART II - DISPLAY AND ALTER STATUS Problem: I want to change the group for a particular file so users outside my group can have access. Solution: chgrp command The formal form for the chgrp command is as follows: ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: chgrp group file_list º º º º See online man pages for details º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ Sample session: ................................................................. . $chgrp class memo . ................................................................. Problem: I want to transfer ownership of a file to another user. Solution: chown command The formal form for the chown command is as follows: ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: chown owner file_list º º º º See the online man pages for details º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ The chown command is short for change owner. Only the owner or Superuser can change the ownership of a file. Example: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $chown rharding /u/do/teacher/memo ³ ³ $ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ The file /u/do/teacher/memo is now owned by the username rharding. Problem: How can I find out how much space I have left on my disk partition? Solution: df command The formal form for the df command is as follows: ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: df [options] [file_system_list] º º º º See the online man pages for details º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ The df (disk free) command will show how much free space is remaining on any mounted device or directory. The amount of space left is usually displayed in blocks. Each block is 1024 bytes in length. Sample session: ................................................................. . $df . ................................................................. Problem: How much space does this file occupy on the disk? Solution: du command The formal form for the du command is as follows: ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: du [options] [file_list] º º º º See the online man pages for details º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ The du (disk usage) command reports how much space a directory and all its subdirectories occupy. It tells the size in blocks, usually 1024 bytes each. Sample session: ................................................................. . $du -s . . 472 . . . $ . ................................................................. Problem: I started a process that I don't need anymore. How can I get rid of it? Solution: kill it with the kill command The formal form for the kill command is as follows: ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: kill [option] PID_list º º º º See the online man pages for details º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ The kill command can stop a process by sending a software termination signal (number 15) to a process. The process being killed must belong to the user of the kill command. The Superuser can, however, kill any process. A message will be displayed indicating that the process was killed. Sample session: ................................................................. . $compute & . . 1742 . . $kill 1742 . ................................................................. Problem: There are some files I need access to but they are in another group. How can I get access to them? Solution: newgrp command The formal form for the newgrp command is as follows: ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: newgrp [group] º º º º See online man pages for details º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ This command will fork a new Shell and while in that Shell you have the privileges of the group you named on the command line. In order for you to use this command you must be listed in the /etc/group file as a member of the group. If you don't specify a group it will change you back to the default as specified in the /etc/passwd file. Sample session: ................................................................. . $newgrp pubs . ................................................................. Problem: This job can be run at a lower priority than default. I want to be a good user and lower the priority so the system can run more efficiently. Can I do that? Solution: nice command The formal form for the nice command is as follows: ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Command Format: nice [option] command_line º º º º See the online man pages for details º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ This command will execute the command line at a lower priority than normal. You can specify a range from 1-19. Sorry, only the Superuser can raise the priority. Sample session: ................................................................. . $nice -19 nroff -m chapter1 > chapter1.out & . ................................................................. Problem: I want the following command to run to completion even after I logout of the system. Is that possible? Solution: noh