快速提问。我将如何使用shell脚本执行以下操作:
非常感谢任何帮助。
答案 0 :(得分:1)
编辑,对于Bash 2.05 以及:
#!/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
原创,适用于Bash 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
答案 1 :(得分:0)
这样的事情会起作用
pmset -g ps | perl -pe 'if(/%.*Ibox ([0-9]+)%; ch.*$/ and $1 < 40){system "nameofshellscript"}'