Two versions:
/bin/csh
: The original BSD version of csh./bin/tcsh
: An improved version of csh called tcsh.When you login the shell is started in your home directory and commands are read from .cshrc
and .login
history
: Command to view command history.alias
mechanism to make transformations on commands.unalias
to unalias commands.fg
, bg
, and ^Z
work the same.source
can be used to read commands into the current shell.Special Variables:
$0
: Name of the script
$1
, $2
, ...
, $9
, ${10}
, ${11}
, ...
: Parameter variables
$*
: All parameters
$#argv
: Number of arguments
$arv[1]
, $arr[2]
, …: Parameter variables
$argv[*]
: All arguments
$argv
: All arguments
$#argv
: Number of arguments
$argv[$#argv]
: Last argument
$<
: Reads a line from stdin
up to but not including newline.
set string=( $string )
Example
#!/bin/csh echo "Name: " set name=$< echo Greetings, $name
Operator | Description |
---|---|
() | Grouping |
! | Logical note |
> >= < <= | Greater than, less than |
&& | Logical and |
=~ | String matches |
!~ | String does not match |
if (expression) command
if (expression) command then
command
endif
if (expression) command then
command
else if (expression) then
else
command
endif
Example
if ($#arv < 2) then
echo "Too few arguments"
exit
endif
-e file
: File merely exists (may be protected)-r file
: File exists and is readable,-w file
: File is writeable-x file
: File is executable-o file
: File is owned-z file
: File has size 0-f file
: File is an ordinary file-d file
: File is a directory@
)#!/bin/csh
if ( $#argv == 0 ) then
echo -n "Enter time in minutes: "
<
@ min = $else
$1
@ min = endif
$min * 60
@ sec = echo “$min minutes is $sec seconds”
#!/bin/csh -f
# Script name: colors
echo -n "Which color do you like? "
set color = $<
switch ("$color")
case bl*:$color
echo I feel $color
echo The sky is
breaksw
case red:# Is is red or is it yellow?
case yellow:$color.
echo The sun is sometimes
breaksw
default:$color not one of the categories.
echo
breakswendsw
Predetermined Iterations:
repeat
repeat command number
command
number
times.repeat 100 echo "Hello"
foreach
foreach name ( wordlist )
commandsend
Condition-based Iterations:
while
while ( expression )
commandsend
Example:
foreach
set list = ( one two three ) foreach word ( $list ) echo $word end
Example:
while
#! /bin/csh @ var = 5while ( $var > 0 ) echo $var @ var --end
break
continue
"..."
$
keeps meaning!
history references keep meaning'...'
!
eval
eval
: Evaluates string and executes the result.
Example
set x=23
set y=x
eval echo \$$y
% csh –n scriptname
% csh –v scriptname
% csh –x scriptname
Note: Flags can also be added to shebang