重用 shell 命令的最后一行输出

tzi*_*ppy 6 stdout

我正在编译一些东西并取决于成功(输出的最后一行包含“success”=我想将二进制文件发送到目标。我更喜欢管道oneliner。有没有办法做到这一点?

cuo*_*glm 8

尝试这个:

your command | tail -n1 | grep -q 'success' && scp ...
Run Code Online (Sandbox Code Playgroud)

例子:

% cuonglm at ~
% printf "sad\nsadsa\nsuccess\n" |
  tail -n1                       |
  grep -q 'success' && echo 'this command run'
this command run
Run Code Online (Sandbox Code Playgroud)


ter*_*don 8

您没有提供有关您正在编译的内容/方式的任何信息。但是,在大多数情况下,如果编译器正确编译,编译器将返回成功退出信号,因此您可以直接使用 shell 的功能:

$ 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
Run Code Online (Sandbox Code Playgroud)

因此,在您的情况下,您可能只需运行

$ complile_command && scp binary user@server:/remote/path     
Run Code Online (Sandbox Code Playgroud)


dev*_*ull 2

事实上,你可以使用一句话:

command | grep -q success && othercommand
Run Code Online (Sandbox Code Playgroud)

othercommand如果命令输出包含success.