current dir from .command scripts
Mike Galloy
mgalloy at gmail.com
Sat Aug 4 23:45:35 PDT 2007
On 8/4/07, Hacker Scot <shacker at birdhouse.org> wrote:
> Interestingly, the
>
> echo `dirname $0`
>
> trick gives the full working path for .command scripts that are
> double-clicked, but just "." (current dir) for the same script run
> from the shell. My script is designed to be double clicked, so this
> largely solved it for me, but just in case, I'll have to build in
> some logic to determine whether it's being double-clicked or run from
> the shell, using pwd to get the path if dirname $0 evaluates to "."
Be careful because $0 won't always be "." when called from the shell. It
will be the path used to call the script. So for example, if test.command is
in ~/Desktop and my shell's current directory is ~, when I call the script
with:
~$ Desktop/test.command
then $0 will be "Desktop/test.command" (and `dirname $0` will be "Desktop").
(If I'm in the same directory as the script, then I will get the "." like
you mention.)
When you double-click on a .command file, it is just shorthand for starting
Terminal, calling the script with a full path to the .command file, and then
issuing the exit command. So in this case, $0 is always an absolute path.
-Mike
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /mailman/archive/macosx-talk/attachments/20070805/ba8a5913/attachment.html
More information about the MacOSX-talk
mailing list