10. tc Shell (Part 2)

tc Shell (Part 2)

if statement

  • When there is only one statement in the true block, and there is no false block, the statement can be all on one line:

if ( expression ) command

  • The complete if else statement is:

if ( expression ) then

commands for true evaluation

else

commands for false evaluation

endif

    • The if … then must be on the same line
  • The if else if statement is:

if ( expression1 ) then

commands for case 1

else if ( expression2 ) then

commands for case 2

else

commands for default case

endif

  • There can be as many else if cases as necessary
  • In each case, the expression has to evaluate to true or false
  • When the expression is a utility or sequence of utilities that the shell must evaluate, then the expression must be surrounded by { } and if there is output to the utility, then the output should be redirected to /dev/null

if { ( commands to be evaluated to true/false ) } then

commands for true evaluation

else

commands for false evaluation

endif

  • By surrounding the if condition with { }, tcsh will run the commands and return true or false so it can be used by the if statement. The format ( `commands` ) will not be evaluated correctly for true / false.
  • For example:

if { ( who | grep $userid > /dev/null ) } then

echo “$userid is logged in”

else

echo “$userid is not logged in”

endif

  • Without the redirection to /dev/null, if the userid is found in the who output, the corresponding line of the who output will be printed to screen

switch statement

  • Similar to the C language, the switch statement is used to select one choice out of many choices

switch ( variable )

case value1:

commands

breaksw

case value2:

commands

breaksw

default:

commands

endsw

  • When variable matches value1 or value2, the appropriate commands run. If variable does not match any given value, the commands of the default case run

Loops

The foreach loop

  • Format

foreach variable ( list )

commands

end

  • Examples

foreach file ( `ls`)

echo $file print filenames in current directory

end

set nums = (2 4 6 8 )

foreach val ($nums)

echo $val print all values of nums array,

end one per line

The while loop

  • Format

while ( expression )

commands

end

  • Example

@ num = 10

while ( $num > 0 )

echo $num print 10 to 1, one number per line

@ num--

end

Functions

  • There is no support for functions in tcsh

Signals

  • Signal handling is with the built-in function onintr, for on interrupt
  • onintr only works with control-c
  • There are 3 ways to use onintr:
    • onintr label Go to code at label when there is a control-c signal
    • onintr - Disable control-c signal
    • onintr Reset to the default signal handling for interrupt..label:.commands
      • exit 1 # or: goto label2
  • There is no handling support for other signals for tcsh. It is assumed that you will write a C program if you need to handle anything more complex than control-c from the keyboard. C has many libraries to support signal handling at a finer detail than any of the shells do

Final Thoughts on Shells

  • The shells we covered are 3 of the most common modern shells
    • bash is the most commonly used shell due to the popularity of Linux and the many features of bash. In addition the Mac’s default shell is also the bash shell.
    • ksh is similar to bash and came before bash, when Linux was not yet widely used. Therefore Unix programmers and those who came from a Unix environment prefer ksh.
    • tcsh is preferred by die hard csh fans because it is most similar to csh. csh at one time was the only one of two choices of shells (the other being the Bourne shell), so as a result an entire ‘generation’ of technical people ‘grew up’ with csh. Many csh fans nowaday have converted to bash, but there are still some supporters for tcsh.
  • Of course there are many other *nix shells, with zsh being one of the prominent ones
  • You can always choose which shell you want to use and work with. But even if you need to write a script for a different shell than what you normally use, it’s mostly a matter of changes in syntax only. The general concepts of shell scripting are the same