LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-24-2012, 09:14 AM   #1
mvairavan
Member
 
Registered: Feb 2011
Posts: 31

Rep: Reputation: 0
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!!
 
Old 03-24-2012, 10:07 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by mvairavan View Post
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.
 
Old 03-24-2012, 10:08 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Not always possible. More here.
 
1 members found this post helpful.
Old 03-24-2012, 01:31 PM   #4
mvairavan
Member
 
Registered: Feb 2011
Posts: 31

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TB0ne View Post
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 View Post
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 View Post
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!!
 
Old 03-24-2012, 04:52 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by mvairavan View Post
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.
 
Old 03-24-2012, 08:27 PM   #6
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
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

Last edited by AnanthaP; 03-24-2012 at 08:29 PM.
 
Old 03-25-2012, 10:56 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by AnanthaP View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Directory paths in shell scripts alienDog Programming 2 11-18-2009 05:10 PM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
Where (in which directory) to store shell scripts ? jeepescu Linux - General 10 01-01-2008 08:01 AM
Shell scripts unable to permanently change directory after exit ernesto_cgf Linux - General 4 12-21-2006 04:08 PM
shell script: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 06:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration