尾巴-Fn0和变量 [英] Tail -Fn0 and variable

查看:87
本文介绍了尾巴-Fn0和变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此后继错误的尾部语法或grep命令?,但是我正在读取给定条件下的实时日志条目,并在满足条件时继续执行脚本的其余部分.我正在使用这个:

This follows on from Faulty tail syntax or grep command? but I'm reading a live log entries for given conditions and when they're met continuing the execution of the rest of the script. I'm using this:

tail -Fn0 /var/log/messages | grep -q "CPU utilization" | grep -q "exceeded threshold"  
FPC=$(echo $line | awk 'END { print substr($8,1,1) }')  
PIC=$(echo $line | awk 'END { print substr($11,1,1) }')  
echo FPC $FPC  
echo PIC $PIC  
echo "Running information gathering"...and rest of script.  

这对于条件检测和进一步执行非常有效,但是我没有用于测试FPC和PIC变量的日志条目.我试过这样包装尾部语句:

Which works perfectly for the conditions detection and further execution, but I don't have the log entry to test for the FPC and PIC variables. I've tried wrapping the tail statement thus:

line=$(tail -Fn0 /var/log/messages | grep -q "CPU utilization" | grep -q "exceeded threshold")  

但是grep -q静默退出,并且$ line变量为空.我已经尝试过:

but grep -q exits silently and the $line variable is blank. I've tried:

line=$(tail -Fn0 /var/log/messages | grep -m1 "CPU utilization" | grep -m1 "exceeded threshold")  

,直到我尝试将CONTROL-C移出脚本后,该命令才起作用.然后它可以正常工作,并且可以完美持续.有人可以帮忙吗?

which doesn't work until I attempt to CONTROL-C out of the script. Then it works fine and continues perfectly. Can someone help please?

我稍后在脚本中需要变量FPC和PIC.

I need the variables FPC and PIC later in the script.

推荐答案

假设以后不再需要这些变量,则可以执行以下操作:

Assuming that you don't need these variables later on, you could do something like this:

tail -Fn0 /var/log/messages | \
awk '/CPU utilization/ && /exceeded threshold/ { 
     print "FPC", substr($8,1,1); print "PIC", substr($11,1,1); exit }'

当该行与两种模式都匹配时,请打印出您感兴趣的两个部分并退出.

When the line matches both patterns, print the two parts of it that you are interested in and exit.

如果确实需要变量,则可以改成这样:

If you do need the variables, you could do something like this instead:

line=$(tail -Fn0 /var/log/messages | awk '/CPU utilization/&&/exceeded threshold/{print;exit}')
FPC=$(echo "$line" | awk '{ print substr($8,1,1) }')
PIC=$(echo "$line" | awk '{ print substr($11,1,1) }')

这篇关于尾巴-Fn0和变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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