"避灾" $执行从蟒蛇面料远程bash命令时, [英] "Escaping" $ when executing a remote bash command from python fabric

查看:139
本文介绍了"避灾" $执行从蟒蛇面料远程bash命令时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想通过自动化的一个python脚本面料拱Linux实例的设立是这样的:

So I am trying to automate the set up of an arch linux instance via a python fabric script like this:

from fabric.api import run, sudo

def server_setup_communityrepo():
    run('echo \'echo "[archlinuxfr]" >> /etc/pacman.conf\' | sudo -s')
    run('echo \'echo "Server = http://repo.archlinux.fr/$arch" >> /etc/pacman.conf\' | sudo -s')
    run('echo \'echo " " >> /etc/pacman.conf\' | sudo -s')
    sudo('pacman -Syy yaourt --noconfirm')

第二出现问题的原因在 $拱 $符号的运行()电话。这种面料功能,因为$后跟一个字符串由布公认的配置变线2失败。但其实我是想$拱在被理解为一个文字的

The problem occurs on the second run() call because of the $ sign in the $arch. This fabric function fails in line 2 because $ followed by a string is recognized by fabric as a config variable. But I actually want $arch to be understood as a literal in the

回声回声服务器= http://repo.archlinux.fr/$arch>> /etc/pacman.conf中在bash shell中调用。

echo 'echo "Server = http://repo.archlinux.fr/$arch" >> /etc/pacman.conf' call in bash shell.

我如何从这种面料怪癖越狱并指定$拱照字面被写入到我的pacman.conf的文件?

How do I "escape" from this fabric quirk and designate the $arch as a literal to be written into my pacman.conf file?

推荐答案

使用回声单引号。这将prevent从扩大 $拱的外壳。

use echo with single quotes. That will prevent the shell from expanding $arch.

run('echo \'Server = http://repo.archlinux.fr/$arch\' | sudo -s tee -a /etc/pacman.conf')

这应该是等同于

echo 'Server = http://repo.archlinux.fr/$arch' | sudo -s tee -a /etc/pacman.conf


快速测试:


quick testing:

>>> import os
>>> os.system('echo \'Server = /foo/$arch\' ')
Server = /foo/$arch
0

这篇关于"避灾" $执行从蟒蛇面料远程bash命令时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆