2017-08-17 46 views
-2

我非常喜歡python。我有bash中的代碼需要將其轉換爲python,厭倦了轉換器,但獲取語法錯誤。如果有人幫我找到錯誤,會非常有幫助!從bash轉換爲python的代碼中的語法錯誤

錯誤:

File "cp_file.py", line 75 
    print(myfilename.val) 
    SyntaxError : Invalid Syntax 

會有很大的幫助,如果有人轉化以下的bash代碼到Python沒有,或者幫助我找到錯誤!

bash代碼:

grep " 200 " /var/log/ba/access.log |awk '{print $7}'|sort|uniq > /tmp/read_log.txt 

for i in $(cat /tmp/read_log.txt); do 
     echo $i 
     myfilename="$(echo ${i##*/})" 
     echo $myfilename 
     wget http://mydata.na.xyz/$i 
     curl -X POST -d @$myfilename http://xyz.xyz/ba/$i 
done 

Python代碼:

#! /usr/bin/env python 
from __future__ import print_function 
import sys,os,subprocess 
class Bash2Py(object): 
    __slots__ = ["val"] 
    def __init__(self, value=''): 
    self.val = value 
    def setValue(self, value=None): 
    self.val = value 
    return value 

def GetVariable(name, local=locals()): 
    if name in local: 
    return local[name] 
    if name in globals(): 
    return globals()[name] 
    return None 

def Make(name, local=locals()): 
    ret = GetVariable(name, local) 
    if ret is None: 
    ret = Bash2Py(0) 
    globals()[name] = ret 
    return ret 

def Str(value): 
    if isinstance(value, list): 
    return " ".join(value) 
    if isinstance(value, basestring): 
    return value 
    return str(value) 

def Array(value): 
    if isinstance(value, list): 
    return value 
    if isinstance(value, basestring): 
    return value.strip().split(' ') 
    return [ value ] 

_rc0 = _rcr1, _rcw1 = os.pipe() 
if os.fork(): 
    os.close(_rcw1) 
    os.dup2(_rcr1, 0) 
    _rcr2, _rcw2 = os.pipe() 
    if os.fork(): 
     os.close(_rcw2) 
     os.dup2(_rcr2, 0) 
     _rcr3, _rcw3 = os.pipe() 
     if os.fork(): 
      os.close(_rcw3) 
      os.dup2(_rcr3, 0) 
      subprocess.call("uniq",shell=True,stdout=file("/tmp/read_log.txt",'wb')) 

     else: 
      os.close(_rcr3) 
      os.dup2(_rcw3, 1) 
      subprocess.call(["sort"],shell=True) 
      sys.exit(0) 

    else: 
     os.close(_rcr2) 
     os.dup2(_rcw2, 1) 
     subprocess.call(["awk","{print $7}"],shell=True) 
     sys.exit(0) 

else: 
    os.close(_rcr1) 
    os.dup2(_rcw1, 1) 
    subprocess.call(["grep","200","/var/log/ba/access.log"],shell=True) 
    sys.exit(0) 

for Make("i").val in Array(os.popen("cat /tmp/read_log.txt").read().rstrip("\n")): 
    print(i.val) 
    Make("myfilename").setValue(os.popen("echo "+str(i.val##*/)).read().rstrip("\n")) 
    print(myfilename.val) 
    subprocess.call(["wget","http://xyz.xyz/"+str(i.val)],shell=True) 
    subprocess.call(["curl","-X","POST","-D","@"+str(myfilename.val),"http://xyz.xyz/ba/"+str(i.val)],shell=True) 
+2

SO是不是代碼寫入服務。您需要嘗試自己動手,然後在出現短**代碼問題時尋求幫助。 –

+0

轉換不應該看起來很醜陋。 –

+0

除了其他代碼錯誤外,'shell = True'會導致除第一個參數以外的所有參數被忽略(第一個參數作爲腳本運行,其他參數傳遞給該腳本,但在這些情況中都不在腳本位置傳遞的字符串查看其他參數)。 –

回答

2

自動生成的Python代碼是可怕的。你堅持Bash會好得多。但最好的方式是使用人類的理解實際將您的代碼遷移到Python。例如,拿眼前這個部分:

grep " 200 " /var/log/ba/access.log | awk '{print $7}'|sort|uniq > /tmp/read_log.txt 

在Python是這樣的:

with open('/var/log/ba/access.log') as infile, open('/tmp/read_log.txt', 'w') as outfile: 
    results = set() 
    for line in infile: 
     if ' 200 ' in line: 
      tokens = line.split() 
      results.add(tokens[6]) # 7th token 
    for result in sorted(results): 
     print >>outfile, result 

對於HTTP部分,使用Python模塊requests。它很容易使用。很有可能你不再需要outfile了 - 你可以直接使用for result in sorted(results)來發送你的HTTP請求。

1

首先不要用for來讀取文件的行,而應該使用while。 See here why

這是一個非常小的腳本,在python中比使用你的轉換器更容易重寫。

如果是在趕時間,確實需要在Python中,你可以使用裏面蟒蛇Linux命令腳本,是不是最好的方式,但更快速和容易的人誰不知道蟒蛇

import subprocess 
p = subprocess.Popen(["curl","POST","-X", "YOUR_URL"], 
stdout=subprocess.PIPE, shell=True) (output, err) = p.communicate() 
+0

但我想使用curl發佈的文件名是從'/var/log/ba/access.log' –