Delayed evaluation of csh variables?
Harald.Eikrem at elab-runit.sintef.no
Harald.Eikrem at elab-runit.sintef.no
Thu Dec 13 13:17:10 AEST 1990
I believe there are cases when csh outperforms Bourne shell, e.g. when you
are on a slow system and need access to expression evaluation, exemplified by:
=========
#!/bin/csh -f
# pickcolor - pick an X application background color semirandomly
if ( $#argv == 0 ) then
set argv=( BlanchedAlmond Bisque NavajoWhite YellowGreen \
BlanchedAlmond Honeydew2 Tan LightSalmon LavenderBlush \
MistyRose Moccasin LightSteelBlue NavajoWhite PaleTurquoise \
BlanchedAlmond Khaki PaleGoldenrod Lavender Tan Salmon \
Orchid Seashell3 PaleVioletRed Thistle NavajoWhite Wheat )
endif
set pick=1
if ( $#argv > 1 ) then
/usr/games/random -e $#argv
@ pick = $status + 1
endif
echo $argv[$pick]
=========
For a different case, save the following as ~/csh/path and then
alias path source ~/csh/path "'\!*'"
then you have a simple csh $path manipulator.
I like to see this done for ksh/bash...... --Harald E
=========
# csh search path management procedure
# - invoke through alias path source <thisdirectory>path "'\!*'"
#
if ( ! $?path ) set path = ""
if ( "$path" == '' && '!*' !~ [./~]* && '!*' !~ \$?* ) then
set path = (. ~/bin /usr/local/bin /usr/ucb /bin /usr/bin)
echo 'No path set; using default settings.\
'$path
else if ( '!*' == '' ) then
echo $path
else if ( '!*' == '-' || '!*' == '-$' ) then
if ( $#path == 1 ) then
echo 'Only one path element left, will not unset $path'
else
@ n__ = $#path - 1
set path = ($path[-$n__])
echo $path
endif
unset n__
else if ( '!*' == '-^' ) then
if ( $#path == 1 ) then
echo 'Only one path element left, will not unset $path'
else
set path = ($path[2*])
echo $path
endif
unset n__
else if ( '!*' =~ \-[1-9] || '!*' =~ \-[1-9][0-9] ) then
@ arg__ = - !^
if ( $arg__ > $#path ) then
if ( $#path == 1 ) then
echo 'There is currently only '$#path' element in $path'
else
echo 'There are currently '$#path' elements in $path'
endif
else if ( $#path == 1 ) then
echo 'Only one path element left, will not unset $path'
else if ( $arg__ == $#path ) then
@ n__ = $arg__ - 1
set path = ($path[-$n__])
echo $path
else
@ n__ = $arg__ - 1
@ r__ = $arg__ + 1
set path = ($path[-$n__] $path[$r__-])
echo $path
endif
unset arg__ n__ r__
else if ( '!*' =~ [1-9^]\ [./~]* || '!*' =~ [1-9][0-9]\ [./~]* || \
'!*' =~ [1-9^]\ \$[_a-zA-Z0-9]* || \
'!*' =~ [1-9][0-9]\ \$[_a-zA-Z0-9]* ) then
if ( !^ > $#path + 1 ) then
if ( $#path == 1 ) then
echo 'There is currently only '$#path' element in $path'
else
echo 'There are currently '$#path' elements in $path'
endif
else if ( !^ <= 1 || !^ == ^ ) then
set path = (!:2* $path)
echo $path
else if ( !^ > $#path ) then
set path = ($path !:2*)
echo $path
else
@ r__ = !^ - 1
set path = ($path[-$r__] !:2* $path[!{^}*])
echo $path
endif
unset arg__ n__ r__
else if ( '!*' =~ [./~]* || '!*' =~ \$[_a-zA-Z0-9]* ) then
set path = ($path !*)
echo $path
else echo 'Usage: path { -^ | -$ | - | -<n> | <n> pathname(s) | pathname(s) }\
meaning:\
-^ strip first element from $path.\
-$ or - strip last element.\
-<n> strip element number <n>, numbering from 1 up.\
<n> pathname(s) insert pathname(s) before element <n>, where\
<n> can be ^ if before first element.\
pathname(s) add pathname(s) beyond last element.\
\
A valid pathname should start with [./~], or be a shell variable constituting\
one or more valid pathnames.'
endif
More information about the Comp.unix.shell
mailing list