为了账号安全,请及时绑定邮箱和手机立即绑定

使用 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 关注
  • 472 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号