是否可以在'if'语句中管道多个命令?

时间:2022-12-27 20:26:39

Part of a script I'm writing needs to check various text files for the same strings. Before I just had to check one file so I had a long list of strings within cateogies to search for which are defined as variables. Later on in the script the variabled are called and output to the screen if there is a match:

我写的脚本的一部分需要检查相同字符串的各种文本文件。在我不得不检查一个文件之前,我在cateogies中有一长串字符串来搜索哪些被定义为变量。稍后在脚本中调用variabled并在匹配时输出到屏幕:

category_1=$(sudo zcat myfile | egrep -c 'Event 1|Event 2|Event 3')

category_2=$(sudo zcat myfile | egrep -c 'Event 4|Event 5|Event 6')

category_3=$(sudo zcat myfile | egrep -c 'Event 7|Event 8|Event 9')

...

echo Category 1

if [[ $category_1 -ge 2 ]];then

            echo There were $category_1 events

elif [[ $category_1 -eq 1 ]]; then

            echo There was $category_1 event

fi

etc, etc...

等等......

Now I need to change it so that I can check the grepped strings against multiple text files. I've tried to define the new files as variables and pipe them in the if statement with the grep variable to no avail:

现在我需要更改它,以便我可以检查多个文本文件的grepped字符串。我试图将新文件定义为变量,并使用grep变量将它们传递到if语句中,但无济于事:

category_1=$(egrep -c 'Event 1|Event 2|Event 3')

category_2=$(egrep -c 'Event 4|Event 5|Event 6')

category_3=$(egrep -c 'Event 7|Event 8|Event 9')


myfile=$(sudo zcat myfile)

myfile2=$(sudo zcat myfile2)

myfile3=$(sudo zcat myfile3)

...

echo Category 1 - Myfile



if [[ myfile | $category_1 -ge 2 ]];then

            echo There were $category_1 events in myfile

elif [[ myfile | $category_1 -eq 1 ]]; then

            echo There was $category_1 event in myfile

fi

It seems that I can't pipe commands in an if statement.

似乎我无法在if语句中管道命令。

3 个解决方案

#1


13  

Use $(...) to capture output of a command into a string:

使用$(...)将命令的输出捕获到字符串中:

if  [[ $(sudo zcat myfile1 | egrep -c 'Event 1|Event 2|Event 3') -ge 2 ]] ; then

#2


0  

You can use pipes in if, but you mix two things. You use $category_1 with pipe; and $category_1 is not a command. You can use a command substitution instead of the variable.

你可以在if中使用管道,但是你可以混合使用两种东西。你使用带管道的$ category_1;和$ category_1不是命令。您可以使用命令替换而不是变量。

if [[ `sudo zcat myfile | egrep -c 'Event 1|Event 2|Event 3')` -ge 2 ]];then

and so on.

等等。

You can also use variables instead of commands, but it will be a little bit strange.

您也可以使用变量而不是命令,但这有点奇怪。

#3


0  

You could instead use a command such as: if [ "$house" = "nice" ]||[ "$house" = "bad" ] then... to do the job

您可以改为使用如下命令:if [“$ house”=“nice”] || [“$ house”=“bad”]然后......来完成工作

#1


13  

Use $(...) to capture output of a command into a string:

使用$(...)将命令的输出捕获到字符串中:

if  [[ $(sudo zcat myfile1 | egrep -c 'Event 1|Event 2|Event 3') -ge 2 ]] ; then

#2


0  

You can use pipes in if, but you mix two things. You use $category_1 with pipe; and $category_1 is not a command. You can use a command substitution instead of the variable.

你可以在if中使用管道,但是你可以混合使用两种东西。你使用带管道的$ category_1;和$ category_1不是命令。您可以使用命令替换而不是变量。

if [[ `sudo zcat myfile | egrep -c 'Event 1|Event 2|Event 3')` -ge 2 ]];then

and so on.

等等。

You can also use variables instead of commands, but it will be a little bit strange.

您也可以使用变量而不是命令,但这有点奇怪。

#3


0  

You could instead use a command such as: if [ "$house" = "nice" ]||[ "$house" = "bad" ] then... to do the job

您可以改为使用如下命令:if [“$ house”=“nice”] || [“$ house”=“bad”]然后......来完成工作