首页 > 解决方案 > 保留美元?使用文件描述符使用多个管道语句输出

问题描述

标签: bashawkscriptingpipe

解决方案


I suspect this is all you really need:

cmd=$(uname -a | awk '{print $1; f=1} END{exit !f}')
echo "$?"

That will exit with status 0 if uname produces any output and awk succesfully reads/prints it, 1 otherwise.

Look:

$ cmd=$(uname -a | awk '{print $1; f=1} END{exit !f}')
$ echo "$?"
0

$ cmd=$(uname + -a | awk '{print $1; f=1} END{exit !f}')
uname: extra operand ‘+’
Try 'uname --help' for more information.
$ echo "$?"
1

推荐阅读