|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
> where_am_i=$((cd $(dirname $0) && pwd))
> > Or is there any other/easier way? How about where_am_i=$(dirname $0) |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|