0

I have two very simple scripts. I have asked this question but people thought I am doing it in different platform. Actually these two scripts are in same folder.

One is source.sh

#!/bin/bash
echo "start"
./call.sh
echo "end"

And second is call.sh

#!/bin/bash
passDir="/etc/passwd"
while read line
do
    while true
        do
            echo "prompt"
            #propmt for username
            read -p "Enter username : " username
            egrep "^$username" $passDir >/dev/null
            if [ $? -eq 0 ]; then
                echo "$username exists!"
            else
                userName=$username
                break
            fi
        done                    
done < user.txt

and user.text file is only two words in two lines

Hello
world

Output:

exisats!
prompt
exisats!
prompt
exisats!
prompt
exisats!
prompt
exisats!
prompt
exisats!
prompt

Until I press Ctrl+d I really appreciate if anyone can tel how I can fix this.

10
  • try this Commented Sep 14, 2014 at 10:22
  • @DOOM I saw that before. I can call another script and it is fine but when it comes to prompting information in the called script it goes to loop! Commented Sep 14, 2014 at 10:28
  • Have you redirected standard input anywhere? Are you running this from cron or some other automated system? Commented Sep 14, 2014 at 12:18
  • @EtanReisner I am running this from my laptop in same environment same folder. Commented Sep 14, 2014 at 13:18
  • 1
    You would have this exact same problem if you didn't have source.sh at all and only invoked call.sh directly. Thus, the way you ask this question (as if it's an issue about scripts calling each other) is misleading. Commented Sep 14, 2014 at 15:32

1 Answer 1

1

You can reduce this to a minimal example:

#!/bin/bash
while read line
do
    echo line is $line

    echo "prompt"
    read -p "Enter username : " username

    echo username is $username

done < user.txt

Now the problem is clear: the script reads everything from user.txt.

Only read should read from user.txt. We can tell read to do this by means of a file descriptor:

#!/bin/bash

exec 3< user.txt    # open the file, give it File Descriptor 3

while read -r -u3 line
do
    echo line is $line

    echo "prompt"
    read -p "Enter username : " username

    echo username is $username

done

exec 3<&-    # close the file
Sign up to request clarification or add additional context in comments.

3 Comments

Why read-write? I'd think that read-only access would be more appropriate here. Also, scoping the FD to the while loop would prevent it from leaking into future parts of the script.
@CharlesDuffy: I'm sure you're right. I'm not a bash expert, I just posted the first solution I got to work. May I take those tricks from your solution, to improve mine?
Absolutely; please do. (To explain the use of read -r, by the way -- by default, read processes backslash-escape sequences; to get the behavior which would otherwise be a more reasonable default, of representing data exactly as read, the -r flag is necessary).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.