【发布时间】:2016-05-28 20:41:43
【问题描述】:
我试图在 tcsh shell 的 IF 语句中使用 OR 条件。相同的语句适用于 CSH。
示例语句:(cat tesh.sh)
if [ "$1" == "hi" -o "$2" == "hello" ];then
echo hi
else
echo hello
fi
现在,如果我执行这个示例脚本,我会收到以下错误:
[35] % sh -x test hi hi hello
+ test hi hi hello
test[7]: hi: A test command parameter is not valid.
+ exit 1
[36] % sh -x test hi hi
+ test hi hi
test[7]: test: Specify a parameter with this command.
+ exit 1
[37] % sh -x test hi hello
+ test hi hello
test[7]: test: Specify a parameter with this command.
+ exit 1
[38] % sh -x test hi hello
+ test hi hello
test[7]: test: Specify a parameter with this command.
+ exit 1
[39] %
请建议可以做什么?
附加信息:
[44] % uname -s
HP-UX
[45] %
[45] % echo $SHELL
/bin/tcsh
[46] %
更多示例:
cat new_test.txt
if ([ $1 == 1 ] || [ $2 == 1 ])
then
echo $1 and $2
fi
./new_test.txt 1 1
./new_test.txt: ==: A test command parameter is not valid.
./new_test.txt: ==: A test command parameter is not valid.
更多示例:
cat suggested.sh
if (($1 == 1) || ($2 == 1)) ; then echo "$1 and $2" ; fi
./suggested.sh 1 1
./suggested.sh: 1: not found.
./suggested.sh: 1: not found.
【问题讨论】:
-
您根本没有在这里使用
tcsh- 这是一件好事,但您的问题具有误导性。代码的语法是sh,您正在使用sh运行脚本。 -
-o不可移植。我会建议一个副本,概述您的选择。 -
(问题标题建议
bash,但一些答案也明确涵盖sh。) -
我这样做只是为了使用调试模式。
标签: shell unix if-statement tcsh