Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting syntax error near unexpected token `;' in python

Tags:

python

I am a Python Novice so please help me out...

#!/usr/bin/python -tt

import sys
import commands

def runCommands():
  f = open("a.txt", 'r')
  for line in f:  # goes through a text file line by line
    cmd = 'ls -l ' + line 
    print "printing cmd = " + cmd,
    (status, output) = commands.getstatusoutput(cmd)
  if status:    ## Error case, print the command's output to stderr and exit
      print "error"
      sys.stderr.write(output)
      sys.exit(1)
  print output
  f.close()

def main():
  runCommands()

# Standard boilerplate at end of file to call main() function.
if __name__ == '__main__':
  main()

I run it as follows:

$python demo.py
sh: -c: line 1: syntax error near unexpected token `;'
sh: -c: line 1: `; } 2>&1'
error

Running less $(which python) says:

#!/bin/sh bin=$(cd $(/usr/bin/dirname "$0") && pwd) exec -a "$0" "$bin/python2.5" "$@"

If i remove for loop then it works fine

$cat a.txt
dummyFile


$ls -l dummyFile
-rw-r--r-- 1 blah blah ...................

$python demo.py
printing cmd = ls -l dummyFile
sh: -c: line 1: syntax error near unexpected token `;'
sh: -c: line 1: `; } 2>&1'
error

I am using 'ls' just for showing the problem. Actually i wanna use some internal shell scripts so i have to run this python script in this way only.

like image 830
rajya vardhan Avatar asked Jun 05 '12 15:06

rajya vardhan


People also ask

How do you fix unexpected tokens in Python?

Unexpected Token This is simply down to a syntax error (your fault, I'm afraid). Perhaps you forgot the ':' after a conditional branch or there is an unclosed parenthesis left somewhere in your code? Python scripts are broken down into 'tokens' which helps the program navigate the logic of your code.

What Is syntax error near unexpected token?

The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.

How do I fix unexpected token in Unix?

If you execute the code written in the Windows OS in the Cygwin, you may get the Syntax error near unexpected token '('. To fix the error, you need to clear the carriage return characters using the DOS to Unix command line tool as a text file format converter.


3 Answers

The problem is caused by this line:

    cmd = 'ls -l ' + line

it should be modified to:

    cmd = 'ls -l ' + line.strip() 

When you read the line from your text file, you also read the trailing \n. You need to strip this so that it works. The getstatusoutput() doesn't like the trailing newline. See this interactive test (which is how I verified it):

In [7]: s, o = commands.getstatusoutput('ls -l dummyFile')

In [8]: s, o = commands.getstatusoutput('ls -l dummyFile\n')
sh: Syntax error: ";" unexpected
like image 104
Levon Avatar answered Sep 28 '22 10:09

Levon


This seems to be a problem with the "python" command, perhaps it's a shell wrapper script or something.

Run

$ less $(which python)

UPDATE:

Try calling the Python executable directly, it seems to be at /usr/bin/python2.5:

$ /usr/bin/python2.5 demo.py
like image 36
unwind Avatar answered Sep 28 '22 10:09

unwind


The documentation for the commands module states that when you run getstatusoutput(cmd),

cmd is actually run as { cmd ; } 2>&1

This should explain where the ; } 2>&1 is coming from.

My first guess is that the problem is being caused by not stripping the newlines off the end of each line you read from the file, and so the command you're actually running is something like

{ ls -l somedir
; } 2>&1

However, I don't know shell programming very well so I don't know how sh will cope with the contents of the { ... } split over two lines, nor why it reports the problem on line 1 when there are now two lines.

A second guess is that there's a blank line in your file, in which case sh may be complaining because it's looking for an argument for ls and it found ; } 2>&1 instead.

A third guess is that one of the files contains a }, or maybe a ; followed by a }.

Ultimately, I can't say for sure what the problem is without seeing the contents of your file a.txt.

Incidentally, I hope this file doesn't contain a line / && sudo rm -rf /, as that might cause you one or two problems.

like image 43
Luke Woodward Avatar answered Sep 28 '22 10:09

Luke Woodward