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
Bash shell `if` command returns something `then` do something
提问by Billy Moon
I am trying to do an if/then statement, where if there is non-empty output from a ls | grep something
command 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 if
with fi
.
嗯,差不多了,但是你需要if
用fi
.
Also, if
just runs a command and executes the conditional code if the command succeeds (exits with status code 0), which grep
does 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 grep
that 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 ls
also returns nonzero if it doesn't find a specified file, you can do the same thing without the grep
at 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 nullglob
option 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 if
built-in executes a shell command and selects the block based on the return value of the command. ls
returns a distinct status code if it does not find the requested files so there is no need for the grep
part. 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'