Re: [問題] import參數檔同時給shell和python用

看板Python作者 (Dark)時間14年前 (2011/04/12 12:47), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/3 (看更多)
※ 引述《IMPOSSIBLEr (不口人)》之銘言: : 小弟最近工作上需要寫一個簡單的script來控制公司內部的server : 因為控制server的script是用python : 而我的環境是linux,所以我在python外面包了shell, : 也就是用shell去呼叫python來控制server : 但是我的shell script和python script都需要用到一些 : 環境參數像是hostname之類的東西。 : 有什麼辦法是最簡單的,我只要另外提供一個.properties檔 : 然後給兩種語言都可以吃呢? : 謝謝 參考 repo (from Android) 的作法 #!/bin/sh ## Python & shell共用,參數可以放這裡 ## repo default configuration REPO_URL='http://android.git.kernel.org/tools/repo.git' REPO_REV='stable' magic='--calling-python-from-/bin/sh--' # 對 shell 來說是 exec (執行完離開) # 對 python 來說是字串 + comment """ """ #xxx """exec" python -E "$0" "$@" """#$magic" # 以下是純 python 程式 if __name__ == '__main__': import sys if sys.argv[-1] == '#%s' % magic: del sys.argv[-1] del magic -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.116.222.246
文章代碼(AID): #1DezbU3s (Python)
文章代碼(AID): #1DezbU3s (Python)