#!/bin/tcsh
 
# BEGIN: alias
alias foo bar
# END
 
# BEGIN: array: declaration
set ARRAY = ("a" "b" "c")
# END
 
# BEGIN: array: size
echo $#ARRAY items
# END
 
# BEGIN: array: first item
echo "first item [1] = $ARRAY[1]"
# END
 
# BEGIN: array: iterate by item
foreach item ( $ARRAY )
    echo item = $item
end
# END
 
# BEGIN: array: iterate by item, single line
#@@ N/A
# END
 
# BEGIN: array: iterate by index
foreach i ( `seq $#ARRAY` )
    echo $i $ARRAY[$i] |\
        awk '{printf("%d %s\n",$1,$2);}'
end
# END
 
# BEGIN: variable: create scalar
set i=0
set="foo bar spam"
# END
 
# BEGIN: variable: create array
set ARRAY=("item1" "item2" "item2")
# END
 
# BEGIN: variable: destroy or unset
set ARRAY=("item1" "item2" "item2")
unset ARRAY
# END
 
# BEGIN: variable: auto increment
set i=0
@ i++
echo auto increment = $i
# END
 
# BEGIN: variable: auto decrement
set i=0
@ i--
echo auto decrement = $i
# END
 
# BEGIN: function
#@@ N/A
#@@ Can be emulated using 
#@@ external scripts
# END
 
# BEGIN: job: start in the background
#@@ job &
# END
 
# BEGIN: job: move into the background
#@@ job
#@@ <Ctrl-Z>
#@@ bg
# END
 
# BEGIN: job: move into the foreground
#@@ fg
#@@ fg %1
# END
 
 
# BEGIN: job: status
#@@ job
#@@ echo status = $?
# END
 
# BEGIN: variable: debug tracing
set verbose # on
pwd
unset verbose # off
pwd
# END
 
# BEGIN: variable: dump state
set
# END
 
# BEGIN: variable: turn on unset variable checking
#@@ N/A
# END
 
# BEGIN: variable: automatically exit on error
#@@ /bin/tcsh -e # invocation only
# END
 
# BEGIN: variable: print environment variables
env
printenv
# END
 
# BEGIN: variable: dynamic
# Not native, need to use eval.
set a="some value"
set b="a"
eval "echo $b = "'$'${b}
set bv=`eval echo '$'$b`
echo bv = $bv
# END
 
# BEGIN: IO: redirect stdout
ls > /tmp/$$
# END
 
# BEGIN: IO: redirect stderr
( ls > /dev/tty ) >& /tmp/$$
# END
 
# BEGIN: IO: redirect stdout and stderr
ls >& /tmp/$$
# END
 
# BEGIN: exit the shell
#@@ exit
#@@ exit 1
# END
 
# BEGIN: formatted printing
# No native support, 
# emulate with awk. 
# Embedded spaces are a pain.
echo 42 "Deep Thought" |\
  awk '{printf("%05d %s %s\n",$1,$2,$3);'}
# END
 
# BEGIN: if/then/else: numeric
set i=2
if ($i>2) then
    echo i greater than 2
else if ($i<2) then
    echo i less than 2
else
    echo i is two
endif
# END
 
# BEGIN: if/then/else: string
set s="foo bar"
if ("$s" == "foo bar") then
    echo "foo"
else
    echo "spam"
endif
# END
 
# BEGIN: include a file
#@@ source include-file
# END
 
# BEGIN: set resource limits
#@@ limit
# END
 
# BEGIN: set path variable
set path = ( $path /tmp )
# END
 
# BEGIN: get shell name
echo $shell
# END
 
# BEGIN: get shell version
tcsh --version
echo $version
# END
 
# BEGIN: switch statement
set arg="foo"
switch ("$arg")
    case "foo":
        echo "do foo"
        breaksw
    case "bar":
        echo "do bar"
        breaksw
    case "spam":
        echo "do spam"
        breaksw
    default:
        echo "do be do be do"
        breaksw
endsw
# END
 
# BEGIN: read values from the console
echo -n "login? "
set login = $<
 
echo -n "passwd? "
stty -echo
set passwd = $<
stty echo
 
echo
echo "login: $login"
echo "passwd: $passwd"
# END
 
# BEGIN: prompt
set prompt='%n >'  # user name
set prompt='%j >'  # host
set prompt='! >'   # command num
set prompt='%c3 >' # last 3 dir parts
set prompt='%c >'  # last dir part
set prompt='%~ >'  # curr dir full
#set prompt=N/A    # shell version
set prompt='%n@%j %c (!) >'
# END
 
# BEGIN: regex extension check
set FILES=('f1.tar.bz2' 'f2.tar.gz' 'f3.zip')
foreach FILE ( $FILES )
    echo $FILE | grep '.tar.bz2$' >&/dev/null
    if ( $? == 0 ) then
        echo "tar.bz2: $FILE"
        continue
    endif
 
    echo $FILE | grep '.tar.gz$' >&/dev/null
    if ( $? == 0 ) then
        echo "tar.gz: $FILE"
        continue
    endif
 
    echo $FILE | grep '.zip$' >&/dev/null
    if ( $? == 0 ) then
        echo "zip: $FILE"
        continue
    endif
 
    echo "unk: $FILE"
end
# END
 
# BEGIN: command substitution
# style 1 - can't be nested
set x = `ls -1`
# style 2 - not supported
set r = `readlink -f .`
set me = `dirname $r`
echo $me
# END
 
# BEGIN: local variable
# functions not supported
# END
 
# BEGIN: range iteration
foreach i ( `seq 1000` )
  echo $i
end
# END
 
# BEGIN: setenv/export
setenv FOOBAR "spam"
# END
 
# BEGIN: variable: substitution
set foo = 'bar'
echo $foo:s/bar/baz/
# END
