6

I am compiling something and depending on success (last line of output contains "success"= I want to scp the binary to the target. I'd prefer a piped oneliner. Is there a way to do this?

terdon
  • 242,166
tzippy
  • 351

4 Answers4

8

Try this:

your command | tail -n1 | grep -q 'success' && scp ...

Example:

% cuonglm at ~
% printf "sad\nsadsa\nsuccess\n" |
  tail -n1                       |
  grep -q 'success' && echo 'this command run'
this command run
cuonglm
  • 153,898
8

You're not giving any information on what/how you are compiling. However, in most cases, the compiler will return a successful exit signal if it compiled correctly so you could just use the shell's features directly:

$ gcc -o foo.bin foo.c && echo YES || echo NO
YES
$ gcc -o foo.bin foo.txt && echo YES || echo NO
foo.txt: file not recognized: File truncated
collect2: error: ld returned 1 exit status
NO

So, in your case, you could probably simply run

$ complile_command && scp binary user@server:/remote/path     
terdon
  • 242,166
3

One way:

cmd | awk 'END{exit!/success/}' && scp ...
2

You could, indeed, use a one-liner:

command | grep -q success && othercommand

This would execute othercommand if the command outputs something containing success.

devnull
  • 10,691