-1

So i've been at this one for a little while and cant seem to get it. Im trying to execute a python script via terminal and want to pass a string value with it. That way, when the script starts, it can check that value and act accordingly. Like this:

sudo python myscript.py mystring

How can i go about doing this. I know there's a way to start and stop a script using bash, but thats not really what im looking for. Any and all help accepted!

amartin94
  • 505
  • 3
  • 19
  • 35

2 Answers2

2

Try the following inside ur script:

import sys
arg1 = str(sys.argv[1])
print(arg1)
M. Adel
  • 401
  • 5
  • 7
-2

Since you are passing a string, you need to pass it in quotes:

sudo python myscript.py 'mystring'

Also, you shouldn't have to run it with sudo.

dursk
  • 4,435
  • 2
  • 19
  • 30
  • I do need to run it with sudo.. It's an RPi project controlling GPIO pins, therefore, sudo is necessary :) – amartin94 Apr 30 '14 at 14:12
  • You are mistaken about the need for quotations marks here. The quotations marks don't make it a string (it's already a string); they are only necessary to protect certain characters from being interpreted by the shell. – chepner Apr 30 '14 at 14:34