convert string to list

Cliff Wells clifford.wells at comcast.net
Wed Oct 13 11:39:54 EDT 2004


On Wed, 2004-10-13 at 16:49 +0200, Jochen Hub wrote:

> > #!/bin/bash
> > myscript.py A B C
> 
> The problem is that I want to pass one or two lists to the script, so it 
> should be able to distinguish between
> 
> thescript ["A","B"] ["C","D",E"]
> thescript ["A","B","C"] ["D",E"]
> thescript ["A","B","C","D",E"]

Okay, after considering several alternatives, it occurs to me that,
depending upon the scope of this script, eval might not be a bad way to
go.  There are two main questions you have to ask yourself when deciding
if eval is safe:

1. Where will the arguments come from?  If you are hardwiring them into
the bash script (and the script is properly secured) then I don't think
there's a problem.  If they come from an untrusted source (network data,
etc), then you should by no means use eval.  

2. Is there any chance this script will be run suid?  I know Linux
doesn't allow suid scripts, but that doesn't prevent the script from
being run by a suid executable and thus gaining privileges.

If you feel secure about those two things, then I would say go ahead
with the eval-based solution, since it is clearly the simplest:

#!/bin/bash
script.py "['a', 'b', 'c']" "['d', 'e', 'f']"



#!/usr/bin/python
import sys

arglist = []
for arg in sys.argv[1:]:
    arglist.append(eval(arg))

print arglist


Regards,
Cliff

-- 
Cliff Wells <clifford.wells at comcast.net>




More information about the Python-list mailing list