观看流程替换

Watch with Process Substitution

我经常运行命令

squeue -u $USER | tee >(wc -l)

其中 squeueSlurm command 以查看您 运行 有多少工作。这给了我 squeue 的输出,并自动告诉我其中有多少行。

我如何watch这个命令?

watch -n.1 "squeue -u $USER | tee >(wc -l)" 结果

Every 0.1s: squeue -u randoms | tee >(wc -l)                                                                                                                                                                                                                                                                                                        Wed May  9 14:46:36 2018

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `squeue -u randoms | tee >(wc -l)'

来自 watch 手册页:

Note that command is given to "sh -c" which means that you may need to use extra quoting to get the desired effect.

sh -c 也不支持进程替换,您在此处使用的语法为 >().


幸运的是,您正在做的事情实际上并不需要该语法:

watch -n.1 'out=$(squeue -u "$USER"); echo "$out"; { echo "$out" | wc -l; }'

...或者,如果您真的想要使用您的原始代码,即使性能损失很大(开始时不只是一个,而是两个 每十分之一秒发射一次新炮弹——首先是 sh,然后是 bash):

bash_cmd() { squeue -u "$USER" | tee >(wc -l); } # create a function
export -f bash_cmd            # export function to the environment
watch -n.1 'bash -c bash_cmd' # call function from bash started from sh started by watch