![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Posts: 2
Rep Power: 0
![]() |
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.listThe $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, |
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() |
__________________
Profanity is the one language that all programmers understand. Check out my Blog <---updated Nov 30 2007! |
|
|
|
|
|
#3 | |
|
Newbie
Join Date: Feb 2005
Posts: 2
Rep Power: 0
![]() |
Quote:
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. |
|
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4
![]() |
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
doneAs usual, my example code is untested; forgive me if it's a brainfart. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|