sys.argv index out of range error

sys.argv index out of range error

Post by Mile » Sat, 15 Sep 2007 06:00:59



Assuming your /bin/sh is actually the Bourne-again shell, this excerpt
from the Bash manual explains what's happening ("source" is a synonym
for "."):

"""
. filename [arguments]
Read and execute commands from the filename argument in the current
shell context. [...] If any arguments are supplied, they become the
positional parameters when filename is executed. Otherwise the
positional parameters are unchanged.
"""

So "source [script] [args]" clobbers the parameters of the *current*
shell context. So any of these should work:
* use "source init.sh" to use the same shell context;
* use "./init.sh $1" to use a new context; or
* save the needed parameters in other variables before clobbering them.

-Miles
 
 
 

sys.argv index out of range error

Post by Ian Clar » Sat, 15 Sep 2007 06:06:40


Taken from `help set` (at the bottom of the page; emphasis mine):
Using + rather than - causes these flags to be turned off. The
flags can also be used upon invocation of the shell. The current
set of flags may be found in $-. *The remaining n ARGs are positional
parameters and are assigned, in order, to $1, $2, .. $n. If no
ARGs are given, all shell variables are printed.*

Short answer: change set to export.

Slightly longer answer:
What this invocation of set is doing is setting the shell variable PATH
to the value you give *and* setting the $1 variable to the same value
and removing all other numbered variables. So if you put a print $2
after `source init.sh $1` in bootstrap.sh you'll notice it's blank, as
set is removing it.

The reason this happens when you source the file rather than just
calling it is that when you source a file it runs in the current shell.
So calling set then wipes out all the other numbered variables. When
simply calling the script it creates a subshell where those things still
happen, just not in the parent shell.

Ian