84
Advanced UNIX • Objectives of these slides: • Introduce the C Shell • Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

Embed Size (px)

Citation preview

Page 1: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

1

Advanced UNIX

• Objectives of these slides:• Introduce the C Shell• Concentrate on features not in the Bourne

Shell (but many are in Bash)

The C Shell

Page 2: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

2

Background

• Originally part of Berkeley UNIX

• More tolerant of mistakes than Bourne• e.g. can avoid accidental logouts, overwrites

• Easier to configure• e.g. alias, history, job control

• Some scripting improvements over Bourne:• better variables, arrays, expressions

Page 3: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

3

• Almost all the good ideas from the C Shell have been added to Bash:• e.g. history, aliases, job control, directory

stacks

Page 4: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

4

Entering & leaving the C Shell

• Check your default shell with ps

• Change shell using chsh or type csh

• Exit with: exit, logout(control-D maybe)

csh is actually tcsh on most Linux versions

Page 5: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

5

History

• A list of recently used command lines (events) that can be reused.

• $ set history = 25 (record 25 events)$ set savehist = 20 (remember 20 events

after logout)$ history (see history list)

Page 6: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

6

Using the history list

• $ !! (execute last event)

• $ !<number> (execute event no. <number>)$ !3

• $ !<initial-text> (execute most recent command that starts with initial-text)

$ !gcc

$ !loc

Page 7: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

7

Bash History• history [ number ]

• lists the last number commands!

• Bash supports the same history list features:• !!, !<number>, !<text>

• Relevant environment variables:• HISTFILE=/home/ad/.bash_history• HISTFILESIZE=500• HISTSIZE=500

Page 8: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

8

• In Bash, you can modify the prompt to include the history number.

• Inside .bashrc:PS1="\!.\u@\h$ "

\! includes thehistory number

Page 9: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

9

Alias

• Define new commands by using string substitution.

• Format:$ alias new-command executed-command(s)

• e.g.$ alias ll ls -lg$ ll

Page 10: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

10

Examples

• $ alias ls ls -lg

• $ alias mi mv -i

• $ alias who ’who ; date’

redefining commandsis okay

Page 11: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

11

Argument Substitution

• \!^ the first argument of the new command• \!* all the arguments of the new command

• $ alias wid ’who | fgrep \!^’

• $ wid igor

is the same as:$ who | fgrep igor

Page 12: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

12

Check your aliases

• $ alias (list the current aliases)

• $ alias hd (check hd alias)

• $ unalias hd (cancel hd alias)

• $ \ls (use original meaning of ls)

Page 13: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

13

Bash Alias

• Bash alias works the same way at the command line as in csh:

alias, unalias, \command

• Defining an alias in .bashrc is different:alias name = value

•alias ls='/bin/ls -F'•alias ll='ls -l'•alias h="history 30"

Page 14: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

14

• Aliases that require multiple commands or arguments are defined as functions:

sgrep(){ ps aux | grep $1 | grep -v grep}

cs(){ cd $1 ls}

Page 15: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

15

Job Control

• Move commands between foreground and background; suspend commands.

• Each background job has a PID and a job number.

Page 16: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

16

Example

• $ spell glossary > glossary.out &[1] 26025

$ date &[2] 26028Fri Jun 6 16:56:11 GMT+7 2000[2] Done date

$ gcc big.c &[2] 26041

Page 17: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

17

• $ jobs[1] - Running spell glossary.out > glo[2] + Running gcc big.c

• Other status messages:• Stopped• Stopped (tty input)• Done

Page 18: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

18

Background to Foreground

• $ fg %job-number

• Example:$ fg %2

Page 19: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

19

Foreground to Background

• $ control-Z (suspends job and puts it into the background)

• $ bg (resumes the job)

• $ bg %job-number (resumes job-number)

Page 20: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

20

Stopping

• $ stop %job-number

• $ kill %job-number (kills job-number)

• A job stopped for tty input must be brought to the foreground (with fg).

Page 21: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

21

State Change

• The C Shell prints a message at the next prompt when the state of a job changes.

• Use notify to make the shell report a change immediately:

$ notify %job-number

Page 22: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

22

Home Directory Short Forms

• ~ (your home directory)

• ~name (home directory of name)

e.g. ~ad

• Use:$ cp idea.txt ~

$ ls ~ad/teach

Page 23: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

23

Filename Completion

• $ set filec (‘switch on’ file completion)

• $ cat trig1A <press esc>$ cat trig1A.txt

Page 24: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

24

Control-D for help

• $ ls h*help.hist help.text help.trig.01

• $ cat h <press esc>$cat help. <beep> <press control-D>help.hist help.text help.trig.01$ cat help.

sometimes <tab>

Page 25: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

25

~

Directory Stacks

• Store a list of directories you are using on a stack.

• Switch between directories by referring to the stack.

adv-unixcops_104

Page 26: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

26

• $ pwd/home/ad/

$ pushd ~/teach/adv-unix~/teach/adv-unix ~

$ pwd/home/ad/teach/adv-unix

$ pushd ~/cops_104~/cops_104 ~/teach/adv-unix ~

$ pwd/home/ad/cops_104

adv-unixcops_104

push the directoryonto the stackand do a cd

~ ~

Page 27: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

27

~

Change directories quickly

• $ pushd~/teach/adv-unix ~/cops_104 ~

$ pwd/home/ad/teach/adv-unix/

cops_104adv-unix

push the top directorydown and do a cd

Page 28: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

28

• $ pushd~/cops_104 ~/teach/adv-unix ~

$ pwd/home/ad/cops_104

~adv-unixcops_104

push the top directorydown and do a cd

Page 29: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

29

~

Popd

• $ popd~/teach/adv-unix ~

$ pwd

/home/ad/teach/adv-unix/

adv-unix

pop the top directoryand cd to the new top

Page 30: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

30

Other Stack Commands

• $ dirs (lists the directories on the stack)

• $ pushd +number

• move the directory in position number to the top (0), and cd to that directory

• the stack is numbered 0, 1, 2, ...

Page 31: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

31

• $ popd +number

• pop off the directory at position number

• if number is not 0 then there is no change to the present directory

Page 32: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

32

Variables ( 4 types)

String Variables

Numerical Variables

Special Forms of User Variables

Shell Variables

Page 33: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

33

String Variables

• $ set name = fred (include spaces in older csh's)

$ echo $namefred

$ set (list set variables)

$ unset name (delete name var)

Page 34: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

34

setenv (environment vars)

• $ setenv name fred$ echo $namefred$ setenv$ unsetenv name

• setenv makes the variable visible to any scripts called from the shell (or from the script containing the setenv).

no =

Page 35: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

35

Arrays of String Vars

• $ set colours = (red green blue orange)

$ echo $coloursred green blue orange

$ echo $colours[3]blue

$ echo $colours[2-4]green blue orange

Page 36: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

36

• $ set shapes = (’’ ’’ ’’ ’’ ’’)

$ echo $shapes

$ set shapes[4] = square

$ echo $shapes[4]square

Page 37: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

37

Braces

• Use {...} to distinguish a variable from surrounding text.

• $ set prefix = Alex

$ echo $prefix is short for {$prefix}ander.Alex is short for Alexander.

• Can also write $prefix{ander}

Page 38: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

38

Numeric Variables• Use the @ command for variables holding numbers.

• $ @ count = 0 (or set count = 0)$ echo $count0$ @ count = ( 5 + 2 )$ echo $count7$ @ result = ( $count < 5 )$ echo $result0

Rememberthe space after the @

Page 39: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

39

• $ @ count = $count + 5$ echo $count12

• Could write:$ @ count += 5

• $ @ count++$ echo $count13

Page 40: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

40

Operators

• Most of the C numerical operators:

• Arithmetic: + - * / %• Relational: > < >= <= != ==• Logical: && || !• etc.

Page 41: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

41

Arrays of Numeric Vars

• $ set ages = (0 0 0 0 0)

$ @ ages[2] = 15

$ @ ages[3] = ( $ages[2] + 4 )

$ echo $ages[3]19

$ echo $ages0 15 19 0 0

Page 42: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

42

Bourne Shell Equivalents

• The Bourne Shell only has string variables.• It uses expr to switch string variables into

numbers:

$ number=5 (Bourne)$ number=‘expr $number + 2‘

$ @ number = 5 (C Shell)$ @ number = $number + 2

Page 43: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

43

Special Forms of User Variables

• $#VAR (returns number of elements in VAR array)

• $?VAR (returns 1 if VAR is declared; 0 otherwise)

Page 44: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

44

• $ set days = (mon tues wed thurs fri)

$ echo $#days5

$ echo $?days1

$ unset days

$ echo $?days0

days is declared

days is not declared

Page 45: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

45

Shell Variables

• Shell variables can be initialized in three ways:• by the shell• by the environment

• by the user with set or setenv

Page 46: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

46

• There are two types of shell variable:• shell variables that take values;

• shell variables that act as switches• they have the value 0 or 1

Page 47: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

47

Shell Vars that take Values

• $argv[0] or $0 command name• $argv[1] or $1 first argument

of command• Also $2,$3,...

• no upper limit; no need for shift

• $argv[*] or $* array of all arguments• $#argv number of arguments

Page 48: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

48

• $HOME pathname to user’s home directory

• $PATH array of pathnamesto commands

$ setenv PATH (/usr/bin /usr/ucb /bin ~/bin)

• $status exit status of last command

• etc...

Page 49: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

49

Shell Vars that act as Switches• $filec turns on file completion

• $ignoreeof disables ctrl-D as logout

• $noclobber stops file overwriting with >

• $notify immediate notification about

background job changes• etc...

Use: $ set ignoreeof

Page 50: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

50

Automated Scripts

• At login, the C Shell executes a range of scripts automatically:• /etc/csh.login, /etc/csh.cshrc• .login in your home directory• .cshrc in your home directory

• At logout, it executes:• /etc/csh.logout

• .logout in your home directory

Page 51: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

51

• When a shell script is called, the shell will execute the cshrc scripts first.

• The cshrc scripts are also executed whenever a new window is created in a X-Windows session.

Page 52: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

52

Examples

• $ cat .loginsetenv TERM vt100stty erase ‘^X’ kill ‘^U’ -lcase -tabsecho “This is who is on the machine:”who

Page 53: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

53

• $ cat .cshrcset noclobberset ignoreofset history = 100set prompt = “! % “set PATH = (/usr/bin /usr/ucb /bin ~/bin)

alias h historyalias ll ls -lg

Only set is needed(not setenv), since.cshrc is executed atthe start of every script.

Page 54: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

54

• $ cat .logoutecho Switch off the air-conditionersleep 10

Page 55: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

55

Script Programming

Executing a C Shell Script

Branching

Looping

Recursion

Interrupt Handling

Page 56: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

56

Executing a C Shell Script

• Put #!/bin/csh as the first line• may need #!/bin/csh -f to switch off the default initial

execution of the cshrc scripts

• Or execute:$ csh script

• Or make csh your default shell.

Page 57: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

57

Branching

• if-then

• File Test Expressions

• Switch

Page 58: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

58

if-then

• Formats:

if (expression) then commandsendif

if (expression) then commandselse commandsendif

Page 59: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

59

if (expression) then commandselse if (expression) then commands :else commandsendif

Page 60: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

60

• $ cat if_else1#!/bin/csh -f# Set class depending on argument value

set number = $argv[1]if ($number < 0) then @ class = 0else if ($number >= 0 && $number < 100) then @ class = 1else if ($number >= 100 && $number < 200)then @ class = 2else @ class = 3endifecho The number $number is in class $class

Page 61: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

61

Usage

• Due to the #!/bin/csh line, the script can be executed from the Bourne/Bash command line:

$ if_else1 55The number 55 is in class 1$

Page 62: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

62

File Test Expressions

• These occur in the expression part of if-then and other control structures.

-d file file is a directory

-f file file is a file

-e file file exists

-z file file is 0 bytes long

-r file file is readable

-w file writable

-x file executable

Page 63: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

63

Switch

• Format:switch (string variable) case pattern: commands breaksw case pattern: commands breaksw : default: commands breakswendsw

Page 64: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

64

Example

• $ cat switch_1#!/bin/csh -f# See if first argument is yes or no. # Deal with a mix of upper and lower case.

# Does argv[1] exist?if ($#argv == 0) then echo “Usage: switch_1 [yes | no]” exit 1endif

:

Usage: $ switch_1 yes Argument 1 is yes $

Page 65: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

65

switch ($argv[1]) case [yY][eE][sS]: echo Argument 1 is yes breaksw case [nN][oO]: echo Argument 1 is no breaksw default: echo Argument 1 is neither yes or no breakswendsw

Page 66: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

66

Looping

• Foreach

• While

Page 67: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

67

foreach

• Format:foreach loop-index (argument-list) commandsend

Page 68: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

68

Example 1• $ cat refname

#!/bin/csh -f# Usage: rename arg1 arg2## Changes the string arg1 to arg2 # in the filenames of every file in # the current working directory.

if ($#argv != 2) then echo Usage: refname arg1 arg2 exit 1endif

:

Usage: $ refname CPP C

Page 69: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

69

foreach i (‘ls‘) set newname = ‘echo $i | sed s/$1/$2/‘ mv $i $newnameend

The text in ithat matches $1 ischanged to $2

VERY DANGEROUS

Page 70: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

70

• $ cat foo-six#!/bin/csh -f# Assign up to 6 command line args # to the buffer array. Execute foo # with buffer as its arguments

set buffer = (0 0 0 0 0 0)@ count = 1

if ($#argv > 6) then echo “Usage: foo-six [up to 6 args]” exit 1endif

:

Example 2 Usage: $ foo-six a b c

Page 71: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

71

foreach arg ($argv[*]) set buffer[$count] = $arg @ count++end

echo There were $count argumentsexec foo $buffer[*]exit 0

expands to $argv[0],$argv[1], etc.

expands to $buffer[0],$buffer[1], etc.

VERY DANGEROUS

Page 72: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

72

while

• Format:while (expression) commandsend

Page 73: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

73

• $cat while_1# Sum the numbers between 1 and # the value in argv[1]

@ limit = $argv[1]@ index = 1@ sum = 0

while ($index <= $limit) @ sum += $index @ index++end

echo The sum is $sum

ExampleUsage: $ while_1 20 The sum is 210

Page 74: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

74

Recursion

• The script cppc visits every directory below the one supplied as its argument and renames any files ending with “.CPP” to end with “.c”• e.g. foo.CPP --> foo.c

• usage:$ cppc code/

• cppc could be implemented with find

Page 75: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

75

cppc#!/bin/csh -f# cppc “ replace all .CPP extensions with .c

if ($#argv != 1) then echo “Usage: $0 directory” exit(1)endif

foreach file ($1/*) # files in directory if (-f $file) then if ($file:e == ‘CPP’) then mv $file $file:r.c echo Modifying $file to $file:r.c endif else if (-d $file) then $0 $file # recursive call endifend

VERY DANGEROUS

Page 76: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

76

Notes• Pulling apart a filename that has an extension

(e.g. foo.bar)

$ set var = foo.bar

$ echo var:r # name without extensionfoo

$ echo var:e # just the extensionbar

Page 77: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

77

A Standard Shape

• Most recursive scripts have the following 'shape':

foreach file ($1/*) # files in directory if (-f $file) then # do something to the file else if (-d $file) then $0 $file # recursive call endifend

Page 78: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

78

Title Words in Web files

• The wtitle script examines each “.html” file in the directories below the one supplied as its argument.

• If outputs the title words on each Web page• e.g

if it sees: <title>My Home Page</title>

then it prints:MyHomePage

Page 79: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

79

wtitle#!/bin/csh -f# wtitle: list the title words in HTML files

if ($#argv != 1) then echo Usage: $0 directory exit(1)endif

foreach file ($1/*) if ((-f $file) && ($file:e == “html”)) then grep -i title $file | \ awk -F\> ’{print $2}’ | \ awk -F\< ’{print $1}’ | \ tr ’ ’ ’\n’ | tr ’A-Z’ ’a-z’ else if (-d $file) then $0 $file # recursive call endifend

Page 80: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

80

Notes• grep -i title $file gets a title tag line:

<title>My Home Page</title>

• awk -F\> ’{print $2}’ separates the line using ‘>‘ and prints the second part:

My Home Page</title>

• awk -F\< ’{print $1}’ separates the line using ‘<‘ and prints the first part:

My Home Page

Page 81: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

81

Usage• $ wtitle caine/

michaelcainecainepicturecaine :

• $ wtitle caine/ | sort | uniqcainemichaelpicture :

sorted and duplicates removed

Page 82: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

82

Interrupt Handling

• Not as good as Bourne• only interrupt is supported• the control flow is hard to understand• much improved in tcsh

• Format:onintr label

• The script must have a line:label:

Page 83: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

83

• $ cat onintr_1#!/bin/csh -f

onintr close

while (1) echo Program is running. sleep 2end

close:echo End of Program

Usage: $ onintr_1 Program is running Program is running

::

End of Program

ctrl-Ctyped

Page 84: 1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell

84

The End

• Practice writing scripts in different shells

• You will figure out that environments support a subset of commands from the others.

• Bourne is still the default and most popular for most linux distributions.