💾 Archived View for gemini.spam.works › mirrors › textfiles › hacking › UNIX › interunx.txt captured on 2022-07-17 at 01:58:45.
⬅️ Previous capture (2022-06-12)
-=-=-=-=-=-=-
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 <filename>.o and <filename>.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 <filename>.c C source code <filename>.f FORTRAN source code <filename>.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 <tab> #-#### 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 <tab>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 \<tab>4 phone.lis ? ? StClair, Fred 4-6122 ? ? Benson, Sam 4-5587 ? ? $ ? ????????????????????????????????????????????????????????????????? Now it worked properly. It searched for a <tab> 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]