0

I need to run several tasks in sequence. The task names are listed in a file (jobs.txt). I use the following code (example):

#! /bin/bash

while IFS=$'\r' read -r line || [[ -n "$line" ]]; do
    echo $line
done < "jobs.txt"

It works well. However, I need to run these tasks with openmpi (in this example, change echo $line to mpirun -n 4 echo $line), then the script does not work any more... It only read the first line from "jobs.txt" and stopped.

Any idea what's the problem and how to fix it?

Thank you very much

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • does `mpirun` consume `stdin`? It might be draining the rest of `jobs.txt` if so – Eric Renouf Feb 11 '19 at 00:55
  • shouldn't the nonzero test be inside the loop? `while READ; do NONZERO && ECHO; done – jhnc Feb 11 '19 at 01:00
  • 1
  • @jhnc, the purpose of `read -r line || [[ $line ]]` as an idiom is to proceed into the body of the loop when the last line of the file doesn't have a terminating newline. `read` returns a nonzero exit status but still populates its output variable in that case. Of course, better practice is to generate valid UNIX text files, in which *all* lines have terminating newlines. – Charles Duffy Feb 11 '19 at 01:11
  • @CharlesDuffy thanks, haven't encountered that before – jhnc Feb 11 '19 at 01:25
  • Since you are using Open MPI, an other option is to `mpirun --stdin none -n 4 echo $line`. Keep in mind the **same** line will be printed 4 times (an other way to put it is that if you expect 4 MPI tasks print 4 different lines, you are doing it wrong). – Gilles Gouaillardet Feb 11 '19 at 04:03
  • @CharlesDuffy Thanks problem solved by your instruction. – michael morgan Feb 12 '19 at 22:46

0 Answers0