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 > Finding absolute path of a script from within
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Finding absolute path of a script from within

Réponse
 
LinkBack Outils de la discussion
Vieux 14/03/2008, 08h27   #1
Jeenu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Finding absolute path of a script from within

Hi,

I'm wondering what would be the best way to get the absolute path of
script, within it. 'basename', 'dirname' and 'pwd' don't always give
the same output, since it depends on where you execute the script
from. Any thoughts?

Thanks
Jeenu
  Réponse avec citation
Vieux 14/03/2008, 09h40   #2
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Finding absolute path of a script from within

Jeenu wrote:
>
> I'm wondering what would be the best way to get the absolute path of
> script, within it. 'basename', 'dirname' and 'pwd' don't always give
> the same output, since it depends on where you execute the script
> from. Any thoughts?


echo $PWD

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
  Réponse avec citation
Vieux 14/03/2008, 09h49   #3
pk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Finding absolute path of a script from within

Jeenu wrote:

> Hi,
>
> I'm wondering what would be the best way to get the absolute path of
> script, within it. 'basename', 'dirname' and 'pwd' don't always give
> the same output, since it depends on where you execute the script
> from. Any thoughts?


basename and dirname are simple text processing tools, which know nothing
about the filesystem. pwd, on the other hand, usually prints correct
information.
However, it's not clear whether you want to know the absolute path the
script is using as its current working dir, or the absolute path where the
script text file is located.

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
  Réponse avec citation
Vieux 14/03/2008, 13h36   #4
Jeenu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Finding absolute path of a script from within

On Mar 14, 1:49 pm, pk <p...@pk.pk> wrote:
> Jeenu wrote:
> > Hi,

>
> > I'm wondering what would be the best way to get the absolute path of
> > script, within it. 'basename', 'dirname' and 'pwd' don't always give
> > the same output, since it depends on where you execute the script
> > from. Any thoughts?

>
> basename and dirname are simple text processing tools, which know nothing
> about the filesystem. pwd, on the other hand, usually prints correct
> information.
> However, it's not clear whether you want to know the absolute path the
> script is using as its current working dir, or the absolute path where the
> script text file is located.
>
> --
> All the commands are tested with bash and GNU tools, so they may use
> nonstandard features. I try to mention when something is nonstandard (if
> I'm aware of that), but I may miss something. Corrections are welcome.


Yeah, my intention was to get the absolute path where the script lies.
I can't use 'pwd' command inside the script, since user might be
executing it from some other directory, in which case it will output
what directory user is in. I figured out this command could do it:

where_am_i=$((cd $(dirname $0) && pwd))

Or is there any other/easier way?
  Réponse avec citation
Vieux 14/03/2008, 13h45   #5
Florian Kaufmann
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Finding absolute path of a script from within

> where_am_i=$((cd $(dirname $0) && pwd))
>
> Or is there any other/easier way?


How about

where_am_i=$(dirname $0)
  Réponse avec citation
Vieux 14/03/2008, 13h55   #6
Jeenu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Finding absolute path of a script from within

On Mar 14, 5:45 pm, Florian Kaufmann <sensor...@gmail.com> wrote:
> > where_am_i=$((cd $(dirname $0) && pwd))

>
> > Or is there any other/easier way?

>
> How about
>
> where_am_i=$(dirname $0)


Yeah, that's what I initially tried in my script. If my script is
executed like
../my_script
OR like
.../../dir/myscript
where_am_i will only contain "." and "../../dir" respectively which
are not absolute
  Réponse avec citation
Vieux 14/03/2008, 14h11   #7
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Finding absolute path of a script from within

Jeenu wrote:
> On Mar 14, 5:45 pm, Florian Kaufmann <sensor...@gmail.com> wrote:
>>> where_am_i=$((cd $(dirname $0) && pwd))
>>> Or is there any other/easier way?

>> How about
>>
>> where_am_i=$(dirname $0)

>
> Yeah, that's what I initially tried in my script. If my script is
> executed like
> ./my_script
> OR like
> ../../dir/myscript
> where_am_i will only contain "." and "../../dir" respectively which
> are not absolute


How about:

cd $(dirname $0) && DIR="$PWD" && cd - >/dev/null
echo $DIR

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
  Réponse avec citation
Vieux 14/03/2008, 14h14   #8
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Finding absolute path of a script from within

2008-03-14, 05:36(-07), Jeenu:
[...]
> where_am_i=$((cd $(dirname $0) && pwd))
>
> Or is there any other/easier way?


That's really an approximation.

where_am_i=$(
cd -P -- "$(dirname -- "$0")" && pwd -P
)

will work in most cases.

case where it may not work is if the script is started as:

sh myscript.sh

instead of just:

myscript.sh

and myscript.sh happens not to be in the current directory in
which case it's being looked up in $PATH. To work around that,
you can do something like:

where_am_i=$(
if [ -e "$0" ]; then
prog=$0
else
prog=$(command -v -- "$0") || exit
fi
cd -P -- "$(dirname -- "$prog")" && pwd -P
)

This may still be fooled if the script is not executable and
your shell is not standard compliant (like bash) and finds it in
$PATH. (sh myscript, if myscript doesn't exist in the current
directory is meant to search for an *executable* myscript in
$PATH, bash misses the "executable" requirement, and command -v
only reports executables).

It will also not work if the directory name ends in newline
characters.

--
Stéphane
  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 22h26.


Édité par : vBulletin® version 3.7.2
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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14027 seconds with 16 queries