There is a very nice tutorial on scripting with bash on TecAdmin.net.
I like the way the material is presented. Below, I will present a tcsh scripting tutorial using a similar structure.
I will not go into too much details but just mention how to do something similiar in tcsh .
Links to sections on this page:
Only the first line is different from bash.
#!/bin/tcsh -f echo "Hello World"
Synatx is different from bash.
To set a variable's value, must use the keyword set and must use an equal sign (i.e., "=").
Since tcsh does not have functions, all variables are global variables.#!/bin/tcsh -f set NAME = "TecAdmin Tutorials" echo $NAME set launchdate = "Feb 08, 2013" echo $launchdate Environment variables can also be accessed directly in tcsh (e.g., $PATH, $SHELL, $HOME, $LANG, $PWD, etc.).
Unlike bash,
you cannot turn debugging on and off inside a tcsh script. But if you have a tcsh script, say, "foo.csh",
you can run it in full-trace mode by doing:
tcsh -xv foo.csh
Unlike bash,
if you execute a pieces of bad code in tcsh, tcsh script will stop running. Test drive the following code to see:
#!/bin/tcsh -f rm -f /tmp/tesfile.txt echo "hi" > /tmp/tesfile.txt ls -l /tmp/tesfile.txt chmod 0444 /tmp/tesfile.txt ls -l /tmp/tesfile.txt echo "hi" > /tmp/tesfile.txt echo "Cannot reach this line of code."
Synatx is a bit different from bash.
Things like "$@" and "$?" are not available in tcsh.
Commandline arguments can be accessed as $argv[$i] where $i ranges from 1 to the number of commandline arguments (denoted by "$#")
and "$#" gives you the number of commandline arguments.
Below is a program and stores the commandline arguments in a list and then print the list out.
#!/bin/tcsh -f set num_args = $# set i = 1 set list = ( ) echo "Build the list from commandline arguments..." while ($i <= $num_args) @ i_minus_1 = $i - 1 set arg = $argv[$i] echo "\targ[$i_minus_1] = $arg" set list = ( $list $arg ) @ i = $i + 1 end echo "Now print the list..." foreach f ($list) echo "\t$f" end
Synatx is the same as bash.
Tcsh also uses backquotes to run shell commands in
the same way as bash.
Tcsh syntax is very different from bash.
The code below demonstrate how you read a string from the user using "$<":
#!/bin/tcsh -f echo -n "Enter a number randomly chosen between 1 and 10: " set x = $< if ($x < 1 || $x > 10) then echo "Invalid input entered: $x" else echo "You have entered: $x" endif
Tcsh syntax is very different from bash.
You must begin a line with "@" for an arithmetic operation.
Please see the example above regarding how to add and subtract an integer from a variable.
Synatx is similar to bash,
except you don't need two pairs of parentheses.
Please see the example above regarding how to compare $i and $num_args.
Tcsh syntax is similar to bash.
The code below reads a string from the user, checks to see if it's an empty string. If not, it does a pattern match
to see if the string ends with ".gif".
#!/bin/tcsh -f echo -n "Enter a file name: " set name = $< if ($name == "") then echo "Did you enter an empty string?" else if ($name =~ "*.gif") then echo "GIF file name: $name" else echo "Not a IF file name: $name" endif endif
Tcsh syntax is different from bash.
Please see the example above regarding nested if-then-else statements.
Tcsh syntax is different from bash.
Instead of using for, tcsh uses foreach.
Please see the example above regarding iterating through a list using foreach.
Tcsh syntax is slightly different from bash.
Please see the example above regarding how to use while to loop as long as a condition is true.
Tcsh syntax is different from bash.
The code below reads a string from the user, checks to see if it equals to one of the cases.
#!/bin/tcsh -f echo -n "Enter a number randomly chosen between 1 and 10: " set x = $< switch ( $x ) case "1", "3", "5", "7", "9": echo "You have entered an odd number between 1 and 10: $x" breaksw case "2", "4", "6", "8", "10": echo "You have entered an even number between 1 and 10: $x" breaksw default: echo "You have entered: $x" endsw
|