LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-05-2007, 09:16 AM   #1
caps_phisto
Member
 
Registered: Sep 2004
Location: NH
Distribution: FC6, FC1-4, RH9, Gentoo 2006.0/1, Slackware 10.1/2,11, Vector SOHO 5.0.1
Posts: 237

Rep: Reputation: 30
while READ loop problem


I have setup a script that reads input from a text file using the following type of loop:
Code:
while read LINE; do...done
However, I wish to prompt the user for input within that loop doing something similar to this:
Code:
 while read LINE; do
echo "Enter your name:  "
read MYNAME 
done
But when I run the script it won't pause on the input prompt. Instead it simply just bypasses it and stores nothing in the variable. Is there something I am missing?

Thanks in advance.
 
Old 02-05-2007, 09:50 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The problem here is that you are trying to read both the text file and the user input from standard input. You have to separate them in some way. One idea is to assign a file descriptor to the text file and leave the standard input for user input, e.g.
Code:
#!/bin/bash
exec 3<>$1
while read -u 3 LINE; do
echo -n "Enter your name:  "
read MYNAME
done
In this example the file descriptor 3 is assigned to the text file passed as argument to the script itself, while the user input comes from file descriptor 0 (standard input). You have to execute the script as in:
Code:
./name_of_the_script name_of_text_file
without the < redirection

Last edited by colucix; 02-05-2007 at 09:52 AM.
 
Old 02-05-2007, 09:57 AM   #3
caps_phisto
Member
 
Registered: Sep 2004
Location: NH
Distribution: FC6, FC1-4, RH9, Gentoo 2006.0/1, Slackware 10.1/2,11, Vector SOHO 5.0.1
Posts: 237

Original Poster
Rep: Reputation: 30
Excellent! That worked like a charm! THANKS!
 
Old 02-05-2007, 09:58 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You're welcome!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
BASH: Read entire file line in for loop clinton Programming 16 04-18-2013 12:06 PM
Problem with C loop ! linuxlover1 Programming 3 11-02-2006 03:32 PM
problem with do while loop. RHLinuxGUY Programming 12 02-06-2006 03:44 PM
loop problem andym Linux - Newbie 0 06-05-2003 05:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration