为什么我不能用作业控制在bash脚本? [英] Why can't I use job control in a bash script?

查看:144
本文介绍了为什么我不能用作业控制在bash脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个答案到另一个问题,有人告诉我,

In this answer to another question, I was told that

在脚本中你没有作业控制
  (并试图打开它是愚蠢的)

in scripts you don't have job control (and trying to turn it on is stupid)

这是我第一次听到这句话,而且我专心致志地摆弄着对作业控制(第7章)的bash.info部分,发现没有提到这两种断言。 [更新:的手册页为好一点,提典型的使用,默认设置,终端I / O,但没有真正的理由作业控制尤为不明智的脚本]

This is the first time I've heard this, and I've pored over the bash.info section on Job Control (chapter 7), finding no mention of either of these assertions. [Update: The man page is a little better, mentioning 'typical' use, default settings, and terminal I/O, but no real reason why job control is particularly ill-advised for scripts.]

那么,为什么不基于脚本的作业控制工作,是什么使它成为一个不好的做法(又名愚蠢)?

So why doesn't script-based job-control work, and what makes it a bad practice (aka 'stupid')?

编辑:有问题的脚本启动一个后台进程,启动第二个后台进程,然后尝试将第一个进程重新推向前台,使其具有正常的终端I / O(好像直接运行),然后可以从脚本的外重定向的。不能做一个后台进程。

The script in question starts a background process, starts a second background process, then attempts to put the first process back into the foreground so that it has normal terminal I/O (as if run directly), which can then be redirected from outside the script. Can't do that to a background process.

正如接受的答案其他问题指出,的确存在许多解决特定的问题而不是试图作业的其他脚本控制。精细。而且骂得狗血淋头脚本使用硬codeD工号—显然不好。但我想了解作业控制是否是从根本上注定的做法。它仍然看起来也许它的可能的工作...

As noted by the accepted answer to the other question, there exist other scripts that solve that particular problem without attempting job control. Fine. And the lambasted script uses a hard-coded job number — Obviously bad. But I'm trying to understand whether job control is a fundamentally doomed approach. It still seems like maybe it could work...

推荐答案

他的意思是,作业控制在默认情况下以非交互模式关闭(即在脚本中。)

What he meant is that job control is by default turned off in non-interactive mode (i.e. in a script.)

庆典手册页:

JOB CONTROL
       Job  control refers to the ability to selectively stop (suspend)
       the execution of processes and continue (resume) their execution at a
       later point.
       A user typically employs this facility via an interactive interface
       supplied jointly by the system’s terminal driver and bash.

   set [--abefhkmnptuvxBCHP] [-o option] [arg ...]
      ...
      -m      Monitor mode.  Job control is enabled.  This option is on by
              default for interactive shells on systems that support it (see
              JOB CONTROL above).  Background processes run in a separate
              process group and a line containing their exit status  is
              printed  upon  their completion.

当他说,很愚蠢,他的意思,不仅:

When he said "is stupid" he meant that not only:


  1. 是作业控制的意味着的大多为促进交互式控制(而一个脚本可以与PID的直接工作),也

  2. 我引用他原来的答复,的 ...依赖于一个事实,你没有在这是一个坏的假设,使的脚本pviously启动任何其他工作$ P $。这是相当正确的。

  1. is job control meant mostly for facilitating interactive control (whereas a script can work directly with the pid's), but also
  2. I quote his original answer, ... relies on the fact that you didn't start any other jobs previously in the script which is a bad assumption to make. Which is quite correct.

更新

在回答您的评论:是的,没有人会阻止你在你的bash脚本使用作业控制 - 有对的强制禁用设置-m <无硬盒/ code>(即是,从剧本作业控制会,如果你想要的工作。)请记住,在年底,特别是在脚本中,总是有对皮肤猫不止一种方法,但有些方法是更便携,更可靠,更简单地处理错误的情况下,解析输出,等等。

In answer to your comment: yes, nobody will stop you from using job control in your bash script -- there is no hard case for forcefully disabling set -m (i.e. yes, job control from the script will work if you want it to.) Remember that in the end, especially in scripting, there always are more than one way to skin a cat, but some ways are more portable, more reliable, make it simpler to handle error cases, parse the output, etc.

您的具体情况可能会或可能无法保证的方式从什么 lhunath (和其他用户)认为最佳做法的不同。

You particular circumstances may or may not warrant a way different from what lhunath (and other users) deem "best practices".

这篇关于为什么我不能用作业控制在bash脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆