首页 > 解决方案 > 观看流程替换

问题描述

我经常运行命令

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

Slurm 命令在哪里查看您正在运行的作业数量squeue。这给了我输出并自动告诉我有多少行。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)'

标签: bashunixwatchprocess-substitution

解决方案


watch手册页:

请注意,命令被赋予“sh -c”,这意味着您可能需要使用额外的引用来获得所需的效果。

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


幸运的是,您正在执行的操作实际上并不需要该语法:

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

...或者,如果你真的想使用你的原始代码,即使是在严重的性能损失(每十分之一秒开始不只是一个,而是两个sh新的 shell - 首先,然后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

推荐阅读