bash 脚本根据标准输出消息发出哔哔声

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

bash script beep depending on stdout message

bashstdoutbeep

提问by sylvian

Never coded on bash but need something urgent. Sorry if this is not the norm but would really like to get some help.

从来没有在 bash 上编码,但需要一些紧急的东西。对不起,如果这不是常态,但真的很想得到一些帮助。

I have some messages that are thrown to stdout, Depending on the message type (the message is a string with the word "found") I need the bash script to beep.

我有一些消息被抛出到标准输出,根据消息类型(消息是一个带有“找到”字样的字符串),我需要 bash 脚本发出哔哔声。

So far I've come up with this.

到目前为止,我已经想出了这个。

  output=$(command 1) # getting stdout stream?
  while [ true ]; do
    if [ "$output" = "found" ]; then # if the stdout has the word "found" 
     echo  $(echo -e '\a') # this makes the beep sound
    fi
  done

I'm not sure where/how to add grepor awkcommand to check for the string that has the word "found" and only return "found" so that in the ifcondition it can check against that word.

我不确定在哪里/如何添加grepawk命令来检查具有“找到”一词的字符串,并且只返回“找到”,以便在if条件下可以检查该词。

Thanks!

谢谢!

回答by cmbuckley

You can do something as simple as:

你可以做一些简单的事情:

command | grep -q 'found' && echo -e '\a'

If the output of commandcontains the text "found", then grepwill return with a zero exit status, so the echocommand will be executed, causing the beep.

如果 的输出command包含文本“找到”,grep则将返回零退出状态,因此echo将执行该命令,导致发出哔哔声。

If the output does not contain "found", grepwill exit with status 1, and will not result in the echo.

如果输出不包含“found”,grep将以状态 1 退出,并且不会导致echo.

Depending on what you need to make the beep work, just replace anything after the &&. The general syntax would be something like:

根据使蜂鸣器正常工作所需的内容,只需替换&&. 一般语法类似于:

command | grep -q "$SEARCH" && command_if_found || command_if_not_found