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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH - read does not wait for user input in some circumstances
# 1  
Old 10-28-2012
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 :

Code:
#!/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

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

I get :
Code:
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

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

When calling from a console

Code:
linux:~/bin # ~/bin/script_1

I get :

Code:
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 :

Code:
#!/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
Code:
linux:~/bin # ~/bin/script _1

give

Code:
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 :

Code:
#!/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
Code:
linux:~/bin # ~/bin/script _1

give


Code:
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
HTML Code:
http://unix.stackexchange.com/questions/26601
Make change in script_1
before :
Code:
#!/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 :
Code:
#!/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
# 2  
Old 10-28-2012
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
Code:
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.

Last edited by elixir_sinari; 10-29-2012 at 07:07 AM..
# 3  
Old 12-06-2012
[Solved] - BASH - read does not wait for user input in some circumstances

Quote:
Originally Posted by elixir_sinari
But, I suggest this because I do not see any such statements in script 2.
Code:
#!/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 :
HTML Code:
http://unix.stackexchange.com/questions/26601
Code:
#!/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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl to read user input

I am creating a bash that uses perl . The below code closes before the input is entered. If I run the perl as a .pl it is fine. What am I doing wrong? Thank you :). #!/bin/bash cd 'C:\Users\cmccabe\Desktop\wget' wget -O getCSV.txt http://xxx.xx.xxx.xxx/data/getCSV.csv print... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Using read to prompt for editable user input in Bash 3

Below is a simple script to prompt for user input while suggesting an editable default value at the prompt: shortname=user1 read -e -i $shortname -p "Please enter the username you would like to add: " input USERNAME="${input:-$shortname}" Please enter the username you would like to add:... (3 Replies)
Discussion started by: woodson2
3 Replies

3. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

4. Shell Programming and Scripting

Help with Bash user input

I am starting to learn how to use bash and I would like the script to do the following: Asks the user for his/her name Asks the user for one number Asks the user for another number Then it adds the two numbers, Also multiply the two numbers I have the part where you get the name, and I... (3 Replies)
Discussion started by: boyboy1212
3 Replies

5. Shell Programming and Scripting

Solaris- Read command from user input

I need to write a bourne shell script (solaris 10) that accepts input from the user. The input will be a command- any command like ls/ pwd/ mv etc. After the input is read, the shell must execute the command supplied by the user. I know we use read to play with user inputs. Just not sure how to... (2 Replies)
Discussion started by: PDManc
2 Replies

6. Shell Programming and Scripting

BASH - read use a path as input

I am trying to script simply data transfer. I would like to have the user input the source "SRC" (/Volumes/DriveName/Users/johnq123) and then name the directory that the copied information will go to, "DST" . put I can't get it to work - #!/bin/bash ... (8 Replies)
Discussion started by: dropkick888
8 Replies

7. Shell Programming and Scripting

Bash user input

Hi all, I currently have a script which uses read -p for user interaction. e.g. read -p "New user? " user Is it possible to have it so if the user enters nothing and just presses return it can resort to a specified value instead? Thanks! :) (5 Replies)
Discussion started by: JayC89
5 Replies

8. UNIX for Dummies Questions & Answers

How to read a line of text from user input?

Hiii I wanna a read a line of text from standard input. The user enter data like this way name phone_no month1_salary month2_salary that is user enter the name ,phone no and salary of 2 months in a single line by giving spaces. I wanna add the 3rd and 4th fields ...ie add both... (4 Replies)
Discussion started by: krishnampkkm
4 Replies

9. UNIX for Dummies Questions & Answers

getline to read input from a user

Hi, The gcc compiler has warned about using gets(), so I've been trying my hand at getline. Problem is that I've been able to read from a file, but what I really need is to read from a user's input. I want to use getline like a scanf() command, but I can't figure what to substitute for the fp... (6 Replies)
Discussion started by: sdsd
6 Replies

10. UNIX for Dummies Questions & Answers

making a .sh wait for user input

I need a script to halt at the end and wait for the user to hit a key...could be any ket or enter. I know it can be done but I am just starting out.. Thanks (9 Replies)
Discussion started by: verystupid
9 Replies
Login or Register to Ask a Question