TheWellington
TheWellington

Reputation: 255

Python: -bash: syntax error near unexpected token

I have written a little script in Python that I use to append text to a work log. I have placed the script in a directory in my $PATH

#!/usr/bin/python

# import necessary modules
import sys
import os
import datetime


# main() function
def main():
  now = datetime.datetime.now()

  tmp = ' '.join(sys.argv[1:])

  outfile = '/path/to/output/done.log'

  outstr = now.strftime("%Y-%m-%d %H:%M:%S") + ' - ' + tmp + '\n'

  f=open(outfile,'a')
  f.write(outstr)
  f.close()

  # print sys.argv[0:1]
  print 'Adding ' + outstr

# Call main()
if __name__ == '__main__':
  main()

When I run the script as in example 1, I get an error.

Example 1:

host:scripts user$ done this is a test
-bash: syntax error near unexpected token `done'

If I run the script as in example 2, it behaves as expected.

Example 2:

host:scripts user$ python done this is a test
Adding 2012-11-15 09:57:44 - this is a test

How do I get this to work in the first example?

Upvotes: 0

Views: 3001

Answers (1)

David Wolever
David Wolever

Reputation: 154454

done is a bash keyword, so can't be used in certain places like "the place Bash expects a command name". You could use ./done (or /path/to/done, or python /path/to/done), or re-name the command.

Upvotes: 4

Related Questions