linux - tail not providing output in bash script -


i have written bash script filter 'tail' output, entire command

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

works fine cli not when placed in bash script:

 #!/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, expected output string) have tried writing log file , writing stdout neither work.

i have tried script using 'cat' instead of 'tail' , works fine. dont want dump output of entire file, hence use of 'tail'.

i have tried using 'tee' no avail

the end goal of script send phone number comes pbx serial device system , used cid.

thanks 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 , awk in infinite loop. adding exit awk script terminates loop when first phone number found. awk exits , output put variable, , tail -f gets sigpipe signal when tries write next line pipe, causes exit.


Comments