argv[0] manipulation

Robin Munn rmunn at pobox.com
Fri Dec 13 14:49:15 EST 2002


P_spam_ at draigBrady.com <P_spam_ at draigBrady.com> wrote:
> Robin Munn wrote:
>> P_spam_ at draigBrady.com <P_spam_ at draigBrady.com> wrote:
>> 
>>>Donn Cave wrote:
>>>
>>>>Quoth P_spam_ at draigBrady.com:
>>>>...
>>>>| I had assumed that the shell passes what you type for executables
>>>>| in the $PATH directly in argv[0]. This is silly of course and
>>>>| it just passes appropriate_path_entry+'/'+what_you_type so you can't
>>>>| distinguish from argv[0] whether a program was found in the $PATH
>>>>| or explicitly specified by the user.
>>>>
>>>>Depends on the shell.  Bash and rc do one thing, ksh does another.
>>>
>>>Well I'll be darn diddley arned. tcsh does the same as ksh.
>>>I.E. if the prog is found in $PATH then the appropriate dir
>>>is not prepended to argv[0].
>> 
>> *Boggle*. Do you mean that tcsh and ksh, if "myscript.py" is found in
>> $PATH, simply pass "myscript.py" as argv[0] instead of passing
>> "/home/myusername/bin/myscript.py"? Then how am I supposed to, say,
>> check for a config file in my script's directory? Or find the data file
>> locations based on my script's location?
> 
> Yes that is a problem. I tried tcsh on RH7.3 and ksh on FreeBSD with
> the same result (path is not prepended).

Strange. *VERY* strange. Here's what happens when I try this:

[rmunn at localhost] /home/rmunn> $ telnet localhost
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Red Hat Linux release 7.3 (Valhalla)
Kernel 2.4.18-3 on an i686
login: rmunn
Password:
Last login: Fri Dec 13 11:50:34 from xxxxxxxx.xxxxxxxx.xxx
[rmunn at localhost] /home/rmunn> $ tcsh
[rmunn at localhost ~]$ ls -l ~/bin
-rwxr-xr-x    1 rmunn  users        50 Dec 12 08:26 tryme.py
[rmunn at localhost ~]$ which tryme.py
/home/rmunn/bin/tryme.py
[rmunn at localhost ~]$ cat /home/rmunn/bin/tryme.py
#!/usr/bin/python

import os, sys

print sys.argv
[rmunn at localhost ~]$ tryme.py
['/home/rmunn/bin/tryme.py']
[rmunn at localhost ~]$ ~/bin/tryme.py
['/home/rmunn/bin/tryme.py']
[rmunn at localhost ~]$ /home/rmunn/bin/tryme.py
['/home/rmunn/bin/tryme.py']
[rmunn at localhost ~]$ python tryme.py
python: can't open file 'tryme.py'
[rmunn at localhost ~]$ python ~/bin/tryme.py
['/home/rmunn/bin/tryme.py']
[rmunn at localhost ~]$ python /home/rmunn/bin/tryme.py
['/home/rmunn/bin/tryme.py']
[rmunn at localhost ~]$ set
[snip]
version tcsh 6.10.00 (Astron) 2000-11-19 (i386-intel-linux) options 8b,nls,dl,al,kan,rh,color,dspm



On my test on a RH7.3 box (note login banner), tcsh always prepends the
path, no matter how the Python script is invoked. What version of tcsh
is running on the RH7.3 box you tested on?

I don't have access to a FreeBSD box; could you provide a brief session
log demonstrating the problem on FreeBSD?

-- 
Robin Munn <rmunn at pobox.com>
http://www.rmunn.com/
PGP key ID: 0x6AFB6838    50FF 2478 CFFB 081A 8338  54F7 845D ACFD 6AFB 6838



More information about the Python-list mailing list