C Shell CIS 218 C Shell overview Csh (new version tchs) is a - - PowerPoint PPT Presentation

c shell
SMART_READER_LITE
LIVE PREVIEW

C Shell CIS 218 C Shell overview Csh (new version tchs) is a - - PowerPoint PPT Presentation

C Shell CIS 218 C Shell overview Csh (new version tchs) is a command language interpreter developed under BSD UNIX. It incorporates features of other shells and a history mechanism. Tcsh development is currently maintained by tcsh.org.


slide-1
SLIDE 1

C Shell

CIS 218

slide-2
SLIDE 2

C Shell overview

  • Csh (new version tchs) is a command language interpreter developed under BSD
  • UNIX. It incorporates features of other shells and a history mechanism. Tcsh

development is currently maintained by tcsh.org.

  • Developed for C programmers who don’t want to learn another language syntax.
  • Most of the rules for command alias, job control, redirection, regular expressions,

quoting and filename expansion apply to the C Shell same as the other shells.

  • The C Shell parses words/tokens following the same rules as bash
  • In addition to the standard file and directory manipulation commands, C Shell also

supports the pushd and popd commands for directory navigation. Commands also now supported under bash.

slide-3
SLIDE 3

Shell operation

  • During startup the C shell begins by executing commands in sequence from:

/etc/csh.cshrc, /etc/csh.login, ~/.tcshrc or ~/.cshrc, ~/.history (or the value of the histfile shell variable) ~/.login ~/.cshdirs (or the value of the dirsfile shell variable) .login may be read before the other files depending on C Shell copile options. Non-login shells read only /etc/csh.cshrc and ~/.tcshrc or ~/.cshrc on startup.

  • At logout, it executes:

/etc/csh.logout ~/.logout in your home directory

slide-4
SLIDE 4

Shell variables

  • Each shell variable has as value an array of zero or more strings. Shell variables are

assigned values by the set command of the form: set name=value

  • As with bash, value can be null. $?name tests if a variable has been defined.

$#name determines size of the value.

  • The setenv command can be used to set variables in the environment defined during

C shell startup. Thus setenv TERM adm3a will set the value of the environment variable TERM to ‘adm3a’. Notice the absence of “=”. A user program printenv exists which will print out the environment variables.

  • The unalias and unset commands can be used to remove aliases and variable

definitions from the shell, and unsetenv removes variables from the environment.

slide-5
SLIDE 5

Shell Environment Variables

  • $filec

turns on file completion

  • $ignoreeof

disables ctrl-D as logout

  • $noclobber

stops file overwriting with

  • $notify

immediate notification about background job changes

  • $HOME

user’s home directory path

  • $PATH

array of pathnames to commands

  • $status

exit status of last command

slide-6
SLIDE 6

Variable substitution

  • After each input line is broken into words and history substitutions are done on it, the

input line is parsed into distinct words separate by the IFS or whitespace character.

  • Variable substitution is done on these words. Variables are referenced in the same

fashion as bash, by preceding the variable name with a $, i.e. echo $TERM. As with bash, this is direct string substitution.

  • A number of notations are provided for accessing components and attributes of

variables.

  • The notation $?name expands to ‘1’ if name is set or to ‘0’ if name is not set. This is

a mechanism used for checking whether particular variables have been assigned values.

  • C Shell follows the same variable quoting rules as bash.
slide-7
SLIDE 7

Variable arrays

  • C Shell can store variables in a list called arrays. Values are assigned with a list

delimited by parentheses and IFS/whitespace separating the individual values corresponding with the usual variable quoting rules. Each array element is referenced by the $variable name and an index # in square brackets[]. For example:

$ set colours = (red green blue orange) $ echo $colours red green blue orange $ echo $colours[3] blue $ echo $colours[2-4] green blue orange

  • In addition to $?name, above $#name specifies the number of elements in the array.

$*name and $name both reference the entire array as one variable.

  • Arrays can be defined and initialized separately:

$ set shapes = (’’ ’’ ’’ ’’ ’’) $ echo $shapes $ set shapes[4] = square $ echo $shapes[4] square

slide-8
SLIDE 8

Numeric Variables

  • Use the @ command for variables holding numbers.

$ @ count = 0 (or set count = 0) $ echo $count $ @ count = ( 5 + 2 ) $ echo $count 7 $ @ result = ( $count < 5 ) $ echo $result

  • C Shell uses the same arithmetic syntax as BASH 4.0 (other than use of @):

@ count = $count + 5 $ echo $count 12 $ @ count += 5 $ @ count++ $ echo $count 13

slide-9
SLIDE 9

Numeric / logical operators

  • Operator

What it Means () change precedence ~ complement ! negation */ % multiply, divide, modulo + - add, subtract << > > left shift, right shift <= >= < > relational operators == != =~ !~ string comparison/pattern matching & bitwise "and" ^ bitwise "exclusive or" | bitwise "inclusive or" && logical "and" || logical "or"

slide-10
SLIDE 10

Numeric Arrays

$ set ages = (0 0 0 0 0) $ @ ages[2] = 15 $ @ ages[3] = ( $ages[2] + 4 ) $ echo $ages[3] 19 $ echo $ages 0 15 19 0 0

slide-11
SLIDE 11

C Shell Command Line Arguments

  • Referenced in C Shell as an array - $argv
  • $argv[0] or $0

command name

  • $argv[1] or $1

first argument

  • f command
  • Also $argv[2] or $2, $argv[3][ or$3,... no upper limit; no need for shift
  • $argv[*] or $*

array of all arguments

  • $#argv

number of arguments

slide-12
SLIDE 12

System Variables

  • $name or ${name} Substitutes the words of the value of variable name, each separated by a
  • blank. Braces insulate name from following characters which would
  • therwise be part of it.
  • $name[selector] or ${name[selector]} Substitutes only the selected words from the value of

name by #.

  • $0

Substitutes the name of the file from which command input is being read.

  • $number or ${number} Equivalent to `$argv[number]`.
  • $*

Equivalent to `$argv`, which is equivalent to `$argv[*]`

  • $argv[N]

same as above

  • $argv

same as above

  • $?name or ${?name} Substitutes the string `1` if name is set, `0` if it is not.
  • $?0

Substitutes `1` if the current input filename is known, `0` if not. `0` in interactive shells.

  • $#name or ${#name} Substitutes the number of words in name.
  • $#

Equivalent to `$#argv`.

  • $%name or ${%name} Substitutes the number of characters in name.
  • $%number or ${%number} Substitutes the number of characters in $argv[number].
  • $?

Equivalent to `$status`.

  • $$

Substitutes the (decimal) process number of the (parent) shell.

  • $!

Substitutes the (decimal) process number of the last background process started

  • $_

Substitutes the command line of the last command executed.

  • $(

Substitutes a line from the standard input, with no further Interpretation. Used to read from the keyboard in a shell script.

slide-13
SLIDE 13

C Shell Scripts

  • 1) Make executable
  • 2) Invoke C
  • #!/bin/csh

as the first line normal execution

  • #!/bin/csh -f

as first line to switch off the default initial execution of the .cshrc scripts

  • $ csh script
  • $ chsh csh
  • Debugging

#!/bin/csh –vx

slide-14
SLIDE 14

C Shell Control Structures

  • File Test Conditions
  • These occur in the expression part of if-then and other condition tests
  • d file

file is a directory

  • f file

file is a regular file

  • e file

file exists

  • z file

file is 0 bytes long

  • r file

file is readable

  • w file

writable

  • x file

executable

  • o file
  • wnership
  • s file

Non-zero size

  • l file

Symbolic link

  • b file

Block special file

  • c file

Character special file

  • p file

Named pipe (fifo)

slide-15
SLIDE 15

C Shell Control Structures

  • if-then

if (expression) then commands endif if (expression) then commands else commands endif if (expression) then commands else if (expression) then commands else commands endif

slide-16
SLIDE 16

C Shell Control Structures

  • If Example:

#!/bin/csh -f # Set class depending on argument value set number = $argv[1] if ($number < 0) then @ class = 0 else if ($number >= 0 && $number < 100) then @ class = 1 else if ($number >= 100 && $number < 200)then @ class = 2 else @ class = 3 endif echo The number $number is in class $class

slide-17
SLIDE 17

C Shell Control Structures

  • switch (string variable)

case pattern: commands breaksw case pattern: commands breaksw : default: commands breaksw endsw

  • break # causes case processing to terminate (same as bash)
  • breaksw # causes case processing to terminate for this selection (switch)
slide-18
SLIDE 18

C Shell Control Structures

  • Switch Example:

#!/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 1 endif 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 breaksw endsw

slide-19
SLIDE 19

C Shell Control Structures

  • foreach

foreach loop-index (argument-list) commands end

  • Example:

#!/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 1 endif foreach i (‘ls‘) set newname = ‘echo $i | sed s/$1/$2/‘ mv $i $newname end

slide-20
SLIDE 20

C Shell Control Structures

  • Foreach Example
  • #!/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 1 endif foreach arg ($argv[*]) set buffer[$count] = $arg @ count++ end echo There were $count arguments exec foo $buffer[*] exit 0

slide-21
SLIDE 21

C Shell Control Structures

  • while

while (expression) commands end Example: 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

C Shell Control Structures

slide-22
SLIDE 22

C Shell Control Structures

  • goto Command
  • goto label

...

  • ther shell commands

... label: shell commands

slide-23
SLIDE 23

C Shell Control Structures

  • Recursion (script calling itself) is allowed in C Shell (and C):
  • Example:

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

slide-24
SLIDE 24

C Shell Control Structures

  • Interrupt Handling: onintr label

The script must have a line: label: Transfers control to label after intercepting Ctrl C

  • Example:

#!/bin/csh -f

  • nintr close

while (1) echo Program is running. sleep 2 end close: echo End of Program

  • Keyboard input:

set x = $<

  • or –

set x = `head -1`

  • reads input from the keyboard up until RETURN <LF> and places results in x.
slide-25
SLIDE 25

C Shell Futures

  • Csh is now tcsh under newer systems.
  • Development now separated from UNIX BSD flavors; supported by tcsh.org.
  • Programatically not as sophisicated as Korn shell. But Korn contains most C shell

arithmetic and logical functions.

  • More C shell features in Korn shell and later versions of bash.
  • UNIX programming now usually done in environments other than UNIX shell, limiting

audience for C shell.

  • LINUX/UNIX system scripts in Bourne (again) Shell.