LinuxQuestions.org
Help answer threads with 0 replies.
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 07-18-2007, 09:21 PM   #1
nilathinesh
Member
 
Registered: Nov 2006
Distribution: Fedora 7 & 9, Ubuntu
Posts: 41

Rep: Reputation: 15
Exclamation location path...


hi,

i am newbie... and i am in a need of help in writing BASH script, in which i need to get the current file location path of the script file inside the script file itself.

ie.,

/home/thinesh/desktop/myscript.sh -> inside this script i need to get the location path of the myscript.sh as "/home/thinesh/desktop"
if same script is copied into /home/thinesh/assignment/myscript.sh, and executed there.. then it has to show the path as "/home/thinesh/assignment/".. and if i invoke the myscript.sh from some other directory as "/Lindows/project$ /home/thinesh/assignment/myscript.sh" there also it has to say the path as /home/thinesh/assignment

thanks...

regards,
thinesh
 
Old 07-18-2007, 09:34 PM   #2
bsdunix
Senior Member
 
Registered: May 2006
Distribution: BeOS, BSD, Caldera, CTOS, Debian, LFS, Mac, Mandrake, Red Hat, Slackware, Solaris, SuSE
Posts: 1,761

Rep: Reputation: 80
Hint. What do the following commands return:
Code:
$ echo $CWD
and
Code:
$ pwd
so
Code:
#!/bin/sh

CURRENT_PATH_LOCATION=`pwd`
echo $CURRENT_PATH_LOCATION
 
Old 07-18-2007, 10:56 PM   #3
nilathinesh
Member
 
Registered: Nov 2006
Distribution: Fedora 7 & 9, Ubuntu
Posts: 41

Original Poster
Rep: Reputation: 15
thanks bsdunix..

.. but the "pwd" will return only the present working directory and not the directory where the
script file resides..
also i do not find the command CWD... !?,

sorry..if my explanation... creates confusion..
 
Old 07-19-2007, 12:04 PM   #4
bsdunix
Senior Member
 
Registered: May 2006
Distribution: BeOS, BSD, Caldera, CTOS, Debian, LFS, Mac, Mandrake, Red Hat, Slackware, Solaris, SuSE
Posts: 1,761

Rep: Reputation: 80
Quote:
i do not find the command CWD... !?
Sorry, I must have been thinking of another un*x.

Quote:
"pwd" will return only the present working directory and not the directory where the script file resides..
Your original post said:
Quote:
... i need to get the current file location path of the script file inside the script file itself.
Maybe if you post your script and identify where it's failing, we (or at least me) can get a better idea of what you want.
 
Old 07-20-2007, 12:46 AM   #5
nilathinesh
Member
 
Registered: Nov 2006
Distribution: Fedora 7 & 9, Ubuntu
Posts: 41

Original Poster
Rep: Reputation: 15
ok..let me explain.. another way...

i am now in the directory

/home/thinesh/desktop

myscript.sh is there.

i have written code as

Code:
.
.
export ASSIGN1_PATH=`pwd`/methods
.
.
so when i invoke this myscript.h from current directory, then
my assign1_path will be like "ASSIGN1_PATH=/home/thinesh/desktop/methods"

1). now.. if i switch to directory /Lindows/Project
and from their i invoke the script as

"[thinesh@localhost Project]$ /home/thinesh/desktop/myscript.sh",

after that....assign1_path is setting as "ASSIGN1_PATH=/Lindows/Project/methods" and not the one i want as "ASSIGN1_PATH=/home/thinesh/desktop/methods"

2). then... if i copy my myscript.sh to someother folder like /work/script/ and i switching to folder /Lindows/methods/ and if invoke the script as

"[thinesh@localhost methods]$ /work/script/myscript.sh"
then i need to get the assign1_path as "ASSIGN1_PATH=/work/script/methods"

sorry.. if it is not clear...
 
Old 07-20-2007, 08:06 PM   #6
bsdunix
Senior Member
 
Registered: May 2006
Distribution: BeOS, BSD, Caldera, CTOS, Debian, LFS, Mac, Mandrake, Red Hat, Slackware, Solaris, SuSE
Posts: 1,761

Rep: Reputation: 80
May not be elegant, but what comes to mind is:
Code:
#!/bin/sh

MYSCRIPT_LOC=`find / | grep myscript.sh | sed s/\/myscript.sh//`
echo $MYSCRIPT_LOC
so if myscript.sh is located at /var/tmp/myscript.sh, the above should output /var/tmp.

Is this what your looking for?

Last edited by bsdunix; 07-20-2007 at 08:20 PM.
 
Old 07-21-2007, 01:54 AM   #7
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
FAQ #28
 
Old 07-21-2007, 10:21 AM   #8
bsdunix
Senior Member
 
Registered: May 2006
Distribution: BeOS, BSD, Caldera, CTOS, Debian, LFS, Mac, Mandrake, Red Hat, Slackware, Solaris, SuSE
Posts: 1,761

Rep: Reputation: 80
Quote:
Originally Posted by digiot
Good info, thanks!
 
Old 07-22-2007, 07:42 PM   #9
nilathinesh
Member
 
Registered: Nov 2006
Distribution: Fedora 7 & 9, Ubuntu
Posts: 41

Original Poster
Rep: Reputation: 15
Smile

thanks bdsunix and digiot,

....but i need to .... to understand
 
Old 07-22-2007, 08:33 PM   #10
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
You're both welcome. (It is a nice resource.)
 
  


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
path file location justanto Other *NIX 2 03-10-2007 06:24 PM
Location of GTK Libraries and Path question. thakis SUSE / openSUSE 3 04-30-2006 09:39 AM
How to show your location / path? Echo Kilo Linux - General 2 12-19-2004 01:40 PM
PATH file location corbis_demon Linux - Newbie 1 10-08-2004 05:34 AM
PATH Location Odd_Bloke Linux - Newbie 4 08-27-2003 04:17 PM

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

All times are GMT -5. The time now is 11:07 PM.

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