我正在编写一个 csh shell 脚本,run
用户应该在其中将路径作为$1
变量传递给它。如何确定是否$1
丢失并为其定义默认值?
问问题
2880 次
2 回答
3
This is know to work on tcsh version 6.15.00
#! /bin/csh
if( $#argv == 0 ) then
set filename="(default file)"
else
set filename=$argv[1]
endif
echo "The file is $filename."
NOTE
There is no standard with csh as there is with the POSIX shell; but at least basic if-then-else
and $#argv
should be portable.
See Also
POSIX and Korn Shell Versus Other Shells
Csh Programming Considered Harmful
Bash
于 2010-09-29T20:14:41.677 回答
-1
您可以执行以下操作:
if [ $# -eq 0 ];then
dir='default' # argument not passed use default.
else
dir=$1 # argument passed..use it.
fi
# use $dir
ls $dir
于 2010-09-28T15:38:39.223 回答