LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   get shell scripts own directory name (https://www.linuxquestions.org/questions/linux-newbie-8/get-shell-scripts-own-directory-name-936187/)

mvairavan 03-24-2012 09:14 AM

get shell scripts own directory name
 
Hi,

I want to get a shell scripts own directory name...

pwd might not help here, coz that script can be executed from any wr else as well...

i can cut it from $0 but this will go wrong if that script is executed from inside that directory..

so wats the easiest way to find that scripts own directory name??

Thanks in advance!!

TB0ne 03-24-2012 10:07 AM

Quote:

Originally Posted by mvairavan (Post 4635111)
Hi,
I want to get a shell scripts own directory name...

pwd might not help here, coz that script can be executed from any wr else as well...i can cut it from $0 but this will go wrong if that script is executed from inside that directory.. so wats the easiest way to find that scripts own directory name??
Thanks in advance!!

Spell out your words, and quit using that text-speak garbage.

This very much sounds like a homework question...what have you done/tried so far? This:
Code:

OWNDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
...will mostly work. I'll leave it to you to figure out how to see if you're referencing a symlink or not, if it matters.

catkin 03-24-2012 10:08 AM

Not always possible. More here.

mvairavan 03-24-2012 01:31 PM

Quote:

Originally Posted by TB0ne (Post 4635147)
Spell out your words, and quit using that text-speak garbage.

Was that in the forum's how to ask a question page? I think i read it and followed that procedure before posting a question!!

And i read some where there, asking users to be polite!!!

Quote:

Originally Posted by TB0ne (Post 4635147)
This very much sounds like a homework question...what have you done/tried so far?

its not a home work stuff.
i was doing if [`dirname $0` not equals "." ] then dirname $0 ;else pwd;
but i came up with a testcase of */Actual_dir/subdir/../test.sh , so my code fails here..

Quote:

Originally Posted by TB0ne (Post 4635147)
This:
Code:

OWNDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
...will mostly work. I'll leave it to you to figure out how to see if you're referencing a symlink or not, if it matters.

So basically u are asking me to cut from $0 and do an internal cd and get pwd??
This satisfies that test case. good on that.

if some one creates a symbolic link of this executable and executes it using that symbolic link, is that what u asked?
i think i will use if [ -z `readlink $0` ] then $0 else readlink $0, something like that...

Somehow i dont feel better in doing this with $0, i don believe on that $0's value.. Anyways this has solved my prob as of now...
Thanks!!

TB0ne 03-24-2012 04:52 PM

Quote:

Originally Posted by mvairavan (Post 4635253)
Was that in the forum's how to ask a question page? I think i read it and followed that procedure before posting a question!!
And i read some where there, asking users to be polite!!!

Think what you like, but posting text-speak isn't nice for ANYONE. And if you don't like the answers, you should first consider what you did to have someone reply like that to you. Did you look around the forum, and see how others respond to text-speak? Or do you think you can just do whatever you'd like, and everyone else has to be nice to you about it??
Quote:

its not a home work stuff. i was doing if [`dirname $0` not equals "." ] then dirname $0 ;else pwd; but i came up with a testcase of */Actual_dir/subdir/../test.sh , so my code fails here..

So basically u are asking me to cut from $0 and do an internal cd and get pwd?? This satisfies that test case. good on that.
if some one creates a symbolic link of this executable and executes it using that symbolic link, is that what u asked?

i think i will use if [ -z `readlink $0` ] then $0 else readlink $0, something like that...
Somehow i dont feel better in doing this with $0, i don believe on that $0's value.. Anyways this has solved my prob as of now...
I also asked you to spell out your words...yet you continue to use text-speak. If you can't be bothered typing out a word, why should anyone here be bothered answering?

And if you read the man page on the "cd" command, you'll see a way to handle symbolic links.

AnanthaP 03-24-2012 08:27 PM

http://www.linuxquestions.org/questi...mmands-852230/

In this thread also, we can see evidence of not understanding that "non native English speaking persons" don't know what's correct "speak" and what's not. (Apart from - at times - incorrect technical advice).

I think that native English speakers (not all mind you) should be just a little more understanding of others. You see, these others may not be aware that they are writing text-speak-garbage.

Sure, we non native English speaking persons will try to improve.

OK

TB0ne 03-25-2012 10:56 AM

Quote:

Originally Posted by AnanthaP (Post 4635467)
http://www.linuxquestions.org/questi...mmands-852230/

In this thread also, we can see evidence of not understanding that "non native English speaking persons" don't know what's correct "speak" and what's not. (Apart from - at times - incorrect technical advice).

I think that native English speakers (not all mind you) should be just a little more understanding of others. You see, these others may not be aware that they are writing text-speak-garbage.

Sure, we non native English speaking persons will try to improve.

Sorry, but this has ZERO to do with native/non-native English speaking persons. The word "you" is spelled the same, no matter if English is your first or fifth language. And "coz", "wr", and "wat" are good examples too. Grammatical errors are one thing...using text-speak is another.


All times are GMT -5. The time now is 05:55 AM.