简体   繁体   English

tail没有在bash脚本中提供输出

[英]tail not providing output in bash script

i have written a bash script that will filter 'tail' output, which the entire command 我写了一个bash脚本,它将过滤'tail'输出,这是整个命令

 tail -f /var/log/asterisk/messages | awk 'match($12, /[^0-9]91([0-9]{10})@default/, a) {print a[1]}'

works fine from the CLI but not when placed in the bash script: 在CLI中工作正常但在放入bash脚本时没有:

 #!/bin/bash

 phonenumber=$(tail -f /var/log/asterisk/messages | awk 'match($12, /[^0-9]91([0-9]{10})@default/, a) {print a[1]}')
 echo "$phonenumber >> test.log"

which doesn't output anything, (2135551234, is the expected output string) i have tried writing to the log file and writing just the stdout but neither work. 它没有输出任何东西,(2135551234,是预期的输出字符串)我已经尝试写入日志文件并只编写标准输出但不起作用。

i have tried the script using 'cat' instead of 'tail' and that works fine. 我已经尝试使用'cat'而不是'tail'来编写脚本,并且工作正常。 but i dont want to dump the output of the entire file, hence the use of 'tail'. 但我不想转储整个文件的输出,因此使用'尾'。

I have also tried using 'tee' but to no avail 我也试过使用'tee',但无济于事

the end goal of this script with be to send the phone number as it comes into the PBX to a serial device to another system and used as the CID. 此脚本的最终目标是将电话号码发送到集团设备到另一个系统并用作CID时进入集团电话。

thanks for all your help in advance 感谢您的所有帮助

Try this: 试试这个:

phonenumber=$(tail -f /var/log/asterisk/messages | awk 'match($12, /[^0-9]91([0-9]{10})@default/, a) {print a[1]; exit}')

Your version doesn't work because tail -f and awk are in an infinite loop. 您的版本不起作用,因为tail -fawk处于无限循环中。 Adding exit to the awk script terminates the loop when the first phone number is found. 在找到第一个电话号码时,向awk脚本添加exit将终止循环。 awk exits immediately and its output is put into the variable, and tail -f gets a SIGPIPE signal when it tries to write the next line to the pipe, which causes it to exit. awk立即退出并将其输出放入变量中, tail -f在尝试将下一行写入管道时获取SIGPIPE信号,这会导致它退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM