0

When I try to execute a python script with arguments, the script fails and show the following error:

-bash: syntax error near unexpected token `"Lary",'

I try to execute the script like this:

display("Lary", 43, "M")

My code

#! /usr/bin/python
def display(name, age, sex):
 print "Name: ", name
 print "Age: ", age
 print "Sex: ", sex

I try setting execute rights to the script (chmod 755 test.py) and I always get the same error.

What is wrong?

James Mertz
8,81912 gold badges63 silver badges88 bronze badges
asked Aug 6, 2014 at 22:50
5
  • Can you show your whole code including where you call display("Lary", 43, "M"). And does the script work when you invoke it by doing python test.py ? Commented Aug 6, 2014 at 22:53
  • And are you sure your python executable is at /usr/bin/ ? Commented Aug 6, 2014 at 22:54
  • 2
    try a shebang like #!/usr/bin/env python Commented Aug 6, 2014 at 22:57
  • 1
    I think the comment above should read #!/usr/bin/env python Commented Aug 6, 2014 at 22:58
  • You have to place display("Lary", 43, "M") inside the script and call it like ./script_name.py. I guess bash is complaining because you are trying to use Python syntax on the command prompt. Commented Aug 6, 2014 at 23:00

4 Answers 4

3

If I understand correctly, you want to run something like:

 ./test.py "Lary" 43 "M"

from the command line. To do this, you set the permissions correctly. But you need to access the command line arguments using the sys module:

 #! /usr/bin/python
 import sys
 def display(name, age, sex):
 print "Name: ", name
 print "Age: ", age
 print "Sex: ", sex
 display(sys.argv[1],sys.argv[2],sys.argv[3])

sys.argv returns a list of the command line arguments with the name of the file being sys.argv[0] (in this case ./test.py). Of course, you should probably have some sort of argument checking making sure there are enough arguments, etc. Also, make sure to parse the commands correctly. For example, if age is supposed to be an integer, you need to do age = int(sys.argv[2]) as sys.argv is a list of strings, as is standard in most languages.

answered Aug 6, 2014 at 23:05
Sign up to request clarification or add additional context in comments.

Comments

1

That error message means you're calling your function from the bash shell:

dsm@winter:~/coding$ display("Lary", 43, "M")
bash: syntax error near unexpected token `"Lary",'

You need to call it from the Python console:

dsm@winter:~/coding$ python -i display.py
>>> display("Larry", 43, "M")
Name: Larry
Age: 43
Sex: M

You can't really call Python functions from the bash console the way you're trying.

answered Aug 6, 2014 at 23:01

Comments

0

If you want to pass arguments to a python program, the simplest way is with sys.argv:

my_program.py:

#!/usr/bin/env python
import sys
print "Arguments passed:"
print sys.argv

Then run your program like:

./my_program.py this is "A test" 1 2 3
answered Aug 6, 2014 at 23:02

Comments

0

You do not execute a script calling the function display("Lary", 43, "M")

you use python my_script.py from bash or ./my_script.py

You are getting a bash error not a python error, to make a python script executable you just need chmod +x my_script.py.

~$ display("Lary", 43, "M")
bash: syntax error near unexpected token `"Lary",'

my_script.py:

import sys
def display(name, age, sex):
 print "Name: ", name
 print "Age: ", age
 print "Sex: ", sex
name = sys.argv[1]
age = sys.argv[2]
sex = sys.argv[3]
display(name, age, sex)
~$ python my_script.py foo 43 male
Name: foo
Age: 43
Sex: male
answered Aug 6, 2014 at 23:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.