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

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

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

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

June 1, 1999 Using The C-Shell 1

Introduction to UNIX

F. Using C Shell Features

Page 2: June 1, 1999Using The C-Shell1 Introduction to UNIX F. Using C Shell Features.

June 1, 1999 Using The C-Shell 2

Using C Shell FeaturesPerformance Objectives 1. Use History to recall/repeat commands (history, !, !!)

2. Edit a command line using the replace function(^)

3. Edit a command line using the substitution method (:s)

4. Identify the concept of STDIN/STDOUT/STDERR

5. Redirect data Input and Output (>, <)

6. Append results of a command to existing files (>>, >>&)

7. Use Pipes to link command data (|)

8. Construct simple aliases (alias)

9. Recognize three basic error messages

Page 3: June 1, 1999Using The C-Shell1 Introduction to UNIX F. Using C Shell Features.

June 1, 1999 Using The C-Shell 3

Shell Initialization and Termination

• Activities: 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.

Page 4: June 1, 1999Using The C-Shell1 Introduction to UNIX F. Using C Shell Features.

June 1, 1999 Using The C-Shell 4

Built-in Commands

• C Shell contains built-in commands: alias fg/bg echo history ignoreeof

jobs kill noclobber noglob set

setenv source stop suspend umask

unalias

• The man page for csh is very long (about 100 screens).

• Use the man search feature (/keyword).

Page 5: June 1, 1999Using The C-Shell1 Introduction to UNIX F. Using C Shell Features.

June 1, 1999 Using The C-Shell 5

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% history1 ls -l2 cd ~ths3 history

Page 6: June 1, 1999Using The C-Shell1 Introduction to UNIX F. Using C Shell Features.

June 1, 1999 Using The C-Shell 6

Command Line Editing

• To make a new simpler command: host% alias hb history

• Note the following:host% cd unix/class/handouts/larn

unix/class/handouts/larn: No such file or directory

host% ^lar^lear^

cd unix/class/handouts/learn

Page 7: June 1, 1999Using The C-Shell1 Introduction to UNIX F. Using C Shell Features.

June 1, 1999 Using The C-Shell 7

Command Line Editing

• A display of the history buffer reveals:host% history2 cd ~ths3 history4 cd unix/class/handouts/larn5 cd unix/class/handouts/learn6 history

• To recall a previous command:host% !cdhost% !5

Page 8: June 1, 1999Using The C-Shell1 Introduction to UNIX F. Using C Shell Features.

June 1, 1999 Using The C-Shell 8

Command Line Editing

• You can list the last n commands from the history buffer:host% history 6 4 cd unix/class/handouts/larn 5 cd unix/class/handouts/learn 6 history 7 cat /home/ths/class0/basics/history/congrats 8 cat /etc/motd 9 history

• To rerun the correct cd command - !5 or !cd

Page 9: June 1, 1999Using The C-Shell1 Introduction to UNIX F. Using C Shell Features.

June 1, 1999 Using The C-Shell 9

Command Line Editing

• Substitution changes any previous command: host% !7:s/0/1 cat /home/ths/sunclass1/basics/history/congrats

• History list should be similar to the following:7 cat /home/ths/sunclass0/basics/history/congrats8 cat /home/ths/sunclass1/basics/history/congrats9 history10 cat /home/ths/sunclass1/basics/history/congrats11 history

Page 10: June 1, 1999Using The C-Shell1 Introduction to UNIX F. Using C Shell Features.

June 1, 1999 Using The C-Shell 10

Redirecting Input/Output

• The 3 UNIX standard files are, the terminal. STDIN (keyboard) STDOUT (display) STDERR (display)

• To redirect output to another file, use: Greater-than > symbol to redirect STDOUT Less-than < symbol to redirect STDIN Ampersand & symbol to redirect STDERR

Page 11: June 1, 1999Using The C-Shell1 Introduction to UNIX F. Using C Shell Features.

June 1, 1999 Using The C-Shell 11

Redirecting Input/Output

• Examples:

host% cat filea > fileb (redirect creates fileb) host% cat filec >> fileb (appends to fileb) host% cat filed >& e.file (error msgs to e.file) host% cat filex >>& e.file (append error to e.file) host% mail -s “sub” address < file (get input from

file)

Page 12: June 1, 1999Using The C-Shell1 Introduction to UNIX F. Using C Shell Features.

June 1, 1999 Using The C-Shell 12

Use of Pipes - |

• Special symbol “|”command [options] | command [options] | ...

• Problem: How many processes are running on your

machine? The command ps -ax will show all processes

on a machine.

Page 13: June 1, 1999Using The C-Shell1 Introduction to UNIX F. Using C Shell Features.

June 1, 1999 Using The C-Shell 13

Using Pipes (Con’t)

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

112 ? IW 0:03 cron

Page 14: June 1, 1999Using The C-Shell1 Introduction to UNIX F. Using C Shell Features.

June 1, 1999 Using The C-Shell 14

Use of Pipes - | (Con’t)

• There are several solutions to this problem: Solution 1

host% ps -ax > psfile

host% wc -l psfile Note the number of processes

Solution 2host% ps -ax | wc -l Note the number of processes

Why is there a difference of one process?

Page 15: June 1, 1999Using The C-Shell1 Introduction to UNIX F. Using C Shell Features.

June 1, 1999 Using The C-Shell 15

Using Filters with Pipes

• Lists are often difficult to preview visually:host% ls -l produces a long list of all files.

-rw-r--r-- 1 pam 880 Sep 28 1988 Aug.notes

drwxr-xr-x 2 pam 512 Oct 23 1985 bin

-rw-r--r-- 1 pam 129 Aug 20 1986 complex.f

-rw------- 1 pam 129 Jul 2 1987 mbox

-rw-r--r-- 1 pam 429 Aug 16 14:18 outdis

drwxr-xr-x 2 pam 512 Jan 22 11:07 remodel

-rwxr-xr-x 1 pam 102 Jun 19 12:55 test.out

Page 16: June 1, 1999Using The C-Shell1 Introduction to UNIX F. Using C Shell Features.

June 1, 1999 Using The C-Shell 16

Using Filters with Pipes - grep

• To search a file for a specified string:host% ls -l | grep Aug-rw-r--r-- 1 pam 129 Aug 20 1986 complex.f-rw-r--r-- 1 pam 4291 Aug 16 14:18 outdis-rw-r--r-- 1 pam 880 Sep 28 1988 Aug.notes

• To search a file for all but specified string: host% ls -l | grep -v Augdrwxr-xr-x 2 pam 512 Oct 23 1985 bin-rw------- 1 pam 129 Jul 21 1987 mboxdrwxr-xr-x 2 pam 512 Jan 22 11:07 remodel-rwxr-xr-x 1 pam 102 Jun 19 2:55 test.out

Page 17: June 1, 1999Using The C-Shell1 Introduction to UNIX F. Using C Shell Features.

June 1, 1999 Using The C-Shell 17

Using Filters - pipes, grep & sort

• Consider the next example:

host% ls -l | grep Aug | sort +4-rw-r--r-- 1 pam 129 Aug 20 1986 complex.f

drwxr-xr-x 2 pam 512 Aug 23 1985 bin

-rw-r--r-- 1 pam 880 Sep 28 1988 Aug.notes

-rw-r--r-- 1 pam 929 Aug 16 14:18 outdis

-rwxr-xr-x 1 pam 972 Aug 19 12:55 test.out

Page 18: June 1, 1999Using The C-Shell1 Introduction to UNIX F. Using C Shell Features.

June 1, 1999 Using The C-Shell 18

Constructing Simple Aliases

• Examine the following aliases: alias rm rm -i

• Full pathnames are preferred: alias cp /bin/cp -I

• Enclose complex commands in “quotes” alias ls "pwd; /bin/ls -sFC"

Command Alias name Command line

Page 19: June 1, 1999Using The C-Shell1 Introduction to UNIX F. Using C Shell Features.

June 1, 1999 Using The C-Shell 19

More Complex Aliases

• Importing an argument: alias chklog 'grep \!* log.* | more'

host% chklog reg1

• alias cnt 'grep -c \!* log.*'

• alias f 'finger “\!*”@lanl.gov'

Alias name Argument List of files

Page 20: June 1, 1999Using The C-Shell1 Introduction to UNIX F. Using C Shell Features.

June 1, 1999 Using The C-Shell 20

End of Module

Complete Using C Shell Function Exercises