shell脚本中的if语句出错
我正在尝试执行以下shell脚本shell脚本中的if语句出错,shell,csh,Shell,Csh,我正在尝试执行以下shell脚本 #!/bin/csh if [[ $# != 1 || $1 != "first" && $1 != "second" ]] then echo "Error: Usage: $0 [first|second]" exit 1 fi 但我得到了一个错误: if:表达式语法 我不知道这个语法有什么问题。我觉得很好。请帮助。如果您使用C-shell,您应该编写 #!/bin/csh if( $# != 1 || $1 !=
#!/bin/csh
if [[ $# != 1 || $1 != "first" && $1 != "second" ]]
then
echo "Error: Usage: $0 [first|second]"
exit 1
fi
但我得到了一个错误:
if:表达式语法
我不知道这个语法有什么问题。我觉得很好。请帮助。如果您使用C-shell,您应该编写
#!/bin/csh
if( $# != 1 || $1 != "first" && $1 != "second" ) then
echo "Error: Usage: $0 [first|second]"
exit 1
endif
您的版本适用于类似Bourne的shell,如sh
,bash
等。C shell(csh)没有变量$\code>。事实上,在csh中,传递、if
语句等的参数与ksh或bash有根本不同。所有代码看起来都像bash代码,但shebang行包含csh
。因此,如果要使用bash
,请将其更改为:
#!/bin/bash
if [[ $# != 1 || $1 != "first" && $1 != "second" ]]
then
echo "Error: Usage: $0 [first|second]"
exit 1
fi
或者,如果您确实想使用csh
,则必须将代码重新写入:
#!/bin/csh
if ( $#argv != 1 || $1 != "first" && $1 != "second" ) then
echo "Error: Usage: $0 [first|second]"
exit 1
endif