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.

Bernard
  • 4,240
  • 18
  • 55
  • 88
  • [try this](http://stackoverflow.com/questions/8352851/how-to-call-shell-script-from-another-shell-script) – DOOM Sep 14 '14 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! – Bernard Sep 14 '14 at 10:28
  • Have you redirected standard input anywhere? Are you running this from cron or some other automated system? – Etan Reisner Sep 14 '14 at 12:18
  • @EtanReisner I am running this from my laptop in same environment same folder. – Bernard Sep 14 '14 at 13:18
  • How are you running the main script when this fails to work? Are there other `read` statements in that script (or the other scripts) that work correctly when you run the main script? – Etan Reisner Sep 14 '14 at 13:54
  • Can you give us a [minimal complete example](http://stackoverflow.com/help/mcve), so that we can reproduce the problem? – Beta Sep 14 '14 at 14:30
  • @EtanReisner I have uploaded all the code. I call main script from terminal on ubuntu. Please have a look at the added code. – Bernard Sep 14 '14 at 14:30
  • @Beta I have edited my question completely. Hope it makes much sense to you. – Bernard Sep 14 '14 at 14:47
  • 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. – Charles Duffy Sep 14 '14 at 15:32
  • `dos2unix user.text`. Good luck. – shellter Sep 14 '14 at 19:54

1 Answers1

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
Beta
  • 96,650
  • 16
  • 149
  • 150
  • 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. – Charles Duffy Sep 14 '14 at 15:35
  • @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? – Beta Sep 14 '14 at 15:43
  • 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). – Charles Duffy Sep 14 '14 at 15:44