2014-01-22 33 views
0

嘿傢伙我試圖集成一些PY到我的shell腳本和運行跨越 以下錯誤,我雖然報價應引用我的變量,但它看起來像它的 沒有按照我的預期做,有人可以幫我解決這個問題嗎?BASH程序中的Python文件測試行失敗,語法錯誤

#!/bin/bash 
host='[email protected]' 
path='/home/user/file' 

python -c "return subprocess.call(['ssh', '$host', 'test -e ' + pipes.quote($path)]) == 0" 

    File "<string>", line 1 
    return subprocess.call(['ssh', "[email protected]", 'test -e ' + pipes.quote(/home/jdaniel/sent)]) == 0 
                       ^
SyntaxError: invalid syntax 

回答

2
python -c "return subprocess.call(['ssh', '$host', 'test -e ' + pipes.quote(\"$path\")]) == 0" 

我會假設

順便說一句..你爲什麼不只是從bash的調用SSH?通過在這裏使用python獲得什麼好處?當你使用-c標誌時,你不需要使用import subprocess嗎?

我會選擇這樣做,整個項目無論是在Python或慶典......但它們混合喜歡這種感覺略帶傻氣(特別是考慮到你的Python代碼做什麼)

+0

文件「」,1個線語法錯誤:「迴歸」之外的功能? – ehime

+0

只是想學習一些PY來補充我的bash,加上bash並沒有真正的測試其他服務器上是否存在文件的方法。我想我可以使用rsync,但它聽起來並不像我想的那樣有趣。 – ehime

+0

是啊,因爲你不能有一個函數外的返回......你有一個返回作爲程序中的唯一行......但這是一個不同的問題......但只是從你的Python代碼中刪除單詞「返回」。 ..你所做的就是從python裏面調用ssh程序......這與100%是一樣的,就像你從bash afaik調用它一樣...... –

1

你需要改變這種

pipes.quote($path) 

pipes.quote('$path') 

如pipes.quote()被期待一個字符串

我要說它更好地使用外殼,而不是蟒蛇

#!/bin/bash 
host='[email protected]' 
path='/home/user/file' 
ssh -q $host "test -e $path" 
+0

的確如此,但我並沒有試圖在BASH中做到這一點,不過謝謝你 – ehime

相關問題