Read not prompting/ pausing for input

Heya people,
So my script is one of those make-user-scripts, making a bunch of directories in the home folder and copying some files over into the new directories.
However, part of the thing my script need to do is check the home folder if a username matches with a directory which already exists.
e.g. if the username 'user1' has a directory in the home folder under the same name, it will prompt the user if he/she wants to keep or delete the directory (and its contents) before moving on with the script.

Here is the part of the script which does this:

cat $1 | while read line; do 

    username=$line
    if [ -d $4/$username ];
        then
        echo WARNING username $username already has a directory.
        read -p "Would you like to KEEP or DELETE?" answer
        
        if [ "$answer" == "DELETE" ];
            then
            rm -fr $4/$username
        elif [ "$answer" == "KEEP" ];
            then
            echo "Keeping directory $username"
        else
            echo "Unknown answer assuming keeping file"
        fi

    fi

done 

Where:
$1 = path to the username_list file
$4 = path to the home directory for user creation

Now as the thread title suggests, the problem is the read line is not prompting for user input.
I suspect it may be something to do with the fact I'm piping via the while loop, but I don't know I'm just a newbie to all this. Can anyone suggest a solution to this?

Thanks in advance,

Cloudy

Try running the following command to debug your script:

sh -x <your script>

Nope that doesn't help one little bit.

Give this one go, Getting user input Post: 302107299
It worked fine with /bin/sh and /bin/bash on Debian Testing

You've got two read s in your code snippet; which one does not operate the way you'd expect it to? In your question text, you name the read line , but this one is not prompting, whilst the other, prompting one is read answer .

Please look again at your script and guess/find out where the second read is redirected to; you may find the answer by yourself.

And, BTW, it doesn't help a little bit either to give snappy answers to people offering help...

In case you are not aware, the redirections that affect the while-loop statement are inherited by all of the commands within it. So, in your case, the pipe between cat and while-read redirects the standard input of the entire while-loop to cat's output. Both of your read commands will be taking their input from cat. Line 1 from cat will be consumed by the first read statement. Line 2 from cat will be consumed by the second read statement, etc. Is that intentional? I assume it's not, since if that were the case, there would be no need to prompt cat for a response.

Regards,
Alister

Hi,

Instead of:

cat $1 | while read line; do
Try:

for line in `cat $1`
do

That way you don't have the "read inside a read loop issue" that alister pointed out. BTW well spotted alister - that one didn't register with me but once you pointed it out it seemed the best way to fix the script was to have an outer loop not using reads so the inner loop could issue reads.

Hope this helps.

-Andy.