【问题标题】:Run multiple scripts one after another in bash在bash中一个接一个地运行多个脚本
【发布时间】:2016-07-01 19:10:34
【问题描述】:

我正在尝试一个接一个地运行三个脚本。一种要求用户输入文件。这些脚本都是单独运行的,当我将一些脚本与 && 结合使用时,它们会起作用,但是当我尝试将这三个脚本结合起来时,它会失败。他们需要彼此工作,所以应该去script1.sh -> script2.sh -> script3.pl

  1. script1.sh 需要输入 2 个文件
  2. script2.sh 需要输出一个.csv
  3. script3.pl 在创建的 .csv 上运行,需要一个我想提示用户输入的输入。

每个都需要很长时间才能运行,所以我也试图让它们在后台运行nohup。它们分别运行如下:

script1.sh file1 file2

script2.sh > file.csv

script3.pl --input answer    

我尝试过使用以下方法;

echo -n "Question?  "
read answer
nohup script1.sh $1 $2 && script2.sh > file.csv && script3.pl --input $answer &

它可以处理问题,script2.shscript3.pl,但是当我有 script1.sh 时它就不起作用了. Script1.shscript2.sh 结合起来也可以,但我正在尝试将这三个结合起来。

【问题讨论】:

  • 您是否尝试将所有 3 行保存在单个 shell 脚本中,例如 combined.sh,然后使用 nohup combined.sh & 执行它?
  • 我仍然需要能够提出要输入到 script3.pl 中的问题
  • 不过好像可以直接在最上面问这个问题?为什么不使用read -p "Question?" answer 然后nohup combined.sh file1 file2 $answer & 运行它?
  • 没有nohup 是否可以工作?这可能会使事情复杂化;理想情况下,您希望通过nohup 运行整个命令链,而不仅仅是script1.sh $1 $2。另一个想法是,当命令以失败代码退出时,您正在使用 &&short circuits。请改用分号 (;)。

标签: linux bash shell command-line


【解决方案1】:

我认为您需要这样的组合脚本 - 另存为 combined:

#!/bin/bash
script1.sh file1 file2
script2.sh > file.csv
script3.pl --input "$1"

然后就可以运行了:

nohup ./combined "answer" & 

【讨论】:

    【解决方案2】:

    试试这个:

    read -p "Question?  " answer ; \
    nohup script1.sh file1 file2    && \
    nohup script2.sh > file.csv     && \
    nohup script3.pl --input $answer &
    

    【讨论】:

    • 我认为运行上述命令的 shell 本身不受 SIGHUP 保护。因此,即使script1.sh 在执行时注销也不会被 SIGHUP 停止,但需要运行 script2.shscript3.sh 的 shell 将退出......因此不会运行它们。
    猜你喜欢
    • 2021-05-04
    • 1970-01-01
    • 2023-01-13
    • 2017-12-12
    • 1970-01-01
    • 2020-11-04
    • 2018-08-12
    • 2018-06-19
    • 1970-01-01
    相关资源
    最近更新 更多