Amin Rezaei
Amin Rezaei

Reputation: 11

Bash script while loop breaks after running another script

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

Upvotes: 0

Views: 1270

Answers (2)

dvlcube
dvlcube

Reputation: 1316

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

Upvotes: 0

Andrew Vickers
Andrew Vickers

Reputation: 2664

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

Upvotes: 1

Related Questions