how do i get the path of the filename
# /bin/bash
how do i get the path of the filename e.g /root/a/b/c/d/e/filename is there any command to pas in e.g. command /root/a/b/c/d/e/filename and it will return /root/a/b/c/d/e/ |
This is not a trivial problem.
The simple answer is to code this in the script: Code:
mypath=`dirname $0` However, it will not always work because of the different ways the script can be invoked. Sometimes mypath will show "./" for example. |
thanks jim mcnamara
i'm getting . as the result is there, any other way that will definately return the path? wouldn't mind if it's a script |
It sounds like you want just the pathname, without the filename?
A simple 'pwd' will do this. Or 'echo $PWD'. Or, if the input '/bin/bash' is coming from elsewhere, you can do something like this: Code:
cd `which bash`/.. |
thanks, but not seem to get the result i want
ok situation: script.sh # /bin/sh echo `pwd` location for script.sh: /root/a/b/c/d/e/script.sh at console if i execute it, i'll get the correct result /root/a/b/c/d/e but if i double click script.sh from gnome, the result is /root how do i get the directory of script.sh when double click on it |
Well, that's a whole different beast. I have no idea how Gnome handles something like that (don't use it :)).
As for 'echo `pwd`' though; I can say that it's redundant... 'pwd' itself gives exactly the same output. HTHGL |
"Well, that's a whole different beast. I have no idea how Gnome handles something like that (don't use it )."
anybody else who can help me "As for 'echo `pwd`' though; I can say that it's redundant... 'pwd' itself gives exactly the same output" thanks :) |
try:
a="$(cd $(dirname $0);pwd)" echo $a |
pwd may not be where he xecuted the script from
example Code:
cd /someplace And if the script is exec'ed it gets worse or if somebody does something wierd like this: Code:
cat /anotherplace/myscript | /bin/ksh |
Quote:
dirname: too many arguments Try `dirname --help' for more information. /root |
It is possible, but it's not trivial.
To have it work from gnome (nautilus actually), try this: Code:
#!/bin/bash Code:
#!/bin/bash |
Hko:
your second way worked for both! though i don't really understand, but yeah! thank you very much and also the rest who helped :) |
It's still not 100% complete. E.g. if the current working dir is /home/hko/dummy and the script is in /home/hko/bin. Then you can start the script with:
shell$ ../bin/thescript.sh ...but it will not correctly print the directory. To solve this it becomes quite more complex, unless you have the "realpath" program installed. But AFAIK only Debain includes it, and Debian does not install it by default. |
oh, too bad then:(
and btw, if i copied your second method into / it will return /root when double click |
Sorry about that.
But having the script in / is really an exceptional case, and a bad idea anyways. Does your distribution have the "realpath" program available? This would make it easier. |
All times are GMT -5. The time now is 05:17 PM. |