我從python模塊返回字符串,用於在bash腳本中使用。bash:字符串插值
這是我到目前爲止有:
SCRIPT_DIR=/somepath/to/scripts/folder
cd $SCRIPT_DIR/eod
python_cmd='import sys;
sys.path.append("/somepath/to/scripts/folder/utils");
import settings as st;
print " ".join(st.foo())'
# Fix indentation for Python
python_cmd=${python_cmd//' '/''}
my_array=(`python -c "$python_cmd"`)
我想用幹理念在片段上方。但是,腳本中重複字符串/somepath/to/scripts/folder
。我想的$ SCRIPT_DIR定義傳遞到PYTHON_CMD - 但是我想(與$ SCRIPT_DIR/utils的替換PYTHON_CMD路徑字符串,它失敗,出現以下錯誤:
Traceback (most recent call last):
File "<string>", line 3, in <module>
ImportError: No module named settings
我在做什麼錯?
注:
我運行bash 4.1.5
對不起,是一個痛苦。但是你可以發表幾行來展示我如何使用'PYTHONPATH'來代替? - 謝謝。 – 2012-07-09 13:26:15
更新了一個例子。 – 2012-07-09 13:28:35
不錯。非常感謝! :) – 2012-07-09 13:30:43