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?

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     
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
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.

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.