For loop inside a bash script is not working with nohup

545 views Asked by At

I have a bash script i.e

#!/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

for loop and if statements are working when i submit my bash script with qsub , but when i submit it with nohup , the for loop is not working , it runs the script only one time and does not resubmit the script again. I do not know why ? any body has any idea ? thanks in advance.

1

There are 1 answers

0
tripleee On

Here is a refactoring which removes the repetitive code and makes your script compatible with sh as a nice bonus.

#!/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

If, like it appears, your seed values are completely predictable, the sh -compatible replacement for your for loop is to use an external utility like seq (though this isn't strictly POSIX either).

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