How can I use curl in this shell script? (it uses grep & echo)

Hi everyone,
This is my basic shell script,

do shell script "echo " & quoted form of theString & " | /usr/bin/grep -o -E '[sS][0-9]{1,2}E[0-9]{1,2}'"

It all works fine, but can I also use it to read a downloaded file ? Wikipedia gave me this to download files,

do shell script "curl -o wikipedia.html [url=][/url]"

But I don’t know how to use them both.
What I actually want is to download the file (like the second script), and then let grep display the searchstring. But I don’t know how to use it…
Oh, the file has to be saved, not only read. (curl -o)


Instead of using echo to send data to grep, use input redirection (“<”) to arrange for grep to read from the saved file.
If you end up having multiple saved files that you want to search all at once, you may want to investigate naming the files as grep arguments and using “-h” to suppress the filename that grep shows before matches when searching multiple files.

set curlSavePath to "wikipedia.html"
set curlURL to "[url=][/url]"

set curlCmd to "curl -o " & quoted form of curlSavePath & " " & quoted form of curlURL
set grepCmd to "/usr/bin/grep -o -E '[sS][0-9]{1,2}E[0-9]{1,2}' <" & quoted form of curlSavePath


-- If you need to fetch and grep separately:
do shell script curlCmd
-- do other stuff
do shell script grepCmd

(* OR *)

-- If you always do the grep immediately after the fetch (and always fetch immediately before the grep):
do shell script curlCmd & " ; " & grepCmd

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 4 Public Beta (4528.17)
Operating System: Mac OS X (10.4)

Ah thanks :D, it actually uses sed too… (so curl, echo, sed & grep :slight_smile: ). I’ll see if I can clean it up a bit. As you said, echo isn’t needed.


Why do you use “quoted form of curlURL”. An URL doesn’t contain spaces right ?

Thanks :slight_smile:

Spaces are not the only character that need to be quoted for the shell.

Others include asterisk, question mark, square brackets, curly braces, parentheses, semicolon, ampersand, greater than, less than, backslash, dollar sign, (leading) tilde, single quotes, double quotes, back quotes, and pipe (vertical bar).

Not all of these are strictly valid in URLs (they should be URI encoded (“percent escaped”)), but I think some of them are valid (at least question mark and ampersand, which are often used to supply request parameters).

Unless you specifically intend to have the shell apply special interpretation to all of the characters in a string value, it is best to quote the string value before using it in the shell.

Since the Wikipedia URL you gave did not seem likely to be the actual URL that you were going to use, I thought it would be best to show how to properly quote a string value for the shell.

You should also be aware that curl might require additional quoting/escaping (at least for square brackets and curly brackets; unless you use –globoff). Such quoting would be done as URI encoding before applying quoted form of.

Thanks, I didn’t know that about the “?” & “-” etc. My URLS are always the same, never including such characters but for the safety, I’ll quote them.

The Wikipedia URL was just a copy paste from their cURL page, I won’t use wikipedia to fetch data from.

Anyways, thank you again :slight_smile: