Linux Bash shell `if` 命令返回一些东西 `then` 做一些事情

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10372626/
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-08-06 06:04:18  来源:igfitidea点击:

Bash shell `if` command returns something `then` do something

linuxbashif-statement

提问by Billy Moon

I am trying to do an if/then statement, where if there is non-empty output from a ls | grep somethingcommand then I want to execute some statements. I am do not know the syntax I should be using. I have tried several variations of this:

我正在尝试执行 if/then 语句,如果ls | grep something命令的输出为非空,那么我想执行一些语句。我不知道我应该使用的语法。我已经尝试了以下几种变体:

if [[ `ls | grep log ` ]]; then echo "there are files of type log";

采纳答案by Mark Reed

Well, that's close, but you need to finish the ifwith fi.

嗯,差不多了,但是你需要iffi.

Also, ifjust runs a command and executes the conditional code if the command succeeds (exits with status code 0), which grepdoes only if it finds at least one match. So you don't need to check the output:

此外,if如果命令成功(以状态代码 0 退出),grep则只运行命令并执行条件代码,只有在找到至少一个匹配项时才会执行。所以你不需要检查输出:

if ls | grep -q log; then echo "there are files of type log"; fi

If you're on a system with an older or non-GNU version of grepthat doesn't support the -q("quiet") option, you can achieve the same result by redirecting its output to /dev/null:

如果您使用的系统较旧或非 GNU 版本grep不支持-q("quiet") 选项,您可以通过将其输出重定向到/dev/null

if ls | grep log >/dev/null; then echo "there are files of type log"; fi

But since lsalso returns nonzero if it doesn't find a specified file, you can do the same thing without the grepat all, as in D.Shawley's answer:

但是因为ls如果没有找到指定的文件也会返回非零,所以你可以在没有的情况下做同样的事情grep,如 D.Shawley 的回答:

if ls *log* >&/dev/null; then echo "there are files of type log"; fi

You also can do it using only the shell, without even ls, though it's a bit wordier:

您也可以仅使用 shell 来完成它,而没有 even ls,尽管它有点冗长:

for f in *log*; do 
  # even if there are no matching files, the body of this loop will run once
  # with $f set to the literal string "*log*", so make sure there's really
  # a file there:
  if [ -e "$f" ]; then 
    echo "there are files of type log"
    break
  fi
done 

As long as you're using bash specifically, you can set the nullgloboption to simplify that somewhat:

只要您专门使用 bash,您就可以设置nullglob选项以稍微简化:

shopt -s nullglob
for f in *log*; do
  echo "There are files of type log"
  break
done

回答by D.Shawley

The ifbuilt-in executes a shell command and selects the block based on the return value of the command. lsreturns a distinct status code if it does not find the requested files so there is no need for the greppart. The [[utilityis actually a built-in command from bash, IIRC, that performs arithmetic operations. I could be wrong on that part since I rarely stray far from Bourne shell syntax.

所述if内置的执行外壳命令并且基于该命令的返回值的块。 ls如果找不到请求的文件,则返回一个不同的状态代码,因此不需要该grep部件。该[[实用程序实际上是来自 bash 的内置命令 IIRC,用于执行算术运算。我在这方面可能是错的,因为我很少偏离 Bourne shell 语法。

Anyway, if you put all of this together, then you end up with the following command:

无论如何,如果你把所有这些放在一起,那么你最终会得到以下命令:

if ls *log* > /dev/null 2>&1
then
    echo "there are files of type log"
fi

回答by Alex

Or without if; then; fi:

或者没有if; then; fi

ls | grep -q log && echo 'there are files of type log'

Or even:

甚至:

ls *log* &>/dev/null && echo 'there are files of type log'