[英]Python file testing line in BASH program failing with syntax error
大家好,我正在嘗試將一些PY集成到我的Shell腳本中並遇到以下錯誤,盡管引用本來應該引用我的變量,但是看起來它沒有達到我的期望,有人可以幫我解決這個問題嗎?
#!/bin/bash
host='user@localhost'
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', "user@localhost", 'test -e ' + pipes.quote(/home/jdaniel/sent)]) == 0
^
SyntaxError: invalid syntax
python -c "return subprocess.call(['ssh', '$host', 'test -e ' + pipes.quote(\"$path\")]) == 0"
我會假設
順便說一句..為什么不只是從bash調用ssh? 您以這種方式在這里使用python有什么好處? 並且在使用-c
標志時是否不需要使用import subprocess
?
我會選擇用python或bash來完成整個程序...但是像這樣混合它們感覺有點傻(特別是考慮到您的python代碼的作用)
您需要對此進行更改
pipes.quote($path)
至
pipes.quote('$path')
如pipes.quote()需要一個字符串
我會說最好使用shell而不是python
#!/bin/bash
host='user@localhost'
path='/home/user/file'
ssh -q $host "test -e $path"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.