1

I have a shell script and I have provided answers for the script through answers.txt file.

echo -n "Enter your name: "; read name
echo -n "Enter your number: "; read number
echo "Your name is $name and your number is $number"

answers.txt file

Buvanesh
12345

I run this script using script.sh < answers.txt

The answer file is dynamic, means user can add more names and numbers in the answer file.

e.g.

Buvanesh
12345
Kanna
45678

In that case, if I run the script it will take only first two inputs. I want to take shell script all inputs and give output accordingly. I guess I have to implement a loop, but I don't know exactly how to for this case. Can anyone help me with this?

1
  • welcome to U&L stack exchange! Don't forget to up-vote answers that helped you, and select the one you like best with the checkmark.
    – Jeff Schaller
    Jun 5, 2017 at 13:30

3 Answers 3

1
while IFS= read -r name && IFS= read -r number; do
   #IFS= read -r number || break
   printf 'Your name is %s and your number is %d\n' "$name" "$number"
done < answers.txt

Your thought processes were on the right track, we need to bring in a loop, the while loop whose condition are read commands joined by an AND.

3
  • 2
    while read name && read number ?
    – ilkkachu
    Jun 5, 2017 at 13:35
  • @ilkkachu Thanks for the updates. I have included the changes recommended.
    – user218374
    Jun 5, 2017 at 13:42
  • and along roaima's comment on mine, incorporate the -p prompt string, if the OP really wants it
    – Jeff Schaller
    Jun 5, 2017 at 13:42
0

The read command will fail when there's no more input, so you could use it as the looping condition:

#!/bin/sh
echo -n "Enter your name: "
while read name
do
  echo -n "Enter your number: "
  read number
  echo "Your name is $name and your number is $number"
done
5
  • Though this doesn't check if the number line gets read correctly
    – ilkkachu
    Jun 5, 2017 at 13:35
  • @ilkkachu read number || break perhaps. With an extra echo -n "Enter your name: " at the end of the loop. Better still, incorporate the prompt in the read itself, while read -p "Enter your name: " name && read -p "Enter your number: " number; do ... done Jun 5, 2017 at 13:37
  • @Shaller, I have applied your concept in following script. But not working :(. paste.fedoraproject.org/paste/… Jun 5, 2017 at 13:50
  • that looks like completely different input than your original question
    – Jeff Schaller
    Jun 5, 2017 at 13:58
  • @Jeff I have applied Rakesh answer. It's working fine. Thanks a lot. Jun 5, 2017 at 14:02
0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .