Watch with Process Substitution

喜你入骨 提交于 2019-12-08 05:07:58

问题


I often run the command

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

where squeue is a Slurm command to see how many jobs you are running. This gives me both the output from squeue and automatically tells how many lines are in it.

How can I watch this command?

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

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)'

回答1:


From the watch man page:

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 also does not support process substitution, the syntax you're using here as >().


Fortunately, that syntax isn't actually needed for what you're doing:

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

...or, if you really want to use your original code even at a heavy performance penalty (starting not just one but two new shells every tenth of a second -- first sh, and then 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


来源:https://stackoverflow.com/questions/50262534/watch-with-process-substitution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!