bash脚本来侦听Shell命令输出 [英] Bash Script To Listen For Shell Command Output

查看:196
本文介绍了bash脚本来侦听Shell命令输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速的问题。我怎么会去有一个shell脚本执行以下操作:

Quick question. How would I go about having a shell script to do the following:


  • 执行UNIX命令(pmset -g PS)来检查脚本每5秒的输出,然后如果该命令的输出下降到低于说40%(例如输出的是:交流电源'从Currenty绘图
    -iBox 100%;充电'),然后为它在UNIX shell脚本...

任何帮助将是非常美联社preciated。

Any help would be much appreciated.

推荐答案

修改,为2.05猛砸的后来的:

Edit, for Bash 2.05 and later:

#!/bin/bash
tab=$'\t'
while true  # run forever, change to stop on some condition
do
    threshold=100
    until (( threshold < 40 ))
    do
        sleep 5
        result=$(pmset -g ps)
        threshold="${result#*iBox$tab}"
        threshold="${threshold%\%*}"
    done
    shell_script
done

原来,为猛砸3.2及更高版本:

#!/bin/bash
pattern='[0-9]+'    # works if there's only one sequence of digits in the output, a more selective pattern is possible if needed
while true  # run forever, change to stop on some condition
do
    threshold=100
    until (( threshold < 40 ))
    do
        sleep 5
        result=$(pmset -g ps)
        [[ $result =~ $pattern ]]
        threshold=${BASH_REMATCH[1]}
    done
    shell_script
done

这篇关于bash脚本来侦听Shell命令输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆