Skip to main content
added 1 character in body
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 265

Mr. Shaller'sSchaller'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.

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.

Mr. Schaller'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.

added 364 characters in body
Source Link
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 answers.txt$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.

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

cntr=0
cat answers.txt | 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.

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.

python uses colons, not the shell
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

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

cntr=0
cat answers.txt | 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.

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

cntr=0
cat answers.txt | 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.

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

cntr=0
cat answers.txt | 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.

Source Link
LJKims
  • 469
  • 4
  • 13
Loading