BASH - read does not wait for user input in some circumstances

Hello.

I am running 2 scripts : script_1 and script_2
These scripts are run as root

Script 2 contains :

#!/bin/bash
#
# ~/bin/script_2
#
E_BAD_PARAM=115
#
date2stamp () {
    date --date "$1" +%Y-%m-%d___%H:%M:%S
}
#
USER_NAME=$1
NB_PARAM=$#
PARAM0=$0
if [ $NB_PARAM -ne 1 ] ; then
    echo "Usage : ~/bin/script_2  user_name"
    exit $E_BAD_PARAM
fi
#
echo "We are in script  2"
#
TIMESTAMP=$(date2stamp  now)
#
if [ -f /home/$USER_NAME/.ssh/id_rsa -a -f /home/$USER_NAME/.ssh/id_rsa.pub  ] ; then
    echo "SSH Keys exists for user : $USER_NAME ."
    echo "Hit CTRL-C to abort"
    echo "Anything else to continue and create a new key for that user"
    read A_LINE
    echo "Creating the key ...."
    mv   /home/$USER_NAME/.ssh/authorized_keys   /home/$USER_NAME/.ssh/authorized_keys.$TIMESTAMP
    mv   /home/$USER_NAME/.ssh/id_rsa   /home/$USER_NAME/.ssh/id_rsa.$TIMESTAMP
    mv   /home/$USER_NAME/.ssh/id_rsa.pub   /home/$USER_NAME/.ssh/id_rsa.pub.$TIMESTAMP
fi
#
echo "Done."

When calling from a console

linux:~/bin # ~/bin/script_2  user_test

I get :

linux:~/bin # ~/bin/script_2  user_test
We are in script 2
SSH Keys exists for user : user_test .
Hit CTRL-C to abort
Anything else to continue and create a new key for that user

and the program wait until I hit enter or CTRL-C

Now this script is call from script_1

#!/bin/bash
#
# ~/bin/script_1
#
############################################
#
~/bin/script_2  user_test

When calling from a console

linux:~/bin # ~/bin/script_1

I get :

linux:~/bin # ~/bin/script_1
We are in script 2
SSH Keys exists for user : user_test .
Hit CTRL-C to abort
Anything else to continue and create a new key for that user

and the program wait until I hit enter or CTRL-C
So same behavior.

If script_1 is :

#!/bin/bash
#
# ~/bin/script_1
#
############################################
#
for USER_NAME in toto tata user_test ; do
    echo "User : $USER_NAME"
    if [[ "$USER_NAME" != "user_test" ]] ; then
        continue
    fi
    ~/bin/script_2  "$USER_NAME"
done

Then calling script_1

linux:~/bin # ~/bin/script _1

give

linux:~/bin # ~/bin/script_1
We are in script 2
SSH Keys exists for user : user_test .
Hit CTRL-C to abort
Anything else to continue and create a new key for that user

and the program wait until I hit enter or CTRL-C
So same behavior.

In real life, the list of users come from a file

Now script_1 is :

#!/bin/bash
#
# ~/bin/script_1
#
############################################
#
while read USER_NAME ; do
    echo "User : $USER_NAME"
    if [[ "$USER_NAME" != "user_test" ]] ; then
        continue
    fi
    ~/bin/script_2   "$USER_NAME"
done   <   "~/list_user"

Then calling script_1

linux:~/bin # ~/bin/script _1

give

linux:~/bin # ~/bin/script_1
User : bidon
User : user_test
We are in script 2
SSH Keys exists for user : user_test .
Hit CTRL-C to abort
Anything else to continue and create a new key for that user
Creating the key ....
`/home/user_test/.ssh/authorized_keys' -> `/home/user_test/.ssh/authorized_keys.2012-10-28___15:50:57'
`/home/user_test/.ssh/id_rsa' -> `/home/user_test/.ssh/id_rsa.2012-10-28___15:50:57'
`/home/user_test/.ssh/id_rsa.pub' -> `/home/user_test/.ssh/id_rsa.pub.2012-10-28___15:50:57'
version: 1  subdir: 2012_10_25 DIR: /data_1/002_config_linux/install_2012_09_04/006_backup_current_conf_files
Generating public/private rsa key pair.
........
........
Done

Now script 2 does not wait for "enter" or "CTRL-C"

I think that the problem come from because there is 2 read commands in sequence
One to get user name one by one
And the second which wait for user input.

Any idea to make this working ?

---------- Post updated at 17:02 ---------- Previous update was at 16:24 ----------

Finally found info on

http://unix.stackexchange.com/questions/26601

Make change in script_1
before :

#!/bin/bash
#
# ~/bin/script_1
#
############################################
#
while read USER_NAME ; do
    echo "User : $USER_NAME"
    if [[ "$USER_NAME" != "user_test" ]] ; then
        continue
    fi
    ~/bin/script_2   "$USER_NAME"
done   <   "~/list_user"

after correction :

#!/bin/bash
#
# ~/bin/script_1
#
############################################
#
while read USER_NAME <&5  ; do
    echo "User : $USER_NAME"
    if [[ "$USER_NAME" != "user_test" ]] ; then
        continue
    fi
    ~/bin/script_2   "$USER_NAME"
done   5<   "~/list_user"

Thread is closed.

Thank you for taking time to re'ad this thread

The problem is quite a simple one. One of the many things inherited by a child process from the parent are the standard file descriptors (0 - standard input, 1-standard output, 2-standard error). Also, read reads from standard input.

When you are running script 1 without any redirection, standard input for both script 1 and 2 comes from the terminal and hence, the read command waits for a line.

When you are running script 1 with standard input source being changed to the file, standard input for script 2 also comes from the file and the read in script 2 will read the next line from the file (or fail if input has been exhausted). In any case, it will go on to the next set of statements.

If you need to read that line (looks like that's a dummy read; you just want to wait, right?) from the terminal, you may include a statement such as

exec < /dev/tty

at the beginning of script 2. But, this will also make the terminal the input source for other commands (only in script 2; script 1 will continue reading from the file as a child cannot affect its parent) reading from standard input. But, I suggest this because I do not see any such statements in script 2.

#!/bin/bash
#
# ~/bin/script_2
#
E_BAD_PARAM=115
#
......
......
if [ -f /home/$USER_NAME/.ssh/id_rsa -a -f /home/$USER_NAME/.ssh/id_rsa.pub  ] ; then
    echo "SSH Keys exists for user : $USER_NAME ."
    echo "Hit CTRL-C to abort"
    echo "Anything else to continue and create a new key for that user"
    read A_LINE
    echo "Creating the key ...."
......
......
......
fi
#
echo "Done."

This exactly what I had found as I said in :

http://unix.stackexchange.com/questions/26601
#!/bin/bash
#
# ~/bin/script_1
#
############################################
#
while read USER_NAME <&5  ; do
    echo "User : $USER_NAME"
    if [[ "$USER_NAME" != "user_test" ]] ; then
        continue
    fi
    ~/bin/script_2   "$USER_NAME"
done   5<   "~/list_user"

Thank you for taking time to help me.