views:

48

answers:

1

I am using BASH 4. I am trying to find a way to legitimately prepend output to indicate the type of output that it is. I am able to do this with something kind of like this...

ls -l /tmp/abcdefgh 2> >(sed 's/^/stderr: /') 1> >(sed 's/^/stdout: /')
stderr: ls: cannot access /tmp/abcdefgh: No such file or directory

ls -l /tmp/ 2> >(sed 's/^/stderr: /') 1> >(sed 's/^/stdout: /')
stdout: drwxr-xr-x 3 root root 4096 2010-10-15 09:08 fsck
stdout: drwxr-xr-x 2 root root 4096 2010-09-10 06:01 kernel
stdout: drwxr-xr-x 2 root root 4096 2010-09-10 06:01 temp_keys
...

This seems to do the trick when I am logged in via SSH and run it interactively. However, this does not always work right if I try to just run the command as a remote command via ssh with the command in quotes. I can always get the stdout lines, but sometimes not the stderr lines.

This will produce output...

ssh root@server1 "ls -l /tmp/ 2> >(sed 's/^/stderr: /') 1> >(sed 's/^/stdout: /')"

This will not produce even an error message...

ssh root@server1 "ls -l /tmp/abcdefgh 2> >(sed 's/^/stderr: /') 1> >(sed 's/^/stdout: /')"

However, this shows the wget status results as stderr results (which it should)

ssh root@server1 "wget http://server2/package.rpm 2> >(sed 's/^/stderr: /') 1> >(sed 's/^/stdout: /')"
A: 

I tried the following on my computer, with no ssh involved at all:

$ ls asdfasdf 2> >(sed 's/^/stderr: /') 1> >(sed 's/^/stdout: /')
$ stderr: ls: cannot access asdfasdf: No such file or directory

That $ in the second line isn't a typo. Those sed commands are being run entirely in the background. ls gave its error message and finished with enough time for the shell to output the prompt before sed got a chance to output anything. I think the far side of your ssh is closing the connection before sed's output arrives.

Buffering through a subshell and a pipe might work better, because cat waits for its input to close rather than waiting for the foreground process to finish:

ssh root@server1 "(ls -l /tmp/abcdefgh 2> >(sed 's/^/stderr: /') 1> >(sed 's/^/stdout: /')) | cat"
Jander
Interesting, I will try that and post results back later today.
Jonathan