Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 28th, 2005, 12:50 PM   #1
jmsilver
Newbie
 
Join Date: Feb 2005
Posts: 2
Rep Power: 0 jmsilver is on a distinguished road
Question Problem read'ing from file and user input

I have a simple script which reads lines from a file (using a while-read loop) and asks the user whether to perform an action on each entry. The problem is that when I use read to prompt the user input, the user read gets data from the file read. Here is the code:

#!/bin/bash

[ -f "download.list" ] || exit 1

while read LINE
do
        URL=${LINE#*,}
        TITLE=${LINE%,*}
        echo "Download $TITLE [y/n]: "
        read RESPONSE

        if [ $RESPONSE = 'y' ]; then
               echo "curl -O $URL"
        fi

done < download.list

The $LINE, $URL & $TITLE are set correctly. What happens is that $RESPONSE is set to the next line in 'download.list' rather than prompting the user for input.

Can anyone tell me what I am doing wrong?

Thanks,
jmsilver is offline   Reply With Quote
Old Mar 1st, 2005, 8:57 AM   #2
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
take a look at this: http://www.tldp.org/LDP/abs/html/

your answer lies within.
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Old Mar 1st, 2005, 12:19 PM   #3
jmsilver
Newbie
 
Join Date: Feb 2005
Posts: 2
Rep Power: 0 jmsilver is on a distinguished road
Quote:
Originally Posted by Pizentios
take a look at this: http://www.tldp.org/LDP/abs/html/

your answer lies within.
Thank you for your RTFM post, they are always so helpful. I don't know why you bothered.

I read the section on 'read' I didn't find anything relvant to what I am trying to do - read within a loop of reads - but I will go back and read the rest of the site when I have time.
jmsilver is offline   Reply With Quote
Old May 20th, 2005, 2:35 PM   #4
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 314
Rep Power: 4 mackenga is on a distinguished road
The problem is that the redirect on the loop redirects input to anything in the loop that reads input, not just what you wanted it to. You need to rephrase this loop so that you don't need to redirect on the loop statement; do a priming read and another read within the loop, say. Or,

#!/bin/bash

[ -f "download.list" ] || exit 1

for line in `cat download.list`; do
        URL=${LINE#*,}
        TITLE=${LINE%,*}
        echo "Download $TITLE [y/n]: "
        read RESPONSE

        if [ $RESPONSE = 'y' ]; then
               echo "curl -O $URL"
        fi

done

As usual, my example code is untested; forgive me if it's a brainfart.
mackenga is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:26 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC