繁体   English   中英

bash脚本中的for循环不适用于nohup

[英]For loop inside a bash script is not working with nohup

我有一个bash脚本,即

#!/bin/bash

for number in {1..20..1}
do
  if [ $number = 1 ]
  then
    sed 's/seed=0/seed=100/' input > input2
    mv input2 input
  elif [ $number = 2 ]
  then
    mv output output1
    sed 's/seed=100/seed=200/' input > input2
    mv input2 input
  elif [ $number = 3 ]
  then
    mv output output2
    sed 's/seed=200/seed=300/' input > input2
    mv input2 input

    .....and so on.....
  fi

  ./compiled_code <input > output

done

当我使用qsub提交我的bash脚本时,for循环和if语句起作用,但是当我使用nohup提交bash脚本时,for循环不起作用,它仅运行脚本一次,并且不会再次提交该脚本。 我不知道为什么 ? 任何人有什么主意吗? 提前致谢。

这是一个重构,它删除了重复的代码,并使您的脚本与sh兼容,这是一个很好的好处。

#!/bin/sh

while read num seed; do
    sed "s/seed=0/seed=$seed/" input >"input$num"
    ./compiled_code <"input$num" > "output$num"
    rm "input$num"
done <<____HERE
    1 100
    2 200
    3 300
    : etc
____HERE

如果看起来像您的种子值是完全可预测的,则for循环的sh兼容替代品是使用seq类的外部实用程序(尽管这也不严格是POSIX)。

for num in $(seq 1 20); do
    sed "s/seed=0/seed=${num}00/" input >"input$num"
    ./compiled_code <"input$num" > "output$num"
    rm "input$num"
done

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM