c shell
play

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.


  1. C Shell CIS 218

  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.

  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

  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.

  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 • user’s home directory path $HOME • $PATH array of pathnames to commands • $status exit status of last command

  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.

  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

  8. Numeric Variables • Use the @ command for variables holding numbers. $ @ count = 0 (or set count = 0) $ echo $count 0 $ @ count = ( 5 + 2 ) $ echo $count 7 $ @ result = ( $count < 5 ) $ echo $result 0 • 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

  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"

  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

  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 of 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

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

  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

  14. C Shell Control Structures • File Test Conditions • These occur in the expression part of if-then and other condition tests file is a directory -d file file is a regular file -f file file exists -e file file is 0 bytes long -z file file is readable -r file writable -w file executable -x file -o file ownership - s file Non-zero size - l file Symbolic link - b file Block special file - c file Character special file - p file Named pipe (fifo)

  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

  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

  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)

  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

  19. C Shell Control Structures • foreach foreach loop-index (argument-list) commands end • Example: #!/bin/csh -f # Usage: rename arg 1 arg 2 # # Changes the string arg 1 to arg 2 # in the filenames of every file in # the current working directory. if ($#argv != 2 ) then echo Usage: refname arg 1 arg 2 exit 1 endif foreach i (‘ls‘) set newname = ‘echo $i | sed s/$ 1 /$ 2 /‘ mv $i $newname end

  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

Recommend


More recommend