1

I have a shell script that needs to run a python script. The python script may be passed an argument.

python pgm32.py $args

But when the argument passed has an escape sequence or if I encase it in quotes, the python scripts does not recognize it.

For instance say args contains "Open drive" OR Open\ drive then python script's sys.argv=['pgm32.py', '"Open', 'drive"'] OR sys.argv=['pgm32.py', '"Open', 'drive"'] respectively.

But when I call the python script directly from the command line, and pass an argument like "Open drive" or Open\ drive, the script's sys.arv=['pgm32.py', 'Open drive']

How can I fix this issue?

Akshay Kalghatgi
  • 423
  • 1
  • 4
  • 11
  • 3
    Did you try `python pgm32.py "$args"`? – Pablo Díaz Ogni Jun 19 '15 at 21:49
  • what about `$args=""Open Drive""` or maybe `$args="\"Open Drive\""` afaik single quotes do something else ... (or do as suggested in the other comment if args will only ever be one item – Joran Beasley Jun 19 '15 at 21:50
  • Sorry, I meant args literally contains `"Open drive"` or `Open\ drive`. – Akshay Kalghatgi Jun 19 '15 at 21:54
  • 1
    @AkshayKalghatgi Don't do that. Let `args` contain the value you want to pass, and not the shell syntax a script would use to expand to that value. – that other guy Jun 19 '15 at 21:55
  • If I just pass `Open drive`, it interprets it as 2 arguments. Actually Open drive is a folder name. There are other arguments that will be passed but they are one word arguments. The destination folder for my python script will be user specified. So it may contain multiple words. So I need `Open drive` (just an example) read as a single argument. – Akshay Kalghatgi Jun 19 '15 at 22:00
  • 2
    That's why you should quote the variable, it prevents it from being split. – Barmar Jun 19 '15 at 22:03
  • @pablo I can't use `python pgm32.py "$args" ` because my shell script also passes has optional parameters to the python script. `-d ` is used to specify the output directory – Akshay Kalghatgi Jun 19 '15 at 22:03
  • 3
    See http://stackoverflow.com/questions/13365553/setting-an-argument-with-bash for how to deal with this. – Barmar Jun 19 '15 at 22:04

3 Answers3

0

It's really quite simple. Quote the argument in the shell command:

$ python pgm32.py "$args"

You already did this yourself:

But when I call the python script directly from the command line, and pass an argument like "Open drive" or Open\ drive, the script's sys.arv=['pgm32.py', 'Open drive']

Which worked because in these lines you protected the space from shell word splitting; in the other examples you didn't. It is all about the shell and has nothing to do with Python.

msw
  • 42,753
  • 9
  • 87
  • 112
-1

This worked:

eval python pgm32.py $args

More details on Setting an argument with bash Thanks @Barmar.

Community
  • 1
  • 1
Akshay Kalghatgi
  • 423
  • 1
  • 4
  • 11
-1

Assuming this is Bash, you could use an array variable.

declare -a args                      # Make args an array.
args=("Open Drive" -d output_dir)    # Assign three elements.
python pgm32.py "${args[@]}"         # Pass to pgm32.py as array.
Nathan Grigg
  • 724
  • 5
  • 11