2

Why is it when I leave the following tailf running:

tailf /var/log/z-way-server.log | grep --line-buffered device-info | gawk 'BEGIN { FS = "\"" } ; {print $4,"is",$8}'

I get my desired output:

Den Window Sensor is off

However, when I want this passed as a parameter to a script (which uses $1 within the script):

./message.sh $(tailf /var/log/z-way-server.log | grep --line-buffered device-info | gawk 'BEGIN { FS = "\"" } ; {print $4,"is",$8}')

It never passes the message as desired? For clarification, message.sh Test would post the word "Test" to a Slack channel.

1

The problem is that your message.sh command is never started. The shell first needs to evaluate the $(...) command in order to pass the result as arguments, but you are using tailf which never ends. Compare with this:

ls $(echo hello;sleep 10) & sleep 1; ps

The ps will show you that ls has not been started, and will only get the hello arg when the sleep 10 finishes and the $() part has finished.

0

try to pass with double quotes. and if your OS supports timeout command then try with timeout (refer the below post)

https://stackoverflow.com/questions/10430126/how-to-stop-tail-f-command-executed-in-sub-shell

if you don't want timeout, then try like this

#!/bin/bash

tailf /var/log/z-way-server.log | grep --line-buffered device-info | gawk 'BEGIN { FS = "\"" } ; {print $4,"is",$8}' | while read line
do
    /absolute/path/of/message.sh "${line}"
done
5
  • I tried with double quotes, didn't change any symptoms. I don't think I want to do the timeout since I want this to run continuously.
    – armani
    Nov 23 '16 at 7:29
  • updated the answer. check now... stackoverflow.com/questions/12079626/…
    – Kamaraj
    Nov 23 '16 at 7:42
  • Thanks for the help but I'm getting no output at all from this.
    – armani
    Nov 23 '16 at 7:49
  • Would it help to know that message.sh is a single 'curl' command? I've been trying to combine commands now but having no luck. What do you think - is that a better route to pursue?
    – armani
    Nov 23 '16 at 7:50
  • is your command produce the output or not ? tailf /var/log/z-way-server.log | grep --line-buffered device-info | gawk 'BEGIN { FS = "\"" } ; {print $4,"is",$8}'
    – Kamaraj
    Nov 23 '16 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.