Linux 可以通过管道输入 if 语句吗?

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

Possible to pipe into an if-statement?

linuxbash

提问by Sandra Schlichting

I have a script that outputs about 10 lines every time if it run. The content of these lines varies.

我有一个脚本,每次运行时都会输出大约 10 行。这些行的内容各不相同。

I would really like to be able to grepin the output and do different things depending on the output.

我真的很希望能够grep在输出中根据输出做不同的事情。

In pseudo this is what I would like to do

在伪这是我想做的

cat /etc/password | \
if [ grep "root" $STDOUT ]; then
   echo "root is found"

elif [ grep "nobody" $STDOUT ]; then
   echo "nobody is found"

fi

Here have I used cat /etc/passwordas an example, but it should be replaced with my scripts mentioned above.

这里以我cat /etc/password为例,但应替换为我上面提到的脚本。

The problem is, how do I get hold of the output from cat /etc/passwordin the if/elifconditions?

问题是,如何从得到的输出保持cat /etc/passwordif/elif条件?

采纳答案by sorpigal

As @Benoit recommends, just use grepdirectly.

正如@Benoit 建议的那样,直接使用grep即可。

As @larsmans notes, you can avoid a double-read of the file by reading it into a variable once.

正如@larsmans 所指出的,您可以通过将文件读入变量一次来避免对文件进行双重读取。

Given the availability of bashI'd do it like this:

鉴于bash我会这样做:

password=$(< /etc/passwd)

if grep -q root <<< "$password" ; then
    echo root found
elif grep -q nobody <<< "$password" ; then
    echo nobody found
fi

One read of the file, one or two invocations of grep, no other processes or subshells launched.

一次读取文件,一两次调用grep,没有启动其他进程或子shell。

回答by Benoit

You just do :

你只需要:

if grep -q "root" /etc/passwd ; then
   ...
fi

which will play the ...commands if grep exit code is 0.

...如果 grep 退出代码为 0 ,它将播放命令。

remember that \[is a external command, probably located in /usr/bin/[(normally it's a hard link to testand when invoked as [it requires a matching ]argument). Also see the pitfallspage here, many of them deal are related to that command.

请记住,这\[是一个外部命令,可能位于/usr/bin/[(通常它是一个硬链接,test并且在调用[时需要匹配的]参数)。另请参阅此处的陷阱页面,其中许多与该命令有关。

回答by Fred Foo

Piping into an if-statement is possible with subshells, but that solution will break since you're running two grepcommands on the pipe, the first of which will exhaust it.

使用 subshel​​l 可以将 if 语句插入到 if 语句中,但是该解决方案会中断,因为您在grep管道上运行两个命令,第一个命令会耗尽它。

The best solution in your case is probably to read /etc/passwdinto a variable, then grepit:

在您的情况下,最好的解决方案可能是读/etc/passwd入一个变量,然后grep它:

passwd=$(cat /etc/passwd)
if (echo $passwd | grep -q root); then
     echo "root found"
fi
if (echo $passwd | grep -q nobody); then
     echo "nobody found"
fi

回答by Micha? Kosmulski

I'd suggest using awk:

我建议使用 awk:

cat /etc/passwd | awk '/root/{ do something }/nobody/{ do something else }'

You can achieve the same in bash using an expression like:

您可以在 bash 中使用以下表达式实现相同的效果:

cat /etc/passwd |
while read; do
  if echo "$REPLY" | fgrep root; then
    something
  fi
  if echo "$REPLY" | fgrep nobody; then
    something_else
  fi
done

However the pure bash solution is less efficient for large inputs because it runs separate instances of grepfor every line.

然而,纯 bash 解决方案对于大输入效率较低,因为它grep为每一行运行单独的实例。

回答by tripleee

In the general case, you could use a temporary file.

在一般情况下,您可以使用临时文件。

t=$(mktemp -t passwd.XXXXXXXX)
trap 'rm $t' 0
trap 'exit 127' 1 2 3 5 15
cat >$t
for u in root nobody; do
  fgrep $u $t
done

The traps are to remove the temporary file afterwards.

traps为事后删除临时文件。

As an aside, you canpipe to an if, but the first grepinside your conditional would already consume all of its standard input. It's more useful in situations like this:

顺便说一句,您可以通过管道连接到if,但是grep条件中的第一个已经消耗了它的所有标准输入。在这样的情况下它更有用:

if $option_count ; then
    wc -l
else
    tac
fi <file

回答by user1789538

Just use &&:

只需使用&&:

grep -q root /etc/password && echo "root is found"

grep -q nobody /etc/password && echo "nobody is found"

回答by JMirabile

How about the following:

以下情况如何:

#!/bin/bash

if [ -z  ]; then
   echo Usage: ##代码## [UID to search for]
   exit 1;
fi

SEARCHID=""

function notFound() {
    echo NOT FOUND 
}

function found() {
    echo Found it
}

function main() {

    grep -i $SEARCHID /etc/passwd
    # Move $? to a variable 
    SEARCHRESULT=$?

    if [ "$SEARCHRESULT" != "0" ]; then
       notFound;
    else
       found;
    fi
}

# call main function
main