Re: To err on the exit code of the FIRST cmd of a piped do shell script
Re: To err on the exit code of the FIRST cmd of a piped do shell script
- Subject: Re: To err on the exit code of the FIRST cmd of a piped do shell script
- From: Harald E Brandt <email@hidden>
- Date: Mon, 8 Dec 2003 23:19:56 +0100
+--> Graff wrote 03-12-08:
Why not just split it into 2 "do shell script" commands, saving the
result from one script and then using it in the next. Something
like this:
----------
try
set curlResult to do shell script "curl badURL"
on error
-- handle curl errors
end try
try
do shell script "echo " & curlResult & " | vis"
on error
-- handle other errors
end try
----------
Yes this just moves the problem to the echo command (unless there is
a way to pass text to vis without using a command), but echo is much
less likely to have problems than curl is. It's not a 100% solution
but I believe it should work.
- Graff
+-
1. Would not work in the general case, since the output from curl may
not even be unicode, but having characters above ascii 127, so 'do
shell script' will bail out severely despite a nice download! (You
know the standard error: "Can't make some data into the expected
type...")(What I really have instead of vis, is actually a perl
script that converts to unicode, so all characters above 127 gets
nicely in with the correct Mac encoding.)
2. A really ugly solution, I think, passing the whole download back
into a new do shell, whereupon it is supposed to be echoed, which may
not work since the string may very well have $ and LF and CR
characters etcetera. If I were to chose, I think I would prefer the
third alternative below - that is the one I have working right now,
although I would very much like a better more elegant and "diskless"
solution.
--heb
(original post below)
+--
On Dec 8, 2003, at 9:26 AM, Harald E Brandt wrote:
Consider this toy example:
do shell script "curl badURL | vis"
Without the pipe, the thing would error with error code 6 - very
easy to trap with a try block. With the pipe, however, it does not
error at all! That's because the exit status is the exit status of
the last command specified in the pipeline - basic shell behavior.
But of course I want that line to error since curl is erroring. How
to do that?
In principle, I could redirect (or actually duplicate) stderr to
stdout and parse the result. That's a drag, since stderr from curl
will always output lots of progress text also in normal situations,
and in this particular case I do not want that text - I just want
it to error with the exit code for curl if it has problems.
Another alternative is to redirect stderr from curl to a file and
then read and parse that. But there's got to be a smarter way!?
A third way is to redirect stdout from curl to a file, then connect
the command with '&&' to a command that reads the file and then
deletes it. The '&&' would make the expression to error on the
first failing command, i.e the curl command. But is there a smarter
way to do this that doesn't need to store the result temporarily on
disk?
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.