line to argv transformation

Marko Rauhamaa marko at pacujo.net
Mon Jun 16 06:59:36 EDT 2014


Chris Angelico <rosuav at gmail.com>:

> def shell_split(cmd):
>     return subprocess.check_output("""python -c 'import sys;
> print("\\0".join(sys.argv[1:]))' """+cmd,shell=True)[:-1].split(b"\0")
>
> You'll get back a list of byte strings, in any case. Feel free to pass
> them through a decode operation, or to incorporate a .decode() into
> the above stream, as you wish.

Now you are subject to the quirks of /bin/sh. For example, '*.xyz'
expands itself ('*.xyz') if there is no match in the current working
directory.

Moreover, you need to guard against arguments like

   $(cat ~/.ssh/id_dsa)

which would spill the beans.


Marko



More information about the Python-list mailing list