Getting user input from inside a while loop?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Getting user input from inside a while loop?
# 1  
Old 09-28-2009
Getting user input from inside a while loop?

I'm new to BASH and i'm trying to create a script which is simply put a large find and replace file. This is what I have so far


N=0
while read LINE ; do

N=$((N+1))

sed 's/'$2'/'$3'/g' $LINE > .temp
echo "Changes to file $N = $LINE"
echo 'The following changes will be made will be made to $LINE :'
diff .temp $LINE
echo 'Are you sure you want to make these changes? y/
readline a

if [[ $a == "N" || $a == "n" || $a == "No" || $a == "no" ]]; then
echo "No changes have been made to $LINE"
rm .tempfr

elif [[ $a == "Y" || $a == "y" || $a == "Yes" || $a == "yes" ]]; then

mv .tempfr $LINE
echo "The given changes have been made successfully"

else
echo $a "is not a recognized input. Please use y/n"

fi
rm .temp
done < $1



For this code the $1 is a file which is simply a list of files which need to be searched with this find and remove script. $2 is the word that i'm looking to find and $3 is the word i'm looking to replace it with. The problem that I seem to be having is that every time that I run this script it seems to skip the

read a

line, and assumes that the user put in no input. I'm worried that this has something to do with using two read commands but I don't exactly know what the problem would be. Does anyone know why it isn't allowing me to ask for user input from inside this while loop?
# 2  
Old 09-28-2009
Code:
echo 'Are you sure you want to make these changes? y/n '
read  -sn1 a < $(tty)

As loops are executed in a subshell, they can't read from the same stdin as the script is executed.

Last edited by daPeach; 09-28-2009 at 06:02 PM.. Reason: missed a space between '-sn1' and 'a' variable name
# 3  
Old 09-28-2009
Quote:
Originally Posted by daPeach
Code:
echo 'Are you sure you want to make these changes? y/n '
read  -sn1a < $(tty)

As loops are executed in a subshell, they can't read from the same stdin as the script is executed.
I'm afraid I don't fully understand. I see the logic in attaching the -sn1 to the read file. That makes it accept y, Y, yes, Yes, n, N, no, and No without having to go through the whole if or statement that I wrote. But I don't know what the -a < $(tty) does. and I don't see how it would help me deal with the stdin issue.
Do you know of a way I could write my code to get around that?
# 4  
Old 09-28-2009
sorry, 'a' is not part of read options: it's not '-a', it's ' a' your variable's name.

I've edited my post to correct my typo.

Last edited by daPeach; 09-29-2009 at 12:21 AM..
# 5  
Old 09-28-2009
Specific tty is not needed actually, /dev/tty should do
# 6  
Old 09-29-2009
Quote:
Originally Posted by matrixmadhan
Specific tty is not needed actually, /dev/tty should do
well, how?
As I can launch a script from /dev/pts/'any munber', or a real /dev/tty'any number', how else could you determine what "tty" should give the input?

Last edited by daPeach; 09-29-2009 at 12:19 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Would like to check user input for letters within a loop

Hi All, #!/bin/bash #Just trying to check if letters are in the user input. Any tips? # I have tried regexp and using 0-9 etc, i cannot get this to work either in just an if statement or while in a loop. echo "Please pick a number" read num if ; then echo "Please enter a number"... (7 Replies)
Discussion started by: jvezinat
7 Replies

2. Shell Programming and Scripting

Unable to read user input inside a loop

Hi, This query is a part of a much more lengthy script. I wish to look for all the files in a folder named "data" which in this case has two files i.e. plan.war and agent.properties. For all the files found under data I wish to ask the user as to where they wish copy the files to. Below,... (14 Replies)
Discussion started by: mohtashims
14 Replies

3. Shell Programming and Scripting

Loop breaks on yes/no user input

I have a shell script, and its pretty much done, I decided to add a loop that ends or continues depending on user input. like "would you like to continue?" and if I hit y or yes it will run the loop again until I hit n or no and breaks out of the loop. To be hones I didn't think I needed to add... (2 Replies)
Discussion started by: Demon_Jester
2 Replies

4. Homework & Coursework Questions

How to read user keyboard input inside the case?

I need to Write a shell script that allows some system-administration tasks to be preformed automatically from a menu-driven interface. with automated following tasks: Copy directory tree Delete files or directories Output Information (this part is done ) *Copy directory tree The “Copy... (2 Replies)
Discussion started by: femchi
2 Replies

5. Shell Programming and Scripting

no chance to input passwd when create new user in loop

Hi Dears, I have one script to create new users with information in one plain text file. This script will read all lines in the file and create one users for one line. Sample file: #action;login,full name title,expire date,project +;gmwen,Bruce Wen QA,04/01/2012,BT +;xxdeng,Shown Deng... (4 Replies)
Discussion started by: crest.boy
4 Replies

6. Shell Programming and Scripting

matching user input to a text file loop?

until do read -p "Invalid cars. Try againa" cars1 done Ok i have the above code, im getting users input and if it doesnt match in the file the user has to try again untill its correct But when i run this it gives me an error saying ./Cars.bash: line 43: (2 Replies)
Discussion started by: gangsta
2 Replies

7. Shell Programming and Scripting

read command (input) inside the while loop

Hi, 'read' command is not working inside the while loop, How can I solve this? Rgds, Sharif. (2 Replies)
Discussion started by: sharif
2 Replies

8. Shell Programming and Scripting

Loop until user input is correct

Hello, i know how to retrieve a user input (read), and how to manage the different options (case statement). But... could anybody show me a script that, if the user option is incorrect, don't allow to continue the excution, i.e., if the value entered is not 1 or 2, the script shows a question. ... (2 Replies)
Discussion started by: aristegui
2 Replies

9. UNIX for Dummies Questions & Answers

read user input from within a wile loop that is being fed from below

hi! i need to do a ksh script that uses a wile loop that is fed form below while read line do some things done < myfile inside the while loop i need to read user input to ask the user what he wants to do, but "read" reads the file, and not the standard input while read line do ... (2 Replies)
Discussion started by: broli
2 Replies

10. Shell Programming and Scripting

input inside while read loop

Hi all Does anyone have a script that will allow me to stop inside a while read loop. I want to pause the loop until a enter is pressed. e.g. While read line do echo something if LINECOUNT > 40 then read ENTER?"PRESS ENTER TO CONT..." ... (3 Replies)
Discussion started by: jhansrod
3 Replies
Login or Register to Ask a Question