Getting script location, trouble with spaces
am 07.01.2008 22:28:09 von Andre-John Mas
Hi,
I am writing a script and in order to run properly it needs to know
where it is located, in order to be able to find its resources. It
works fine, until the path has a space in it. I having a little
trouble resolving the issue.
#!/bin/sh
cd `dirname "$0"`
pwd
java -cp lib/myjar.jar:lib/icu4j-3_8.jar osj.main.MainClass
If at the route I have two folders:
/Applications
/Applications (Java)
and my script is in the "Applications (Java)" folder, then I always
end up in the Applications folder. At the same time if I call:
dirname "/Applications (Java)/MyApp/myapp.sh"
then this works as should.
Can anyone suggest a solution to my problem?
Andre
Re: Getting script location, trouble with spaces
am 07.01.2008 22:35:52 von Andre-John Mas
On Jan 7, 4:28 pm, Andre-John Mas wrote:
> Hi,
>
> I am writing a script and in order to run properly it needs to know
> where it is located, in order to be able to find its resources. It
> works fine, until the path has a space in it. I having a little
> trouble resolving the issue.
>
> #!/bin/sh
>
> cd `dirname "$0"`
> pwd
> java -cp lib/myjar.jar:lib/icu4j-3_8.jar osj.main.MainClass
>
> If at the route I have two folders:
>
> /Applications
> /Applications (Java)
>
> and my script is in the "Applications (Java)" folder, then I always
> end up in the Applications folder. At the same time if I call:
>
> dirname "/Applications (Java)/MyApp/myapp.sh"
>
> then this works as should.
>
> Can anyone suggest a solution to my problem?
>
> Andre
Never mind, solved the problem. The issue wasn't with dirname, but
with cd since I was missing some quotes:
#!/bin/sh
cd "`dirname "$0"`"
pwd
java -cp lib/myjar.jar:lib/icu4j-3_8.jar osj.main.MainClass
Andre
Re: Getting script location, trouble with spaces
am 08.01.2008 09:57:57 von Stephane CHAZELAS
On Mon, 7 Jan 2008 13:28:09 -0800 (PST), Andre-John Mas wrote:
[...]
> I am writing a script and in order to run properly it needs to know
> where it is located, in order to be able to find its resources. It
> works fine, until the path has a space in it. I having a little
> trouble resolving the issue.
>
> #!/bin/sh
Beware!
#! /path/to/the/posix/sh --
or
nothing at all.
As on some systems, the POSIX sh is not in /bin and /bin/sh is
the deprecated Bourne shell.
>
> cd `dirname "$0"`
cd -P -- "$(dirname -- "$0")"
It still won't work if the dirname of $0 ends in newline
characters because of a flaw in shells design.
--
Stephane