0

I've written a bash script based on this and this. The script is:

#!/bin/bash 
until python data.py $1 $2 ; do
    echo "'data.py' crashed with exit code $?. Restarting..." >> errors.txt
    sleep 1
done

I run it:

./run.sh ‘a’ ‘n’

but in data.py

if sys.argv[1] == 'a':

returns false. How do I pass $1 and $2 to my Python program? TIA!!!

EDIT: SOLUTION

Thanks to Jandom and andlrc: I call it:

./run.sh "a" "n"

and change the script to:

until python data.py "$@" ; do 
Community
  • 1
  • 1
schoon
  • 2,858
  • 3
  • 46
  • 78
  • 4
    Are these really `‘` (i.e. [`u'\u2018'`](http://www.charbase.com/2018-unicode-left-single-quotation-mark))? Because then, `sys.argv == [script_name, '‘a‘', '‘b‘']`, probably. – dhke May 07 '16 at 15:52

2 Answers2

3

script.sh (make sure it allow execute)

#!/bin/sh
python script.py "$@"

script.py (first param is ./script.sh if run as ./script.sh from terminal), so, script.py:

import sys
print(sys.argv[1:])
# sys.argv[1] == a, sys.argv[2] == b, etc..
VelikiiNehochuha
  • 3,775
  • 2
  • 15
  • 32
2

You are sending special quotes to your shell script:

% charinfo ‘’
U+2018 SINGLE TURNED COMMA QUOTATION MARK [Pi]
U+2019 SINGLE COMMA QUOTATION MARK [Pf]

Run it with single or double quotes:

./run.sh "a" "b"

Also remember quotes in your bash script or $1 and $2 will undergo word splitting:

#!/bin/bash
until python data.py "$1" "$2"; do
    echo "'data.py' crashed with exit code $?. Restarting..." >> errors.txt
    sleep 1
done

As pointed out before you can use "$@" which will refer to all arguments send to your bash script.

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123