我想在 bash 中 grep ifconfig 的输出

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

I'd like to grep the output of ifconfig in bash

bashshellgrepifconfig

提问by Red Cricket

I'm making a small script to determine if I have an internet connection on OSX. More of just practice, I suppose.

我正在制作一个小脚本来确定我是否在 OSX 上有互联网连接。更多的只是练习,我想。

In terminal "ifconfig | grep -cs 'status: active' " will return 1 if there's at least one active connection

在终端 "ifconfig | grep -cs 'status: active' " 如果至少有一个活动连接,将返回 1

The script I have is this

我的脚本是这样的

    #!/bin/bash

detect(){
ONLINE=ifconfig | grep -cs 'status: active'
}

if [[ detect = 1 ]]
then
        echo "Online"
else
        echo "Offline"
fi

However the Variable ONLINE always returns 0. From what I can tell/understand, this has to do with using a pipe inside of the script. A sub-pipe is used when running the command, and ONLINE just gets stuck with 0 as the sub-pipe closes.

然而,变量 ONLINE 总是返回 0。据我所知/理解,这与在脚本内部使用管道有关。运行命令时使用子管道,当子管道关闭时,ONLINE 只会卡在 0 上。

I think I see the issue, but I don't know how to get around this. I saw a bunch of work arounds for scripts having this issue with while loops, but nothing where I need the output from ifconfig fed into grep.

我想我看到了这个问题,但我不知道如何解决这个问题。我看到了一堆解决方法,用于解决 while 循环存在此问题的脚本,但在我需要将 ifconfig 的输出输入 grep 的情况下,没有任何解决方法。

回答by Floris

Several problems with your current script:

您当前脚本的几个问题:

  1. You set a variable ONLINE, but you test for detect.
  2. You don't actually assign the result of the ifconfig | grep -cs 'status: active'command to the variable ONLINE
  3. You use =instead of ==to test for equality
  1. 您设置了一个变量ONLINE,但您测试了detect.
  2. 您实际上并未将ifconfig | grep -cs 'status: active'命令的结果分配给变量ONLINE
  3. 您使用=而不是==来测试相等性

The following would seem to be closer to what you intended:

以下似乎更接近您的意图:

#!/bin/bash
ONLINE=$(ifconfig | grep -cs 'status: active')
if [[ $ONLINE == 1 ]]
then
  echo "online"
else
  echo "offline"
fi

回答by Soosh

use this:

用这个:

ONLINE=$(ifconfig | grep -cs 'status: active')


cause without "$" what bash will return is the result of the command being successful or not and if it is successful it is always zero.


由于没有“$”,bash 将返回的结果是命令成功与否的结果,如果成功,则它始终为零。

回答by Red Cricket

Or you can just keep it simple like this ...

或者你可以像这样保持简单......

ifconfig | grep 'status: active' > /dev/null 2>&1
if [ $? == 0 ]
then
   echo "online"
else
   echo "offline"
fi

回答by Samveen

As none of the answers explain the exact issue with your script, I'm adding an answer.

由于没有一个答案能解释您的脚本的确切问题,我正在添加一个答案。

The issue lies with the line ONLINE=ifconfig | grep -cs 'status: active'.

问题在于线路ONLINE=ifconfig | grep -cs 'status: active'

What is wrong is that there is no command substitution ($(...)or `...`) used in the line. As correctly suggested by other answers as well, the assignment needs to be $(ONLINE=ifconfig | grep -cs 'status: active').

错误的是该行中没有使用命令替换($(...)`...`)。正如其他答案所正确建议的那样,作业需要是$(ONLINE=ifconfig | grep -cs 'status: active').

What this line actually does is that it assigns the string "ifconfig"to the variable ONLINEand pipes the output of that (no output in this case) through grep -cs ...

这一行实际上做的是将字符串分配"ifconfig"给变量ONLINE并将其输出(在这种情况下没有输出)通过grep -cs ...

One point to note is that this assignment is only for the duration of that line, and does not survive till the next line. To illustrate:

需要注意的一点是,此分配仅适用于该行的持续时间,并且不会持续到下一行。为了显示:

samveen@precise:~$ I=0
samveen@precise:~$ echo $I
0
samveen@precise:~$ I=1 | echo "blank"
blank
samveen@precise:~$ echo $I
0

Edit:I totally missed another very important point: Subroutines do not return values in bash, just exit status

编辑:我完全错过了另一个非常重要的点:Subroutines do not return values in bash, just exit status

Thus capturing variables from subroutine calls needs the subroutine to echo it's expected return value and it's call needs an assignment with command substitution.

因此从子例程调用中捕获变量需要子例程来回显它的预期返回值,并且它的调用需要一个带有命令替换的赋值。

Something like this:

像这样的东西:

detect(){
    ONLINE=$(ifconfig | grep -cs 'status: active')
    echo $ONLINE
}

if [[ $(detect) -eq 1 ]]
then
    echo "Online"
else
    echo "Offline"
fi

Also, use -eqto test numeric equality.

此外,用于-eq测试数字相等性。

Finally, the shortest way to do what you want is

最后,做你想做的事情的最短方法是

ifconfig | grep -q 'status:active' && echo "online" || echo "offline"