“ wait&”(“ wait&”符号)是有用的(流行的)shell / shell脚本惯用法或技巧吗?


12

我已经“继承”了运行GNU“ bash” shell的Linux机器的一些shell脚本。在一种特定情况下,机器运行GNU bash版本2.0.5b

这些脚本之一有一个wait &(“&”符号)作为for循环“ for行”的一部分。乍一看,这似乎是一种好奇/有趣的习惯用法,但是我在网上搜索时并未返回任何相关信息。man wait显示了“ BASH_BUILTINS”(“ BASH BUILTINS COMMAND”)联机帮助页,该联机帮助具有以下描述:

wait [n]  
  Wait for the specified process and return its termination status.
  n may be a process ID or a job spec­ification;  if  a  job spec is given,
  all processes in that job's pipeline are waited for.  If n is not
  given, all currently active child processes are waited for, and the 
  return status is zero. If n speci­fies a non-existent process or job, 
  the return status is 127.  Otherwise, the return status is the exit 
  status of the last process or job waited for.

通过阅读该手册页的那部分,在我看来,它wait &是在默默地(在后台)确保“ 正在等待所有当前活动的子进程,并且返回状态为零 ”。我的解释正确吗?这是常见和/或有用的习语吗?

为了增加上下文,我正在谈论脚本中的以下用法:

for file in `ls *.txt ; wait &`
do
   ...
   [cp instructions]
   ...
   [mv instructions]
   ...
   [mailx instruction]
done

Answers:


14

我无法想象编写此代码的任何理由,而且我不太确定编写此代码的人试图实现的目标。wait这里什么都不做-从它的角度来看,没有孩子的过程,所以它只会立即退出,基本上作为一个空操作(wait本身执行的子过程中由于命令替换,但是这是不相关的)。

顺便说一句,解析ls的输出非常脆弱。相反,请考虑这样做:

for file in *.txt; do
    ...
done
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.