LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-12-2005, 08:40 PM   #1
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Rep: Reputation: 15
'./file' meaning in python & shell script


I found the meanings of './file' are different in shell script & python, while in python, it means the same directory the program runs, and in shell, it means the current directory the user is when he/she runs the script.

How can I get the python effect in shell script? When I run the script anywhere, it can find the 'subscripts' that it calls correctly?

Thanks.
 
Old 12-13-2005, 12:09 AM   #2
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
could do this as the first thing in the bash script

cd `dirname $0`

then from elsewhere in the script ./someprog will run someprog in the same directory the script is located.
Note this may still not work exactly as you would expect if the script was run through a symbolic link.
 
Old 12-28-2005, 12:11 AM   #3
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Original Poster
Rep: Reputation: 15
well, it's a good idea. Thank you very much.

And I have made a mistake: Both shell & python have the same meaning of "./file", but I cat get the absolute pathname by os module in python, just like `dirname $0`:

import os
import sys
location = os.path.abspath(os.path.dirname(sys.argv[0]))
 
Old 12-28-2005, 08:44 PM   #4
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Original Poster
Rep: Reputation: 15
I think there are still some problems to use `dirname $0` if you don't execute the script with absolute pathname.

Any good idea?
 
Old 12-28-2005, 09:05 PM   #5
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Actually its important to do this in the script...
1. wd="`pwd`"
2. cd "`dirname "$0"`"
3. programdir="`pwd`"
4. cd "$wd"
This will eliminate the problem of executing the script with a relative path.
`pwd` will always begin with a '/', root path.
So far I haven't found a better way to get the program directory, there could very well be. Also remember if the program is run through a symbolic link then the program directory will end up being the directory the symbolic link is in.
 
Old 12-29-2005, 03:53 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
That will not work when the script is started with:
bash script.sh
./script.sh
 
Old 12-29-2005, 09:52 PM   #7
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Hi,
I'm not sure exactly what won't work,
its probably just my misunderstanding of what the OP wants.
However, all my tests show it does work even the two cases
you showed me above.
It was my understanding his desire was to know the location
of the script, and the code I posted seems to do that,
am I missing something... just wanted to know.

Code:
[0 randyd@entropy ~]$ cat t.sh
#!/bin/sh

wd="`pwd`"
cd "`dirname "$0"`"
programdir="`pwd`"
cd "$wd"

echo "$programdir"

[0 randyd@entropy ~]$ ./t.sh
/home/randyd
[0 randyd@entropy ~]$ bash t.sh
/home/randyd
[0 randyd@entropy ~]$ cd ..
[0 randyd@entropy home]$ randyd/t.sh
/home/randyd
[0 randyd@entropy home]$
 
Old 12-30-2005, 03:32 AM   #8
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Original Poster
Rep: Reputation: 15
I think this is a good method for shell script. Although it's not so convenient, it does work. Thank you.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
shell script + change shell && continue darkRoom Programming 6 02-25-2005 03:50 AM
Looking for addusesr&passwd shell script szklanka Programming 1 02-22-2005 09:37 PM
Shell Script with ssh & scp ErikJohnson Linux - Software 3 03-11-2004 05:53 PM
Shell Script with ssh & scp ErikJohnson Linux - Networking 1 03-11-2004 05:14 PM
File Maintenance in a shell script? mdscudder Linux - General 1 02-20-2003 06:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:38 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