有一个守护进程,例如,在一个脚本中有5种类型。现在,我希望能够通过指定守护进程的编号(逐个启动)或指定"all"(批量启动)来启动/停止它
格式:(runscript)(commandName)(守护进程#或"all")
需要满足两个条件,当用户输入:
(1)正确(通过数字或"全部")或
(2) 错误地(输入的num大于$count或所有其他字符串都大于"all")
如果用户输入其他字符串而不是"All",则除一个条件外,所有条件都已实现
样本代码:
case 'startDaemon': #commandName
set count = 5
if ($#argv == 2 && $2 == all) then
echo "correct, do this"
else if ($#argv == 2 && $2 < $count) then
echo "correct too, do this"
else if ($#argv == 2 && ($2 != all || $2 >= $count)) then
echo "Incorrect parameter: specify daemon # less than $count or 'all' to start all."
else
echo "Please use: $0(runscript) $1(commandname) (daemon # or all)"
每当我键入:(runscript)startDaemon hello时,例如,错误显示:
if: Expression syntax
当它应该进入第三种状态时。请帮助并指出问题是否在条件或逻辑运算符或其他方面。提前感谢
PS。我使用csh。给我的剧本是csh,所以是的。
直接的问题是比较$2 < $count
,当$count
包含字符串时,该比较无效。
这是一个有效的解决方案:
#!/bin/csh
set count = 5
if ($#argv == 2) then
if ($2 == all) then
echo "correct, do this"
else if (`echo $2 | grep '^[0-9]*$'`) then
if ($2 < $count) then
echo "correct too, do this"
else
echo "Number must be less than 5"
endif
else
echo "Incorrect parameter: specify daemon # less than $count or 'all' to start all."
endif
else
echo "Please use: $0(runscript) $1(commandname) (daemon # or all)"
endif