SyntaxError:Python语法无效

时间:2014-11-25 03:29:06

标签: python bash shell

我有一个python脚本。这只是我转换到我的shell脚本,对不起我只是这种语言的新手。错误是:

  File "chkrpm.py", line 19
    if(rc != 0):
    ^
SyntaxError: invalid syntax

我不知道什么是错的。这是我的shell脚本

retval=0
for i in $(cat /var/tools/tools/newrpms.txt)
do
        ls /var/patchbundle/rpms/ | grep $i
        if [ $? != 0 ]
        then
                echo "$i NOT FOUND!"
                retval=255
        fi
done
exit $retval

这是我的python转换。只关注错误方面。

import package, sys, os, subprocess

path = '/var/tools/tools/newrpms.txt'

newrpms = open(path, "w")
fds = newrpms.readline()
newrpms.close


def checkrc(rc):
                if(rc != 0):
                        sys.exit(rc)
cmd = package.Errata()
retval=0


for i in newrpms:
        rc = cmd.execute("ls /var/patchbundle/rpms/ | grep %newrpms ")
                if(rc != 0):
                    cmd.logprint ("%s not found !" % i)
                    retval = 255

checkrc(rc)

我的代码错了。它适合我的bash脚本吗?

当我尝试按照以下所有建议时发现错误:

 File "chkrpm.py", line 19
    echo("%s not found !" % i)
    ^
SyntaxError: invalid syntax

1 个答案:

答案 0 :(得分:0)

您的错误消息说“语法无效”,因为缩进错误

你的for循环看起来像:

for i in newrpms:
        rc = cmd.execute("ls /var/patchbundle/rpms/ | grep %newrpms ")
                if(rc != 0):
                    cmd.logprint ("%s not found !" % i)
                    retval = 255

应该是这样的:

for i in newrpms:
    rc = cmd.execute("ls /var/patchbundle/rpms/ | grep %newrpms ")
    if(rc != 0):
        cmd.logprint ("%s not found !" % i)
        etval = 255