亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 python 生成 shell 腳本

使用 python 生成 shell 腳本

白板的微信 2022-08-02 10:53:09
我目前正在開發一個python腳本,該腳本生成shell腳本以在Linux服務器上安裝代理。python腳本輸出.sh文件不斷以“語法錯誤:文件意外結束”結束,但是當我在vi中手動輸入確切的輸出時,似乎沒有問題。我用python編寫它的方式是否有任何問題,或者通過python進行編寫是否可行?腳本import csvdef menu():    print("type of scripts")    print("1. Install + Generation")    print("2. Unregister + Reregister")    print("3. Unregister + Uninstall")#Converts numeral choice into type for script namingdef choicename(choice):    choice = int(choice)    if choice==1:        return "install"    elif choice == 2 :        return "rereg"    else:        return "uninstall"#Generates the install agent scripts    def installScript(agentname,agentfile,mgrfile,prigw,secgw,ostype):    #Generates the script for Linux agents (.sh)    if ostype=="Linux":        agentpath = 'agent="/opt/test/ragent/bin/racli"'        installerpath = '\ninstaller="/opt/test/installer/bin/racli"'        checkAgent = '\nif [ ! -e "$agent" ]; then' +"\n" + "./" + agentfile + " -n -d /opt/test" + '\nelse\necho "Agent is already installed"\nfi'        checkInstaller = '\nif [ ! -e "$installer" ]; then' + "\n" +"./" + mgrfile + " -n -d /opt/test"+ '\nelse\necho "Manager is already installed"\nfi'        regAgent = "\n/opt/test/ragent/bin/cli registration advanced-register registration-type=Primary ragent-name="+ agentname+ " gw-ip="+ prigw+ " gw-port=443 manual-settings-activation=Automatic monitor-networkchannels=Both password=$1"        if secgw!="":            regAgent+="\n/opt/test/ragent/bin/cli registration advanced-register registration-type=Secondary ragent-name="+ agentname+ " gw-ip="+ secgw+ " gw-port=443 manual-settings-activation=Automatic monitor-networkchannels=Both password=$1"        startAgent="\n/opt/test/ragent/bin/rainit start"
查看完整描述

1 回答

?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

換行符約定在窗口上。Bash 將換行符解釋為...換行符,是 的正常字符。\r\n\rbash

修復:

sf = open(agentname+ "_install.sh", "w", newline='\n')


查看完整回答
反對 回復 2022-08-02
  • 1 回答
  • 0 關注
  • 463 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號