Skip to main content
3 of 4
added 364 characters in body
LJKims
  • 469
  • 4
  • 13

Mr. Shaller's answer is good. The downside of redirecting input from a file is that your manual input is now also subject to the loop so that users will have to know to hit enter (not provide a value) to end the loop. You might use your current logic for manual runs and do something like below when there is an input file ($1) supplied to the script (not redirected).

If you are confident that the format of the file will always be "name\nnumber\n" then you could do:

cntr=0
cat $1 | while read line
do
    if [ $cntr -eq 0 ]
    then
      name=$line
      cntr=1
    elif [ $cntr -eq 1 ]
      number=$line
      echo "Your name is $name and your number is $number"
      cntr=0
    fi
done

This should print your statement until all name/number combinations have been read.

LJKims
  • 469
  • 4
  • 13