Unix Shell - Re: Running a script: How to reference the directory the script is located in.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > Re: Running a script: How to reference the directory the script is located in.





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Re: Running a script: How to reference the directory the script is located in.
jhagge@gmail.com

2006-10-24, 1:17 pm

> dir=$(cd $(dirname "$0"); pwd)
>
> This seems to be what I need, but I don't think I fully understand what
> it is doing. For a test, I simply put that line in a new script and
> the next line is:
>
> echo $dir
>
> and when i run the script it returns:
>
> /tmp
>
> The script is not saved in /tmp (it is currently saved on a folder in
> the users desktop directory). I was expecting that path to be
> returned. Setting a variable seems to be the thing to do, I just need
> the script to be able to see where its current location is and set a
> variable accordingly to reflect this path.
>
> So, either it is not working correctly or I don't understand how it is
> supposed to be implemented (i am guessing the latter).


So it seems the reason this wasn't working was because I was using
ScriptGUI to create a Scriptlet. I was able to modify the command so
it would work in the Scriptlet, but decided it wasn't worth the fuss,
so I am not using Scriptlet anymore.

Now I am saving the script with the .command extension and running it.
This is working great except if there is a space in the path to the
script. This seems to mess up the command to set the dir variable.

I would guess most users will be saving this to there desktop, but if
they did happen to put it in a directory with a space in the name, the
variable is set wrong - basically stopping where the space is.

If the path is ~/Desktop/Saved Files/DOGFOOD

the variable gets set to:

~/Desktop/Saved

Any ideas how to work around this?

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com