Introduction to UNIX F Using C Shell Features











![Use of Pipes - | • Special symbol “|” command [options] |. . . Use of Pipes - | • Special symbol “|” command [options] |. . .](https://slidetodoc.com/presentation_image_h2/be02dff54e0c724f85d99ee4fc713652/image-12.jpg)








- Slides: 20

Introduction to UNIX F. Using C Shell Features June 1, 1999 Using The C-Shell 1

Using C Shell Features Performance Objectives 1. 2. 3. 4. 5. 6. 7. 8. 9. Use History to recall/repeat commands (history, !, !!) Edit a command line using the replace function(^) Edit a command line using the substitution method (: s) Identify the concept of STDIN/STDOUT/STDERR Redirect data Input and Output (>, <) Append results of a command to existing files (>>, >>&) Use Pipes to link command data (|) Construct simple aliases (alias) Recognize three basic error messages June 1, 1999 Using The C-Shell 2

Shell Initialization and Termination • Activities: n n Reads. cshrc from home directory when new C shell started. Reads. login when initiated as a login shell. Reads. logout when shell terminated (if the login shell) Operates in either interactive or non-interactive (batch) mode. June 1, 1999 Using The C-Shell 3

Built-in Commands • C Shell contains built-in commands: alias fg/bg echo history ignoreeof jobs kill noclobber noglob setenv source stop suspend umask unalias • The man page for csh is very long (about • 100 screens). Use the man search feature (/keyword). June 1, 1999 Using The C-Shell 4

The History Mechanism • Command Line Editing Allows you to: rerun any command in the history buffer, and è change/correct commands in the history buffer. è • To set up a history list: set history=15 (. cshrc) • To display the last n commands. host% history 1 ls -l 2 cd ~ths 3 history June 1, 1999 Using The C-Shell 5

Command Line Editing • To make a new simpler command: host% alias hb history • Note the following: host% cd unix/class/handouts/larn: No such file or directory host% ^lar^lear^ cd unix/class/handouts/learn June 1, 1999 Using The C-Shell 6

Command Line Editing • A display of the history buffer reveals: host% history 2 cd ~ths 3 history 4 cd unix/class/handouts/larn 5 cd unix/class/handouts/learn 6 history • To recall a previous command: host% !cd host% !5 June 1, 1999 Using The C-Shell 7

Command Line Editing • You can list the last n commands from the history buffer: host% history 6 4 5 6 7 8 9 cd unix/class/handouts/larn cd unix/class/handouts/learn history cat /home/ths/class 0/basics/history/congrats cat /etc/motd history • To rerun the correct cd command - !5 or !cd June 1, 1999 Using The C-Shell 8

Command Line Editing • Substitution changes any previous command: host% !7: s/0/1 cat /home/ths/sunclass 1/basics/history/congrats • History list should be similar to the following: 7 cat /home/ths/sunclass 0/basics/history/congrats 8 cat /home/ths/sunclass 1/basics/history/congrats 9 history 10 cat /home/ths/sunclass 1/basics/history/congrats 11 history June 1, 1999 Using The C-Shell 9

Redirecting Input/Output • The 3 UNIX standard files are, the terminal. n n n STDIN (keyboard) STDOUT (display) STDERR (display) • To redirect output to another file, use: n n n Greater-than > symbol to redirect STDOUT Less-than < symbol to redirect STDIN Ampersand & symbol to redirect STDERR June 1, 1999 Using The C-Shell 10

Redirecting Input/Output • Examples: host% è host% è June 1, 1999 cat filea > fileb (redirect creates fileb) cat filec >> fileb (appends to fileb) cat filed >& e. file (error msgs to e. file) cat filex >>& e. file (append error to e. file) mail -s “sub” address < file (get input from file) Using The C-Shell 11
![Use of Pipes Special symbol command options Use of Pipes - | • Special symbol “|” command [options] |. . .](https://slidetodoc.com/presentation_image_h2/be02dff54e0c724f85d99ee4fc713652/image-12.jpg)
Use of Pipes - | • Special symbol “|” command [options] |. . . • Problem: n n How many processes are running on your machine? The command ps -ax will show all processes on a machine. June 1, 1999 Using The C-Shell 12

Using Pipes (Con’t) • But you must count each line on the screen: ted% ps -ax PID 0 1 2 55 60 62 73 74 75 76 87 95 97 101 104 109 TT ? ? ? ? 112 ? June 1, 1999 STAT TIME COMMAND D 2: 16 swapper IW 0: 00 /sbin/init D 0: 12 pagedaemon IW 2: 59 portmap IW 0: 00 ypbind IW 0: 00 keyserv S 1: 57 (biod) S 1: 56 (biod) IW 0: 03 syslogd IW 0: 00 rpc. statd IW 0: 00 rpc. lockd IW 11: 01 automount S 0: 41 screenblank S 20: 21 update IW 0: 03 cron Using The C-Shell 13

Use of Pipes - | (Con’t) • There are several solutions to this problem: n Solution 1 host% ps -ax > psfile host% wc -l psfile è Note the number of processes n Solution 2 host% ps -ax | wc -l è Note the number of processes n Why is there a difference of one process? June 1, 1999 Using The C-Shell 14

Using Filters with Pipes • Lists are often difficult to preview visually: host% ls -l -rw-r--r-- 1 drwxr-xr-x 2 -rw-r--r-- 1 -rw------- 1 -rw-r--r-- 1 drwxr-xr-x 2 -rwxr-xr-x 1 June 1, 1999 produces a long list pam 880 Sep 28 1988 pam 512 Oct 23 1985 pam 129 Aug 20 1986 pam 129 Jul 2 1987 pam 429 Aug 16 14: 18 pam 512 Jan 22 11: 07 pam 102 Jun 19 12: 55 Using The C-Shell of all files. Aug. notes bin complex. f mbox outdis remodel test. out 15

Using Filters with Pipes - grep • To search a file for a specified string: host% ls -l -rw-r--r-- 1 | grep Aug pam 129 Aug 20 1986 complex. f pam 4291 Aug 16 14: 18 outdis pam 880 Sep 28 1988 Aug. notes • To search a file for all but specified string: host% ls -l drwxr-xr-x 2 -rw------- 1 drwxr-xr-x 2 -rwxr-xr-x 1 June 1, 1999 | grep -v Aug pam 512 Oct 23 pam 129 Jul 21 pam 512 Jan 22 pam 102 Jun 19 Using The C-Shell 1985 1987 11: 07 2: 55 bin mbox remodel test. out 16

Using Filters - pipes, grep & sort • Consider the next example: host% ls -l | grep Aug | sort +4 -rw-r--r-drwxr-xr-x -rw-r--r--rwxr-xr-x June 1, 1999 1 2 1 1 1 pam pam pam 129 512 880 929 972 Aug Sep Aug Using The C-Shell 20 23 28 16 19 1986 1985 1988 14: 18 12: 55 complex. f bin Aug. notes outdis test. out 17

Constructing Simple Aliases • Examine the following aliases: alias rm rm -i Command Alias name Command line • Full pathnames are preferred: n alias cp /bin/cp -I • Enclose complex commands in “quotes” n alias ls "pwd; /bin/ls -s. FC" June 1, 1999 Using The C-Shell 18

More Complex Aliases • Importing an argument: alias chklog 'grep !* log. * | more' Argument Alias name è List of files host% chklog reg 1 • alias cnt 'grep -c !* log. *' • alias f 'finger “!*”@lanl. gov' June 1, 1999 Using The C-Shell 19

End of Module Complete Using C Shell Function Exercises June 1, 1999 Using The C-Shell 20