C Shell - PowerPoint PPT Presentation

C shell
1 / 25

  • Uploaded on
  • Presentation posted in: General

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.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Download Presentation

C Shell

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

C shell

C Shell

CIS 218

C shell overview

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.

Shell operation

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)


    ~/.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:


    ~/.logout in your home directory

Shell variables

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.

Shell environment variables

Shell Environment Variables

  • $filecturns on file completion

  • $ignoreeofdisables ctrl-D as logout

  • $noclobberstops file overwriting with

  • $notifyimmediate notification about background job changes

  • $HOMEuser’s home directory path

  • $PATHarray of pathnames to commands

  • $statusexit status of last command

Variable substitution

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.

Variable arrays

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]


Numeric variables

Numeric Variables

  • Use the @ command for variables holding numbers.

    $ @ count = 0(or set count = 0)

    $ echo $count


    $ @ count = ( 5 + 2 )

    $ echo $count


    $ @ result = ( $count < 5 )

    $ echo $result


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

    @ count = $count + 5$ echo $count12

    $ @ count += 5

    $ @ count++$ echo $count13

Numeric logical operators

Numeric / logical operators

  • OperatorWhat it Means

    ()change precedence



    */ %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"

Numeric arrays

Numeric Arrays

$ set ages = (0 0 0 0 0)

$ @ ages[2] = 15

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

$ echo $ages[3]


$ echo $ages

0 15 19 0 0

C shell command line arguments

C Shell Command Line Arguments

  • Referenced in C Shell as an array - $argv

  • $argv[0] or $0command name

  • $argv[1] or $1first argument

    of command

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

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

  • $#argvnumber of arguments

System variables

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 otherwise 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.

C shell scripts

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

C shell control structures

C Shell Control Structures

  • File Test Conditions

  • These occur in the expression part of if-then and other condition tests

    -d filefile is a directory

    -f filefile is a regular file

    -e filefile exists

    -z filefile is 0 bytes long

    -r filefile is readable

    -w filewritable

    -x fileexecutable

    -o fileownership

    - s file Non-zero size

    - l file Symbolic link

    - b file Block special file

    - c file Character special file

    - p file Named pipe (fifo)

C shell control structures1

C Shell Control Structures

  • if-then

    if (expression) then commandsendif

    if (expression) then commandselse commandsendif

    if (expression) then


    else if (expression) then





C shell control structures2

C Shell Control Structures

  • If Example:

    #!/bin/csh -f# Set class depending on argument valueset 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

C shell control structures3

C Shell Control Structures

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

  • break # causes case processing to terminate (same as bash)

  • breaksw # causes case processing to terminate for this selection (switch)

C shell control structures4

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 1endif

    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

C shell control structures5

C Shell Control Structures

  • foreach

    foreach loop-index (argument-list) commandsend

  • 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 1endif

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

C shell control structures6

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 argumentsset buffer = (0 0 0 0 0 0)@ count = 1if ($#argv > 6) then echo “Usage: foo-six [up to 6 args]” exit 1endif

    foreach arg ($argv[*]) set buffer[$count] = $arg @ count++endecho There were $count argumentsexec foo $buffer[*]exit 0

C shell control structures7

C Shell Control Structures

  • while

    while (expression) commandsend


    Sum the numbers between 1 and # the value in argv[1]@ limit = $argv[1]@ index = 1@ sum = 0while ($index <= $limit) @ sum += $index @ index++endecho The sum is $sum

C Shell Control Structures

C shell control structures8

C Shell Control Structures

  • goto Command

  • goto label


    other shell commands



    shell commands

C shell control structures9

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 endifend

C shell control structures10

C Shell Control Structures

  • Interrupt Handling: onintr label

    The script must have a line:


    Transfers control to label after intercepting Ctrl C

  • Example:

    #!/bin/csh -fonintr closewhile (1) echo Program is running. sleep 2endclose: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.

C shell futures

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.

  • Login