Re: do shell script undesired behaviour (!)
Re: do shell script undesired behaviour (!)
- Subject: Re: do shell script undesired behaviour (!)
- From: "Mark J. Reed" <email@hidden>
- Date: Mon, 15 Nov 2010 09:32:23 -0500
On Mon, Nov 15, 2010 at 4:39 AM, julifos <email@hidden> wrote:
> do shell script "cd /BAD/PATH; ls -la"
> Previously, if you concatd various commands in the same do shell script, the
> first of them causing errors would cause also an applescript error, so
> subsequent commands wouldn't be executed (in this sample, "ls -la").
I don't believe that has ever been true.
> I think it's the same in a Terminal window, so maybe AS can't do nothing at
> this point (?)
No, that's not the case. The exit code of a shell command string is
always the exit code of the *last* command in that string. If you
want a failure early on to prevent execution of later commands, you
must request that behavior explicitly by using the "&&" operator in
lieu of the semicolon, as Axel mentioned:
do shell script "cd /BAD/PATH && ls -la"
Which OMM yields "0:40: execution error: sh: line 0: cd: /BAD/PATH: No
such file or directory (1)".
> I also noticed that now the default path in do shell script is the root:
The current working directory for do shell script is not specified;
you should always cd to where you want to be (and make sure the cd
succeeded). As far as I can recall, it's always been "/" for scripts
run via Script Editor. But if you run the script via osascript, then
the current working directory is inherited:
$ cd /tmp && osascript -e 'do shell script "pwd"'
/tmp
> do shell script "cd /bogus/path; rm -rf"
Ouch. (I assume there was a '*' at the end.) That is why shell
scripts doing such things should always take advantage of '&&'. The
damage from the command above was hopefully minimized by the fact that
you weren't running it as root.
Always check your working directory before doing a wildcard rm.
Here's an unfortunate session from my Unix sysadmin past:
$ cd /tmp
$ su -
Password:
# rm -rf *
Things to note: the cd did in fact, succeed, and I was in /tmp as me.
Then I ran the su command to become root (this being before sudo use
was widespread). Unfortunately, I included the '-' parameter to su,
which gave me root's login environment - including a home directory of
/ (something else that's not usually true these days; on 10.6, root's
home directory is /var/root, but the reason the account is called
"root" is because their home directory was originally the root of the
filesystem). So the subsequent rm trashed the system, necessitating a
reinstall of the OS. (From tape, which is a lot slower than CD, but at
least it was less annoying than swapping a zillion floppy disks.)
--
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:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden