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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:01:23  来源:igfitidea点击:

One line if statement in bash

bashif-statement

提问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 resultfrom 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 ltrather than <, since <is a redirection operator. (Unless you intend to run a command named $tmpwith input from a file named by the variable $result)

此外,您需要使用lt而不是<,因为<它是重定向运算符。(除非您打算$tmp从变量命名的文件中运行以输入命名的命令$result

回答by hek2mgl

You are missing a semicolon and need to use -ltinstead of <, as others pointed out.

正如其他人指出的那样,您缺少分号,需要使用-lt而不是<

An alternative to the ifstatement 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 ifneeds to be followed by a ficommand, but you don't have any such command. You have a fiin your code, but it's in the middle of another command, so it no more completes the ifthen the fiin echo fiwould. If you're going to merge lines together, you need to use a semi-colon to separate the commands.

if需要跟随一个fi命令,但您没有任何这样的命令。您fi的代码中有 a ,但它位于另一个命令的中间,因此它不再完成if然后fiin 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: doand thencan 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([]), -ltis used to compare numbers.

  • 例外:doandthen后面可以跟一个命令,所以当你在下一行合并时,它们后面不需要分号。

  • 请注意您如何不需要用;? ;仅在命令之间需要。

  • test( []) 中,-lt用于比较数字。