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?
4 Answers
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
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
You could, indeed, use a one-liner:
command | grep -q success && othercommand
This would execute othercommand
if the command outputs something containing success
.