Bash script idea using cUrl -- possible?

I have an alias already in my .bash_profile to download files using cUrl's -o (output to file, user provides the file name) option. I find I'm using it quite a bit, so I wanted to write a script to run "curl -o", taking the necessary inputs - file name and URL from which to download - and then returning to a "parked" state like some python scripts I've seen do, ready to take in another filename and URL. The script should also, on reading a 'buzzword' (not "quit" -- that's too literal and it might be confused by the shell), put up one last echo and exit as normal.

Then my imagination kicked in and I started to wonder, also, if it wouldn't be possible to have the script log its own activity to an external file as soon as a download is complete, naming the file with the date and time created along with its own name or some generic 'tag' such as "curldl-activity-021810-223516H.txt", and keep logging to the same file until the exit word or phrase was entered.

I have a pretty firm idea of how to write the part of the script that takes in the name and URL and invokes cUrl to do the downloading. That would probably best be handled in a function, I'm thinking. I'm a bit sketchy on setting the conditional for an exit buzzword, and while I've written scripts and executed commands to send data to outside files, where to put such a command in this script is something I'm sure I've learned somewhere but can't recall, at any rate.

So again, here's me picking the brains of the gurus and asking for advice.

BZT

Here is a very rough outline of one way to do it. It works but needs a lot of polishing and may not be the method you want to use. I didn't fool around with the logfile name or any error checking.

while true 
do echo "Gimme me something to download"
read LINK
[ "$LINK" = "stop" ] && exec echo "I'm exiting"
echo "File name?"
read NAME
curl -o "$NAME" "$LINK" && echo "$NAME" "$LINK" > file.log
done

"while true; do [stuff]; done" is an infinite loop. "read" just sits there and waits for you to input something, which it then puts in the LINK variable. If that something is "stop," it will say "Im exiting" and exit. The exec command is what makes it exit in this example, it runs one command then exits. For anything except "stop," it asks for a file name, then curl gets the link and puts it in that file. If curl succeeds, it echos the name and link to a logfile.

edit to add: I'm certainly not a guru and this is just a rough example. Don't take it as The Way Things Should Be Done.

I guessed that you folks would want some "code o' my own," so I wrote some.
Did some double-checking on the rules of parameter extraction -- last time I did anything of the kind was in AppleScript three or four years ago.

So here's my script as it stands now. The echos about a correctly-formatted web address are the ones I intend to replace with the cUrl commands.

#!/bin/bash

function whatchagot() {

echo -ne "Where is the file coming from?\n"
read stringZ
dimple=$(echo ${stringZ:0:5})
if [ $dimple != "http:" ]
then
	echo -ne "That was not a web address.\n"
	echo -ne "I can't fill in your blanks like a web browser can, you know!\n"
else
	echo -ne "Very good.\n"
	echo -ne "You know what a web address is!\n"
fi
}

echo -ne "What is your file to be called?\n"
read theName
if [ $theName != "done" ]
then
	whatchagot
else
	echo -ne "Alright. I quit!\n"
fi

I'm taking this one step at a time. I have the function ready, allowing for some substitution of commands. Thanks for the "while true" hint. I'll add your && echo "$NAME" "$LINK" > file.log code later on.

BZT

---------- Post updated 27th Feb 2010 at 13:54 ---------- Previous update was 26th Feb 2010 at 22:42 ----------

Still a little wary of applying the "while true" loop, I made some modifications to the script as last posted, dressing up the output to the log file and making sure that the exit commands were working.

#!/bin/bash


function whatchagot() {

thisFile=$theName
echo -ne "Where is the file?\n"
read thisSource
dimple=$(echo ${thisSource:0:5})
if [ $dimple != "http:" ]
then
	echo -ne "I'm exiting.\n"
else

But by avoiding the "while true" loop to this point, I may have hung myself by my own petard, as now, predictably, every time the script is run it creates a new log file with a new date & time as part of the name.

logtime=$(date "+%d.%m.%Y-%H.%M")
logname="curlactivity-$logtime.log"
touch $logname

echo -ne "What is your file to be called?\n"
read theName
if [ $theName != "done" ]
then
	whatchagot
else
	echo -ne "Alright. I quit!\n"
fi

I like the kind of output I'm getting, nicely formatted etc.:

27.02.2010 @ 13:37:54: tuxubuntu-cute.jpg came from http://www.cogite.info/wallpaper/ and was called Christmas_Tux_Ubuntu_1440-900.jpg

But it looks like I'll have to "dive into the while pool" to make these into real log files instead of "results" redirects with a single string of output to them. Which is not what I meant for them to be.

Would anyone advise abandoning the function part and just bulldozing through, beginning to end, with what I have now? Does that tend to work better when while loops are applied to scripts like this? Or am I asking two incompatible questions?

BZT

My complete script as of this post time:

#!/bin/bash


function whatchagot() {

thisFile=$theName
echo -ne "Where is the file?\n"
read thisSource
dimple=$(echo ${thisSource:0:5})
if [ $dimple != "http:" ]
then
	echo -ne "I'm exiting.\n"
else
	GRABTIME=$(date "+%d.%m.%Y @ %H:%M:%S")
	thisPlace=`echo $thisSource | rev | cut -d / -f2- | rev`
	thisName=`echo ${thisSource##*/}`
	curl -o "$thisFile" "$thisSource" && echo -ne "$GRABTIME": "$thisFile" came from "$thisPlace/" and was called "$thisName\n">> $logname
fi
}

logtime=$(date "+%d.%m.%Y-%H.%M")
logname="curlactivity-$logtime.log"
touch $logname

echo -ne "What is your file to be called?\n"
read theName
if [ $theName != "done" ]
then
	whatchagot
else
	echo -ne "Alright. I quit!\n"
fi

I "dove in" and put the while true where it works. I eliminated the function, but added a timestamp as an echo redirect to the log file, just so I'd know when each log was started. So if anyone would like to use this script with their cUrl install (I'm using curl 7.20.0 as I write this), feel free to download the attachment.

BZT