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.
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.
The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.
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.
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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With