0

I'm using sed to add the date at the beginning of every lines given by a command (which use vlc and never stops...). I found this way on another stackexchange post and that works as expected.

command | sed "s/^/$(date +%Z-%Y-%m-%d-%T) /" &>> $LOGFILE &

Now I want to use that from a script file as a background process. My problem is that the script never finish and stop on this line.

If I understand well sed never finish as long as command is running but anyway it should run in background and go to the next step?

If I remove the sed part the script ends up as expected.

I'm running GNU bash, version 4.2.53(1)-release (x86_64-redhat-linux-gnu) on Centos 6.4

  • try bash path-to-your-script. What do you see? – Evgeny Vereshchagin Aug 3 '15 at 16:00
  • I just tried bash script but it still doesn't give back hand. – Quentin Morrier Aug 4 '15 at 6:19
0

Your command needs to print a new-line to cause the pipe to process it.

test1(){ echo "testing-one"; sleep 100; }
test2(){ echo -n "testing-two"; sleep 100; }

test1| sed "s/^/$(date -u +%Z-%Y-%m-%d-%T)/"

When the function (serving as command proxy) tested is test1, it does emit a newline. When used, a line is emitted immediately, even if the command itself has not finished (sleep 100).

When the function test2 is used (edit the third line), the line is not emitted till the whole command line is finished.

  • The only part of this answer that might need quoting is the $(date ...) bit. Except that ^ is a non-standard zsh special pattern char, and so it might matter in that shell, and in crazy old Thomas shells (I think that's what they were called) it was the pipe token. It's probably theoretically correct though. Else if the asker is writing to a pipe it will very likely buffer. – mikeserv Aug 4 '15 at 5:10
  • @mikeserv The question is tagged bash, the user explains what version of bash he is using. Really: Where do you get it is zsh? – user79743 Aug 4 '15 at 5:55
  • I don't - and that's why I said the only part of this that for which quotes might actually serve a purpose is the one part you don't quote. I only noted that ^ might need quotes in some other shells which are not the askers'. – mikeserv Aug 4 '15 at 5:58
  • The command, as posted by the user, does not work. I made the absolutely least of editing to get it work, making it easier for the OP to understand what is going on. Accepting that the result of date is perfectly valid, correct and does not have any external input, it is perfectly valid to NOT quote it (in bash). Clear now?. – user79743 Aug 4 '15 at 6:09
  • @BinaryZebra You're right this is not my working command (I did so many tries... I posted the wrong one). I've just edited my post. – Quentin Morrier Aug 4 '15 at 6:35

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.