0

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
    Please indent your program code to make it easier to read. – Cyrus Jan 19 '19 at 09:42
  • sorry, i am new to this Site, what do you mean by indenting the code ? – Skeptical Khan Jan 19 '19 at 09:49
  • I've done that now. – Cyrus Jan 19 '19 at 09:52
  • I got it, Thanks for your edition to my Question – Skeptical Khan Jan 19 '19 at 09:54
  • Is myscript.sh name of your script ? or you are calling another script ? – ganeshragav Jan 19 '19 at 13:32
  • myscript is the compiled file of my code which i want to run with the help of a bash script as above. – Skeptical Khan Jan 19 '19 at 13:56
  • 1
    It sounds like you're running it with `sh` instead of `bash`. What's your complete `nohup` command? – that other guy Jan 19 '19 at 22:42
  • 1
    Calling `./compiled_file output` will fail when the crontab starts your script in another directory. Show your crontab entry. After the loop the file `output` will only show the results of number `20`, use `output${number}` or ` >>` – Walter A Jan 20 '19 at 10:03
  • 1
    What is the purpose of your code? Can you skip the looping and use `sed -i 's/seed=0/seed=2000/' input` ? – Walter A Jan 20 '19 at 10:06
  • @ Walter, the compiled code is inside the same directory, after each run. Actually I want my bash script change the value of a variable inside input file and resubmit the job again, so for that purpose i have to run the for loop. After each run, i rename the output with a new name. – Skeptical Khan Jan 21 '19 at 06:32
  • @that other guy, i am using bash script to submit again and again my compiled code with a change in input file for each run. – Skeptical Khan Jan 21 '19 at 06:34
  • Again, please show exactly how you are submitting with `qsub` and how you are submitting with `nohup`. The hypothesis that you are using `sh` instead of `bash` in one scenario seems to be able to explain the symptoms fully; can you verify that the shell is indeed `/bin/bash`? See also https://stackoverflow.com/questions/5725296/difference-between-sh-and-bash – tripleee Jan 21 '19 at 07:22
  • Another possibility could be that the system where you run the script with ```nohup``` kills the process very soon. This is sometimes the case on clusters where user-run scripts are killed if they use too much cpu or memory. This is done to prevent users from running jobs like normal command instead of submitting them to the queue via ```qsub```. Can you check if your script with ```nohup``` works on a local machine, for instance by commenting out the submit command and run with ```bash -x```? – francesco Jan 21 '19 at 08:54
  • @tripleeee, I am using bash as you can see in the start of my bashscript, #!/bin/bash, i submit my code with qsub as qsub bash-script, inside bash bashscript, i also include #PBS -l nodes=1:ppn=12, #PBS -q bash etc, – Skeptical Khan Jan 21 '19 at 09:43
  • @franscesco, My server is one node machine with 24 threads, there is no such kind of condition, i can use all the memory. – Skeptical Khan Jan 21 '19 at 09:48
  • 1
    Still if you use more than the available memory the OOM killer might be configured to kill `nohup` scripts more easily. – tripleee Jan 21 '19 at 10:17
  • @tripleee, i have run the script for small tests codes also , i am sure, it is not a memory issue – Skeptical Khan Jan 21 '19 at 10:23

1 Answers1

1

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
tripleee
  • 175,061
  • 34
  • 275
  • 318