1.
Usage of switch and case to test whether the script argument is yes or no regardless of upper and/or lower case.
#!/bin/csh
#----------case.csh: switch/case example in csh
#
echo "Proc "$0": demo for csh switch/case"
echo ""
if ($#argv == 0) then
echo "Proc arg missing"
else
switch ($argv[1])
case [yY][eE][sS]:
echo "argv[1] is 'yes'"
breaksw
case [nN][oO]:
echo "argv[1] is 'no'"
breaksw
default:
echo "invalid arg: must be yes or no"
breaksw
endsw
endif
#
#----------end script: switch/case------------------
2.
The following example uses the numeric value of the script argument to compute a digit according to argument sign and magnitude.
#!/bin/csh
#----------if.csh: if-then-else example
#
echo "Proc "$0": demo for csh if-then-else"
echo ""
set class
set number = $argv[1]
#
if ($number < 0) then @ class = 0 else if (0 <= $number && $number < 10) then @ class = 1 else @ class = 2 endif # echo "No. $number class ==> $class"
echo ""
#
#----------end script: if/then/else------------------
3.
Example of the while loop. The script computes the sum of the first n integers where n is the script argument.
#!/bin/csh
#----------sum.csh: while example in csh
#
# sum first n integers with n = sum argument
#
echo "Proc "$0": demo for csh while"
echo ""
set limit = $argv[1]
set index = 1
set sum = 0
#
while ($index <= $limit)
@ sum += $index
@ index++
end
#
echo "Sum: $sum"
#
#----------end script: sum------------------
4.
In C shell a script can read user input with the set command (see Bourne read builtin) with the following syntax:
#!/bin/csh
#-----------read user input
#
echo "Please enter a file name:"
set file = $< # C shell input from stdin
echo "just read var is: $file "
dir $file
#
#----------end script: read user input------------------
Print this post
No comments:
Post a Comment