在Bash中更新watch命令中的变量

huangapple go评论56阅读模式
英文:

Updating variables in the watch command in bash

问题

我有一系列以序列命名的目录(即001 002 003 ... 999)。我正在运行一个作用于每个目录中内容的作业,按照从001到999的顺序升序执行。作业会将结果回显到每个目录中的output.txt文件中。当作业完成处理目录中的内容时,它会将"running DIRECTORY_NAME"回显到父目录中的job_status.txt文件,并继续处理下一个目录。

我想运行一个简单的命令,查看作业当前正在运行的目录的output.txt。

我尝试过

watch "i=$(tail -n 1 job_status.txt | awk '{print $2}'); tail $i/output.txt"

这最初是有效的;然而,当作业移动到新目录时,watch命令中的$i未更新,我会继续查看作业已经完成的目录中的output文件。

如何解决这个问题?

非常感谢

Jacek

英文:

I have a series of directories with names in a sequence (i.e. 001 002 003 ... 999). I am running a job that works on the contents in each directory, ascending the sequence from 001 to 999. The job will echo into an output.txt file in each directory. When the job has finished working on the content in a directory, it echoes "running DIRECTORY_NAME" to the job_status.txt file in the parent directory and moves on to work on the next directory.

I would like to run a simple command that looks at the output.txt of the directory that the job is currently running in.

I tried

watch "i=$(tail -n 1 job_status.txt | awk '{print $2}'); tail $i/output.txt"

This works initially; however, when the job moves on to a new directory, $i in the watch command is not updated and I'm stuck looking at the output file from the directory that the job has already completed.

How can I solve this issue?

Many thanks

Jacek

答案1

得分: 3

你在watch执行之前甚至扩展了变量。请改用单引号代替:

watch 'i=$(awk "END { print $2 }" job_status.txt); tail "$i/output.txt"'

当执行该命令时,watch调用sh -c,因此定义命令时要像使用sh一样调用它。

英文:

You're expanding the variable even before watch executes. Use a single quote instead:

watch 'i=$(awk "END { print \$2 }" job_status.txt); tail "$i/output.txt"'

Watch calls sh -c when executing the command, so define the command as if you're calling it with sh.

huangapple
  • 本文由 发表于 2023年2月8日 11:41:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定