Re: do shell script "ps -auxwww | grep rsync | grep -v grep"
Re: do shell script "ps -auxwww | grep rsync | grep -v grep"
- Subject: Re: do shell script "ps -auxwww | grep rsync | grep -v grep"
- From: "Mark J. Reed" <email@hidden>
- Date: Fri, 7 Oct 2005 07:51:44 -0400
In UNIX commands, the exit status was originally designed to indicate
whether or not an error occurred - exit status 0 indicates no error,
while anything else indicates an error, and the specific number may or
may not be an indicator of which specific error occurred. Because of
this behavior, the exit status came to be used for all Boolean tests
in shell programming, with exit status 0 meaning "true" and anything
else meaning "false". This in turn led to the creation of commands
such as grep, which perform some sort of boolean test and exit with
code 0 if they find what they're looking for and some other number
(typically 1) if they don't.
It's convenient for shellscripting, because the syntax of e.g. the if
command is just:
if some command to run goes here; then
this gets run if it exited with code 0
else
this gets run if it exited with any other code
fi
So if you were doing this in a shell script instead of AppleScript,
your ps|grep line would be something like this:
if ps auxww | grep rsync | grep -v grep >/dev/null; then
stuff to do if rsync is running
else
stuff to do if rsync isn't running
fi
Of course, AppleScript has no way of knowing that the nonzero code
means "false" rather than "error" in any given case. You could wrap
it in a try/on error block, but it's probably better to just arrange
it such that the shell command line always exits with 0 and lets its
output convey the truth or falseness of the condition to AppleScript.
If the command is already something whose output will do that, then
you can just add an "; exit 0" at the end to make sure the shell exits
with code 0. If you need to know the exit status but don't want an
error to be thrown when it's not zero, put a ";echo $?" at the end,
which will cause the output of "do shell script" to be the exit status
itself.
-- get the process id of rsync, empty string if it's not running, no
error either way
set rsyncPID to (do shell script " ps auxww | grep rsync | grep -v
grep; exit 0")
-- find out if it's running; don't care what the pid is
set rsyncRunning to (do shell script "ps auxww | grep rsync | grep -v
grep >/dev/null; echo $?")
if rsyncRunning is "0" then
-- it's running
else
-- it's not running
end if
On 10/7/05, Michael Heinz <email@hidden> wrote:
>
> The problem is the final grep.
>
> From the man page:
>
> "Normally, exit status is 0 if selected lines are found and 1
> otherwise. But the exit status is 2 if an error occurred, unless the -
> q or --quiet or --silent option is used and a selected line is found."
>
> Basically, that's the way grep works.
>
> On Oct 7, 2005, at 6:05 AM, Simon Forster wrote:
>
>
> >
> > Why does:
> >
> > do shell script "ps -auxwww | grep rsync | grep -v grep"
> >
> > error with "The command exited with a non-zero status" if rsync's
> > not running? Seems to exit fine in terminal (which I know is a
> > different environment). Can I get this to exit gracefully or do I
> > need to trap for the error?
> >
> > TIA
> >
> > Simon Forster
> > _____________________________________________________
> > LDML Ltd, 62 Pall Mall, London, SW1Y 5HZ, UK
> > Tel: +44 (0)70 9230 5244 Fax: +44 (0)70 9230 5247
> > _____________________________________________________
> >
> >
> > _______________________________________________
> > Do not post admin requests to the list. They will be ignored.
> > Applescript-users mailing list (Applescript-
> > email@hidden)
> > Help/Unsubscribe/Update your Subscription:
> > 40comcast.net
> >
> > This email sent to email@hidden
> >
> >
>
> ---
> Porkchop D. Clown
> Balloons, Buffoonery and Balderdash.
>
>
>
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Applescript-users mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
--
Mark J. Reed <email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden