试图才达到类似:管道到不同的命令基于退出状态在bash
command | { [[ ${PIPESTATUS[0] == 0 ]] && cmd_pipe_success || cmd_pipe_failed ; }
例如管道到不同的命令,取决于退出状态。
上述〔实施例没有作品,例如:
command_ok() { sed 's/^/OK /'; }
command_no() { sed 's/^/NO /'; }
touch x
ls -l x 2>&1 | { [[ ${PIPESTATUS[0]} == 0 ]] && command_ok || command_no ; }
rm x
ls -l x 2>&1 | { [[ ${PIPESTATUS[0]} == 0 ]] && command_ok || command_no ; }
两个打印OK
,因此,在这两种情况下运行command_ok
。
当ls
的出口站点非零时,可能实现运行command_no
?
我知道,这是可能实现像
command_ok() { sed 's/^/OK /'; return 0; }
command_no() { sed 's/^/NO /'; return 0; }
touch x
res=$(ls -l x 2>&1)
[[ $? == 0 ]] && command_ok <<<"$res" || command_no <<<"$res"
rm x
res=$(ls -l x 2>&1)
[[ $? == 0 ]] && command_ok <<<"$res" || command_no <<<"$res"
,但我想避免辅助变量,也没有临时文件。有可能的?
在'command'退出之前需要设置管道,对吧?你怎么能根据不存在的退出状态切换管道的目的地? – 2014-09-10 18:51:49
只是想通了 - 因为**我不知道命令的退出状态**,因为它仍在运行...... – kobame 2014-09-10 18:52:08
如果shell运行同一管道中的所有组件shell,理论上类似'{command1&pid = $ !; } | {wait $ pid && command_ok || command_no; ''可以工作。 POSIX并不禁止这样做,但我不知道有这样做的shell。 – chepner 2014-09-10 18:54:17