如何在 tcsh if 语句中使用 OR 条件
How to use OR condition inside tcsh if statement
我正在尝试在 tcsh shell 的 IF 语句中使用 OR 条件。同样的声明适用于 CSH。
示例语句:(cat tesh.sh)
if [ "" == "hi" -o "" == "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 ])
then
echo and
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)) ; then echo " and " ; fi
./suggested.sh 1 1
./suggested.sh: 1: not found.
./suggested.sh: 1: not found.
有一些困惑:
- 你的第一个脚本是用bourne写的-shell
- 你的脚本没有提到 shell 应该用什么来解释它(这可能会导致一些混乱,具体取决于你执行它的方式)
- 您的第二个脚本是一个 tcsh 脚本,但具有误导性的语法
- 看来你不知道
[
是什么。
像这样修改第一个脚本:
#!/bin/sh
if [ "" == "hi" -o "" == "hello" ]; then
echo hi
else
echo hello
fi
这意味着如果可执行此脚本将使用 /bin/sh
来解释它。但是,如果您使用命令强制 shell:
% sh -x test hi hi
+ '[' hi == hi -o hi == hello ']'
+ echo hi
hi
% tcsh -x test hi hi
if [ hi = hi -o hi = hello ]
if: Expression Syntax.
then
then: Command not found.
%
您可以观察到使用错误的 shell 会导致一些语法错误。
如果你想写一个tcsh脚本,这是一个解决方案:
#!/bin/tcsh
if ( "" == "hi" || "" == "hello" ) then
echo hi
else
echo hello
endif
if
的语法不同,因为 tcsh 具有内部测试功能,而标准 bourne shell 没有。在 bourne shell 中,使用别名 [
的外部命令 test
进行测试。因此,您可以阅读手册中有关 test
syntax 的文档:
SYNOPSIS
test expression
[ expression ] ...
s1 = s2 True if the strings s1 and s2 are identical. ...
expression1 -o expression2
True if either expression1 or expression2 are true.
对于 tcsh 的 if
,请阅读 tcsh(或其父 csh)的手册:
if (expr) command
If expr (an expression, as described under Expressions) evalu-
ates true, then command is executed. Variable substitution on
command happens early, at the same time it does for the rest of
the if command. command must be a simple command, not an
alias, a pipeline, a command list or a parenthesized command
list, but it may have arguments. Input/output redirection
occurs even if expr is false and command is thus not executed;
this is a bug.
if (expr) then
...
else if (expr2) then
...
else
...
endif If the specified expr is true then the commands to the first
else are executed; otherwise if expr2 is true then the commands
to the second else are executed, etc. Any number of else-if
pairs are possible; only one endif is needed. The else part is
likewise optional. (The words else and endif must appear at
the beginning of input lines; the if must appear alone on its
input line or after an else.)
Logical, arithmetical and comparison operators
These operators are similar to those of C and have the same precedence.
They include
|| && | ^ & == != =~ !~ <= >=
< > << >> + - * / % ! ~ ( )
...
我正在尝试在 tcsh shell 的 IF 语句中使用 OR 条件。同样的声明适用于 CSH。
示例语句:(cat tesh.sh)
if [ "" == "hi" -o "" == "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 ])
then
echo and
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)) ; then echo " and " ; fi
./suggested.sh 1 1
./suggested.sh: 1: not found.
./suggested.sh: 1: not found.
有一些困惑:
- 你的第一个脚本是用bourne写的-shell
- 你的脚本没有提到 shell 应该用什么来解释它(这可能会导致一些混乱,具体取决于你执行它的方式)
- 您的第二个脚本是一个 tcsh 脚本,但具有误导性的语法
- 看来你不知道
[
是什么。
像这样修改第一个脚本:
#!/bin/sh
if [ "" == "hi" -o "" == "hello" ]; then
echo hi
else
echo hello
fi
这意味着如果可执行此脚本将使用 /bin/sh
来解释它。但是,如果您使用命令强制 shell:
% sh -x test hi hi
+ '[' hi == hi -o hi == hello ']'
+ echo hi
hi
% tcsh -x test hi hi
if [ hi = hi -o hi = hello ]
if: Expression Syntax.
then
then: Command not found.
%
您可以观察到使用错误的 shell 会导致一些语法错误。
如果你想写一个tcsh脚本,这是一个解决方案:
#!/bin/tcsh
if ( "" == "hi" || "" == "hello" ) then
echo hi
else
echo hello
endif
if
的语法不同,因为 tcsh 具有内部测试功能,而标准 bourne shell 没有。在 bourne shell 中,使用别名 [
的外部命令 test
进行测试。因此,您可以阅读手册中有关 test
syntax 的文档:
SYNOPSIS
test expression
[ expression ] ...
s1 = s2 True if the strings s1 and s2 are identical. ...
expression1 -o expression2
True if either expression1 or expression2 are true.
对于 tcsh 的 if
,请阅读 tcsh(或其父 csh)的手册:
if (expr) command
If expr (an expression, as described under Expressions) evalu-
ates true, then command is executed. Variable substitution on
command happens early, at the same time it does for the rest of
the if command. command must be a simple command, not an
alias, a pipeline, a command list or a parenthesized command
list, but it may have arguments. Input/output redirection
occurs even if expr is false and command is thus not executed;
this is a bug.
if (expr) then
...
else if (expr2) then
...
else
...
endif If the specified expr is true then the commands to the first
else are executed; otherwise if expr2 is true then the commands
to the second else are executed, etc. Any number of else-if
pairs are possible; only one endif is needed. The else part is
likewise optional. (The words else and endif must appear at
the beginning of input lines; the if must appear alone on its
input line or after an else.)
Logical, arithmetical and comparison operators
These operators are similar to those of C and have the same precedence.
They include
|| && | ^ & == != =~ !~ <= >=
< > << >> + - * / % ! ~ ( )
...