0

Running another script in bash script while loop runs but the loop breaks! N.B. The script I mentioned just loops over files in current directory and just run mpirun. Here's my bash script:

#!/bin/bash
np="$1"
bin="$2"
ref="$3"
query="$4"
word_size="$5"

i=1;
input="$query"
while read line; do
echo $line
  if [[ "${line:0:1}" == ">" ]] ; then
    header="$line"
    echo "$header" >> seq_"${i}".fasta
  else
    seq="$line"
    echo "$seq" >> seq_"${i}".fasta
    if ! (( i % 5)) ; then
        ./run.sh $np $bin $ref $word_size
        ^^^^^^^^
        #for filename in *.fasta; do
        #    mpirun -np "${np}" "${bin}" -d "${ref}" -ql "${filename}" -k "${word_size}" -b > log
        #    rm $filename
        #done
    fi
    ((i++))
  fi
done < $input
8
  • When does the loop break? The only way this loop exits is if there is no more data to read from the file named by $query. Commented Oct 19, 2019 at 14:03
  • It's not the case. there are still lots of lines to be read but after the first time run.sh runs the loop breaks Commented Oct 19, 2019 at 14:15
  • 2
    Does run.sh read from standard input? It inherits its standard input from the loop, so it would consume the rest of the data before read line has a chance to execute again. Commented Oct 19, 2019 at 14:16
  • no it does not, run.sh just loops over files in current directory and run another program and pass each file to the program that just called by run.sh Commented Oct 19, 2019 at 14:25
  • 2
    @chepner was right, mpirun consumes stdin Commented Oct 19, 2019 at 15:09

2 Answers 2

1

The problem is that your run.sh script is passing no parameters to mpirun. That script passes the variables ${np} ${bin} ${ref} ${filename} ${word_size} to mpirun, but those variables are local to your main script and are undefined in run.sh. You could export those variables in the main script so that they are available to all child processes, but a better solution would be to use positional parameters in run.sh:

for filename in *.fasta; do
  mpirun -np "${1}" "${2}" -d "${3}" -ql "${4}" -k "${5}" -b > log
  rm $filename
done
Sign up to request clarification or add additional context in comments.

3 Comments

Does't work that way either, I say forget the run.sh script, if I call mpirun instead of calling run.sh, mpirun would work properly once and the main while loop breaks after running mpirun But if I call ls command instead of mpirun or run.sh the while loop works and ls would run multiple time.
Then I suspect that you are passing the parameters to mpirun in the wrong order. Surely you should be passing the command-line options and their values BEFORE you pass the ${bin} parameter. What happens when you call mpirun directly from the command line instead of via this script?
I think inside his script, $filename is an internal variable. So you probably want mpirun -np "${1}" "${2}" -d "${3}" -ql "${filename}" -k "${4}" -b > log. There is no ${5}.
0

I don't know about mpirun, but if you have anything inside your loop that reads from stdin, the loop will break.

1 Comment

So to redirect the stdin you could do e.g.: ./run.sh $np $bin $ref $word_size </dev/null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.