home *** CD-ROM | disk | FTP | other *** search
/ Hackers Toolkit v2.0 / Hackers_Toolkit_v2.0.iso / HTML / archive / Texts / interunix.txt < prev    next >
Text File  |  1999-11-04  |  24KB  |  507 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.                                  UNIX for Intermediate Users
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.                             Developed by:
  39.  
  40.                                    User Liaison Section, D-7131
  41.                             [Name and numbers removed at author's request]
  42.  
  43.  
  44.  
  45.                             Revision Date:
  46.  
  47.                                       TABLE OF CONTENTS
  48.  
  49.  
  50. I.  INTRODUCTION........................................................................ ii
  51.        A.  Audience..................................................................... ii
  52.        B.  Course Objectives............................................................ ii
  53.        C.  Course Handout Conventions...................................................iii
  54.  
  55. 1.  THE FILE CALLED .profile AND PROCESSES..............................................  1
  56.        1.1  HOME........................................................................  1
  57.        1.2  PATH........................................................................  2
  58.        1.3  INGRES Environment Variables................................................  2
  59.        1.4  ING_HOME....................................................................  3
  60.        1.5  TERM_INGRES.................................................................  3
  61.        1.6  ING_EDIT....................................................................  3
  62.        1.7  Processes...................................................................  4
  63.        1.8  Executing a Command.........................................................  4
  64.        1.9  Process Identification......................................................  5
  65.        1.10  Interrupt Handling.........................................................  7
  66.  
  67. 2. COMPILING "C" PROGRAMS............................................................... 10
  68.        2.1  "C": Sample Program with a Main and Two Functions
  69.               in One        ............................................................ 10
  70.        2.2  "C": Compiling a Program.................................................... 12
  71.        2.3  "C": Renaming the Executable Module......................................... 13
  72.        2.4  "C": Giving a Name to the Output File....................................... 14
  73.        2.5  "C": Producing an Assembly Listing.......................................... 15
  74.        2.6  "C": Main and Two Functions in Three Separate
  75.               Source Files.............................................................. 16
  76.        2.7  "C": Compiling but Not Producing an Executable
  77.               Module.................................................................... 17
  78.  
  79. 3.  COMPILING FORTRAN  PROGRAMS......................................................... 18
  80.        3.1  FORTRAN: Sample Program a Main and Two
  81.               Subroutines............................................................... 18
  82.        3.2  FORTRAN: Compiling a Program................................................ 19
  83.        3.3  FORTRAN: Renaming the Executable Module..................................... 20
  84.        3.4  FORTRAN: Giving a Name to the Output File................................... 21
  85.        3.5  FORTRAN: Producing an Assembly Listing...................................... 22
  86.        3.6  FORTRAN: Main and Two Subroutines in Three
  87.               Separate Source              Files........................................ 23
  88.        3.7  FORTRAN: Compiling But Not Producing an Executable
  89.               Module.................................................................... 24
  90.        3.8  FORTRAN: Compiling Object Files to Produce an
  91.               Executable                   Module....................................... 25
  92.  
  93. 4.  COMPILING COBOL PROGRAMS............................................................ 26
  94.        4.1  COBOL: Sample Program with a Main and Two
  95.               Subroutines............................................................... 26
  96.        4.2  COBOL: Compiling a Program.................................................. 27
  97.        4.3  COBOL: Running a Program.................................................... 28
  98.        Workshop 2-4..................................................................... 30
  99.  
  100. 5.  UNIX TOOLS.......................................................................... 34
  101.        5.1  The make Utility............................................................ 34
  102. p: A Pattern Matching Filter............................................................ 37
  103.               5.2.1  More on Regular Expressions........................................ 38
  104.               5.2.2  Closure............................................................ 42
  105.               5.2.3  Some Nice grep Options             ................................ 43
  106.               5.2.4  Summary of Regular Expression Characters........................... 44
  107.        5.3  sed: Edit a File to Standard Output......................................... 45
  108.        5.4  awk: A Pattern Matching Programming Language................................ 49
  109.        5.5  sort: Sort a File........................................................... 53
  110.        5.6  Archiver and Library Maintainer............................................. 56
  111.        5.7  Creating an Archive File with Object Modules................................ 57
  112.        5.8  Verifying the Contents of the Archive File.................................. 57
  113.        5.9  Removing Duplicate Object Files............................................. 58
  114.        5.10  Compiling Main and Archive Files........................................... 58
  115.        Workshop 5....................................................................... 59
  116.  
  117. 6.  UNIX UTILITIES PART I - DISPLAY AND MANIPULATE FILES................................ 63
  118.  
  119. 7.  UNIX UTILITIES PART II - DISPLAY AND ALTER STAUTS................................... 73
  120.  
  121. 8.  UNIX UTILITIES PART III - MISCELLANEOUS............................................. 85
  122.  
  123. 9.  ADVANCED FEATURES OF FTP............................................................ 90
  124.        9.1  Initializing FTP on UMAX.................................................... 91
  125.        9.2  Multiple File Transfers..................................................... 92
  126.        9.3  Auto Login Feature.......................................................... 93
  127.        9.4  Macros...................................................................... 95
  128.        9.5  Filename Translation........................................................ 96
  129.        9.6  Aborting Transfers.......................................................... 97
  130.        9.7  More Remote Computer Commands............................................... 98
  131.        Workshop 10...................................................................... 99
  132.  
  133. APPENDIX A - sh.........................................................................101
  134.  
  135. APPENDIX B - ftp........................................................................116
  136.  
  137. APPENDIX C - C Compiler.................................................................128
  138.  
  139. APPENDIX D - FORTRAN Compiler...........................................................137
  140.  
  141. APPENDIX E - lint.......................................................................147
  142.  
  143. APPENDIX F - cb.........................................................................151
  144.  
  145. APPENDIX G - ar.........................................................................152
  146.  
  147. INDEX...................................................................................157
  148.  
  149. I.  INTRODUCTION
  150.  
  151.  
  152. A.  Audience
  153.  
  154.  
  155. This course is for individuals who need to use utilities and
  156. advanced features of the UNIX operating system.
  157.  
  158.  
  159.  
  160. B.  Course Objectives
  161.  
  162.  
  163. Upon successful completion of this course the student will be
  164. able to:
  165.  
  166.        1.     Compile C, FORTRAN, and COBOL programs.
  167.  
  168.        2.     Create processes to run in the background
  169.  
  170.        3.     Use advanced features of FTP such as: multiple file
  171.               transfers, auto logins, macros, globbing, filename
  172.               translation, aborting transfers, and other remote
  173.               computer commands.
  174.  
  175.        4.     Use UNIX utility programs such as grep, sed, awk, sort,
  176.               and others.
  177.  
  178.        5.     Use the make utility.
  179.  
  180.        6.     Understand processes, including structure, executing a
  181.               command, process identification, exit status, plus .
  182.               (dot) and exec processing.
  183. C.  Course Handout Conventions
  184.  
  185.  
  186. There are several conventions used in this handout for
  187. consistency and easier interpretation:
  188.  
  189.  
  190.        1.     Samples of actual terminal sessions are single-lined
  191.               boxed.
  192.  
  193.  
  194.        2.     User entries are shown in bold print and are
  195.               underlined.
  196.  
  197.               exit
  198.  
  199.  
  200.        3.     All keyboard functions in the text will be bold.
  201.  
  202.               (Ret)                       Backspace
  203.               Tab                         Ctrl-F6
  204.               Print (Shift-F7)            Go to DOS (1)
  205.  
  206.               NOTE:         (Ret) indicates the Return or Enter key
  207.                             located above the right Shift key.
  208.  
  209.  
  210.        4.     Examples of user entries not showing the computer's
  211.               response are in dotted-lined boxes.
  212.  
  213.  
  214.  
  215.        5.     Command formats are double-lined boxed.
  216.  
  217.  
  218.  
  219.        6.     Three dots either in vertical or horizontal alignment
  220.               mean continuation or that data is missing from diagram.
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227. ┌───────────────────────────────────────────────────────────────┐
  228. │                                                               │
  229. │        Multimax, Nanobus, and UMAX are trademarks of          │
  230. │        Encore Computer Corporation.                           │
  231. │                                                               │
  232. │                                                               │
  233. │        Annex is a trademark of XYLOGICS, Inc.                 │
  234. │                                                               │
  235. │                                                               │
  236. │        UNIX and Teletype are registered trademarks of         │
  237. │        AT&T Bell Laboratories                                 │
  238. │                                                               │
  239. │                                                               │
  240. │        Ethernet is a trademark of Xerox Corporation           │
  241. │                                                               │
  242. └───────────────────────────────────────────────────────────────┘
  243. 1.  UNIX PROCESSES AND A FILE CALLED .profile
  244.  
  245.  
  246. 1.1  Processes
  247.  
  248.  
  249. A process is the execution of a command by UNIX.  Processes can
  250. also be executed by the operating system itself.  Like the file
  251. structure, the process structure is hierarchical.  It contains
  252. parents, children, and even a root.  A parent can fork (or spawn)
  253. a child process.  That child can, in turn, fork other processes.
  254. The first thing the operating system does to begin execution is
  255. to create a single process, PID number 1.  PID stands for Process
  256. Identification.  This process will hold the same position as the
  257. root directory in the file structure.  This process is the
  258. ancestor to all processes that each user works with.  It forks a
  259. process for each terminal.  Each one of these processes becomes a
  260. Shell process when the user logs in.
  261.  
  262. 1.2  Process Identification
  263.  
  264.  
  265. The UNIX operating system assigns a unique process identification
  266. number (PID) to each process.  It will keep the same PID as long
  267. as the process is in existence.  During one session, the same
  268. process is always executing the login Shell.  When you execute
  269. another command, a new process is forked and a new PID is
  270. assigned to that process.  When that child process is finished,
  271. you are returned to the login process, which is running the
  272. Shell, and that parent process has the same PID as when you
  273. logged in.
  274.  
  275.  
  276. The Shell stores the PID in Shell variable called $$.  The PID
  277. can also be shown with the process status (ps) command.  The
  278. format for ps is as follows:
  279.  
  280.  
  281. ╔═══════════════════════════════════════════════════════════════╗
  282. ║    Command Format:  ps [options]                              ║
  283. ║                                                               ║
  284. ║    See on-line manual for options                             ║
  285. ╚═══════════════════════════════════════════════════════════════╝
  286.  
  287. With no options given the ps command will give you certain
  288. information about processes associated with the controlling
  289. terminal.  The output consists of a short listing containing the
  290. process id, terminal id, cumulative execution time, and the
  291. command name.  Otherwise, options will control the display.
  292.  
  293. Sample session:
  294.  
  295. ┌────────────────────────────────────────────────────────────────────────────┐
  296. │ $echo $$                                                                   │
  297. │ 8347                                                                       │
  298. │ $ps                                                                        │
  299. │    PID TTY      TIME COMMAND                                               │
  300. │   8347 rt021a0  0:03 ksh                                                   │
  301. │   8376 rt021a0  0:06 ps                                                    │
  302. │ $                                                                          │
  303. └────────────────────────────────────────────────────────────────────────────┘
  304.  
  305.  
  306. The PID numbers of the Shell are the same in the sample session
  307. because the Shell will substitute its own PID number for $$. 
  308. The Shell makes the substitution before it forks a new process to
  309. execute the echo command.  Therefore, echo will display the PID
  310. number of the process that called it, not the PID of the process
  311. that is executing it.
  312. The -l option will display more information about the processes.
  313.  
  314.  
  315. Sample Session:
  316.  
  317. ┌──────────────────────────────────────────────────────────────────────────────────────────┐
  318. │ $ps -l                                                                                   │
  319. │     F S   UID   PID  PPID  C PRI NI     ADDR     SZ    WCHAN TTY      TIME COMD          │
  320. │ f0000 S   115  8347   309  2  30 20  1009000    140    94014 rt021a0  0:03 ksh           │
  321. │ f0000 O   115  8386  8347 16  68 20  1308000     72          rt021a0  0:01 ps            │
  322. │ $ps -l                                                                                   │
  323. │     F S   UID   PID  PPID  C PRI NI     ADDR     SZ    WCHAN TTY      TIME COMD          │
  324. │ f0000 S   115  8347   309  1  30 20  1009000    140    94014 rt021a0  0:03 ksh           │
  325. │ f0000 O   115  8387  8347 26  73 20  1146000     72          rt021a0  0:01 ps            │
  326. │ $                                                                                        │
  327. └──────────────────────────────────────────────────────────────────────────────────────────┘
  328.  
  329.  
  330. 1.3  Executing a Command
  331.  
  332.  
  333. When you give a command to the Shell, it will fork a process to
  334. execute the command.  While the child process is executing the
  335. command, the parent will go to sleep.  Sleeping means that the
  336. process will not use any CPU time.  It remains inactive until it
  337. is awakened.  When the child process has finished executing the
  338. command, it dies.  The parent process, which is running the
  339. Shell, wakes up and prompts you for another command.
  340.  
  341. When you request a process to run in the background (by ending
  342. the command line with an ampersand character (&), the Shell forks
  343. a child process that is allowed to run to completion.  The parent
  344. process will report the PID of the child process and then prompt
  345. you for another command.  The child and parent are now
  346. independent processes.
  347.  
  348.  
  349. 1.4  The . (dot) and exec Commands
  350.  
  351. There are two ways to execute a program without forking a new
  352. process. The . (dot) command will execute the script as part of
  353. the current process. When the new script has finished executing,
  354. the current process will continue to execute the original script. 
  355. The exec command will execute the new script in place of
  356. (overlays) the original script and never returns to the original
  357. script.
  358.  
  359. The . (dot) command will not execute compiled files (binary) and it does not require execute
  360. permission on the script file that is being executed. The exec command does require access
  361. permission to either a binary program or a shell script.
  362.  
  363.  
  364. Sample session:
  365.  
  366. ┌───────────────────────────────────────────────────────────────┐
  367. │    $ls -l prog2                                               │
  368. │    -rw-r--r--  1 teacher  class   22 Jan 18 10:30 prog2       │
  369. │    $cat prog2                                                 │
  370. │    echo 'prog2 PID =' $$                                      │
  371. │    $cat dot_example                                           │
  372. │    echo $0 'PID=' $$                                          │
  373. │    . prog2                                                    │
  374. │    echo 'This line is executed'                               │
  375. │    $dot_example                                               │
  376. │    dot_example PID= 6942                                      │
  377. │    prog2 PID = 6942                                           │
  378. │    This line is executed                                      │
  379. │    $                                                          │
  380. └───────────────────────────────────────────────────────────────┘
  381.  
  382. The exec command will overlay the sh and control will never return to the calling script.
  383. Let's look at another example with a call to prog2 using exec instead of . (dot):
  384.  
  385. Sample session:
  386.  
  387. ┌───────────────────────────────────────────────────────────────┐
  388. │    $ls -l prog2                                               │
  389. │    -rwxr-xr-x  1 teacher  class   22 Jan 18 10:30 prog2       │
  390. │    $cat prog2                                                 │
  391. │    echo 'prog2 PID =' $$                                      │
  392. │    $cat exec_example                                          │
  393. │    echo $0 'PID=' $$                                          │
  394. │    exec prog2                                                 │
  395. │    echo 'This line is never executed'                         │
  396. │    $exec_example                                              │
  397. │    exec_example PID= 6950                                     │
  398. │    prog2 PID = 6950                                           │
  399. │    $                                                          │
  400. │                                                               │
  401. └───────────────────────────────────────────────────────────────┘
  402.  
  403. Background Processing
  404.  
  405. When a program is running in background you do not have to wait for it to finish before
  406. starting another program. This is useful because you can start long/large jobs and then
  407. continue to do another task on your terminal.
  408.  
  409. To run a program in background simply type an ampersand character (&) at the end of the
  410. command line before the (Ret) key. The Shell will return the PID of the background process
  411. and then give you another system prompt.
  412.  
  413. Sample session:
  414.  
  415. ┌───────────────────────────────────────────────────────────────┐
  416. │    $ls -l | lp &                                              │
  417. │    [1]     21334                                              │
  418. │    $request id is mt_600-2736 (standard input)                │
  419. │                                                               │
  420. │    $                                                          │
  421. └───────────────────────────────────────────────────────────────┘
  422.  
  423. If the background task sends output to standard output and you fail to redirect it, the
  424. output will appear on your terminal even if you are running another program at the time. 
  425.  
  426. It is necessary to use the kill command to stop a process that is running in background the
  427. (DEL) key or its equivalent will not work.
  428.  
  429. Exit Status
  430.  
  431. When a process stops executing for any reason, it will return an exit status to the parent
  432. process. This exit status is also referred to as a condition code or return code.The Shell
  433. stores the exit status in a Shell variable called $?. By convention, a non-zero exit status
  434. means that it has a false value and the command failed. On the other hand, a zero status
  435. indicates true and the command was successful.
  436.  
  437. It is possible for you to specify the exit status when you exit a script. This is done by
  438. specifying the number to be used as the exit status using the exit command. The following
  439. script is an example:
  440.  
  441. Sample Session:
  442.  
  443. ┌───────────────────────────────────────────────────────────────┐
  444. │    $cat exit_example                                          │
  445. │    echo 'This program returns an exit status'                 │
  446. │    echo 'of 7.'                                               │
  447. │    exit 7                                                     │
  448. │    $exit_example                                              │
  449. │    This program returns an exit status                        │
  450. │    of 7.                                                      │
  451. │    $echo $?                                                   │
  452. │    7                                                          │
  453. │    $echo $?                                                   │
  454. │    0                                                          │
  455. │    $                                                          │
  456. │                                                               │
  457. └───────────────────────────────────────────────────────────────┘
  458.  
  459. This script will display the message and then exit with an exit code of 7. The exit status
  460. is stored in the Shell variable called $?. The second echo command above displays the exit
  461. status of the first echo command. Since it completed successfully it has a value of zero.
  462.  
  463. 1.4  Interrupt Handling
  464.  
  465.  
  466. A signal is a report to a process about a condition.  UNIX uses
  467. these signals to report bad system calls, broken pipes, illegal
  468. instructions, and other conditions.  There are three signals that
  469. are useful when programming in the Shell.  They are the terminal
  470. interrupt signal (number 2), the kill signal (number 9) and the
  471. software termination signal (number 15).
  472.  
  473. You can use the trap command to capture a signal and then take
  474. whatever action you specify.  It can close files or finish other
  475. processing that needs to be done, display a message, terminate
  476. execution immediately, or ignore the signal.
  477.  
  478. ╔═══════════════════════════════════════════════════════════════╗
  479. ║   Command Format: trap ['commands'] signal_numbers            ║
  480. ║                                                               ║
  481. ║   See online man pages for details                            ║
  482. ║                                                               ║
  483. ╚═══════════════════════════════════════════════════════════════╝
  484.  
  485. The signal_numbers are the numbers corresponding to the signals
  486. that will be trapped by the trap command.  There must be at least
  487. one number present.  The 'commands' portion of the command is
  488. optional.  If it is not present, the command resets the trap to
  489. its initial condition, which is to exit the program.  When the
  490. commands is present the Shell executes the commands when it
  491. catches one of the signals.  After executing the commands, the
  492. Shell continues executing the script where it left off.
  493.  
  494. You can interrupt a program you are running in the foreground by 
  495. pressing the Delete key.  When you press this key a signal
  496. (number 2), a terminal interrupt, to the program.  The Shell will
  497. terminate the execution of the program if the program does not
  498. trap the signal.  The following example demonstrates the trap
  499. command that will trap the signal and return an exit status of 1.
  500.  
  501. Sample session:
  502.  
  503. ┌───────────────────────────────────────────────────────────────┐
  504. │    $cat inter                                                 │
  505. │    trap 'echo PROGRAM INTERRUPTED; exit 1' 2                  │
  506. │    while (true)                                               │
  507. │          do