bash 中的一行 if 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31118780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
One line if statement in bash
提问by RagnaRock
I've never programed in bash... yet I'm trying to solve a problem for an anchievement in a game (codingame.com)
我从来没有用 bash 编程过......但我正在尝试解决游戏中的成就问题(codingame.com)
I have the following code:
我有以下代码:
for (( i=0; i<N-1; i++ )); do
tmp=$(( sorted_array[i+1] - sorted_array[i] ));
if [ $tmp < $result ]; then result=$tmp fi
done
And this error:
而这个错误:
/tmp/Answer.sh: line 42: syntax error near unexpected token `done'at Answer.sh. on line 42
/tmp/Answer.sh: line 42: `done' at Answer.sh. on line 42
I want to compare adjacent values of my array and store the minimun diference between them... but I cant figure how to do an If statement in bash
我想比较数组的相邻值并存储它们之间的最小差异......但我无法弄清楚如何在 bash 中执行 If 语句
回答by William Pursell
Each command must be properly terminated, either by a newline or a semi-colon. In this case, you need to separate the assignment of result
from the keyword fi
. Try adding a semi-colon;
每个命令都必须以换行符或分号正确终止。在这种情况下,您需要将 的分配result
与关键字分开fi
。尝试添加分号;
for (( i=0; i<N-1; i++ )); do
tmp=$(( sorted_array[i+1] - sorted_array[i] ))
if [ "$tmp" -lt "$result" ]; then result=$tmp; fi
done
Also, you need to use lt
rather than <
, since <
is a redirection operator. (Unless you intend to run a command named $tmp
with input from a file named by the variable $result
)
此外,您需要使用lt
而不是<
,因为<
它是重定向运算符。(除非您打算$tmp
从变量命名的文件中运行以输入命名的命令$result
)
回答by hek2mgl
You are missing a semicolon and need to use -lt
instead of <
, as others pointed out.
正如其他人指出的那样,您缺少分号,需要使用-lt
而不是<
。
An alternative to the if
statement would be to use the logical andoperator &&
:
该if
语句的替代方法是使用逻辑和运算符&&
:
for (( i=0; i<N-1; i++ )); do
tmp=$(( sorted_array[i+1] - sorted_array[i] ))
[ $tmp -lt $result ] && result=$tmp
done
回答by ikegami
Your if
needs to be followed by a fi
command, but you don't have any such command. You have a fi
in your code, but it's in the middle of another command, so it no more completes the if
then the fi
in echo fi
would. If you're going to merge lines together, you need to use a semi-colon to separate the commands.
您if
需要跟随一个fi
命令,但您没有任何这样的命令。您fi
的代码中有 a ,但它位于另一个命令的中间,因此它不再完成if
然后fi
in echo fi
。如果要将行合并在一起,则需要使用分号来分隔命令。
So to collapse
所以要崩溃
for (( i=0; i<N-1; i++ ))
do
tmp=$(( sorted_array[i+1] - sorted_array[i] ))
if [ $tmp -lt $result ]
then
result=$tmp
fi
done
you'd use
你会用
for (( i=0; i<N-1; i++ )); do
tmp=$(( sorted_array[i+1] - sorted_array[i] ))
if [ $tmp -lt $result ]; then result=$tmp; fi
done
Exception:
do
andthen
can be followed by a command, so you don't need a semi-colon after them when you merge in the next line.Notice how you don't need to terminate your commands with
;
?;
is only needed between commands.Inside of a
test
([]
),-lt
is used to compare numbers.
例外:
do
andthen
后面可以跟一个命令,所以当你在下一行合并时,它们后面不需要分号。请注意您如何不需要用
;
?;
仅在命令之间需要。在
test
([]
) 中,-lt
用于比较数字。