250 likes | 395 Views
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.
E N D
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. • 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 • 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
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 • $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
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 • 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
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 $count12 $ @ count += 5 $ @ count++$ echo $count13
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"
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
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
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 • 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 • 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 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)
C Shell Control Structures • if-then if (expression) then commandsendif if (expression) then commandselse commandsendif if (expression) then commands else if (expression) then commands else commands endif
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 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 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 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 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 Structures • while while (expression) commandsend Example: 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 Structures • goto Command • goto label ... other shell commands ... label: shell commands
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 Structures • Interrupt Handling: onintr label The script must have a line: label: 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 • 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.