PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Getting script location, trouble with spaces
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Getting script location, trouble with spaces

Réponse
 
LinkBack Outils de la discussion
Vieux 07/01/2008, 21h28   #1
Andre-John Mas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Getting script location, trouble with spaces

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


  Réponse avec citation
Vieux 07/01/2008, 21h35   #2
Andre-John Mas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting script location, trouble with spaces

On Jan 7, 4:28 pm, Andre-John Mas <andrejohn....@gmail.com> 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
  Réponse avec citation
Vieux 08/01/2008, 08h57   #3
Stephane Chazelas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting script location, trouble with spaces

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h57.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09248 seconds with 11 queries