在Vagrant Provisioning中使用Sed附加行以归档 [英] Using Sed to Append Lines to File During Vagrant Provisioning

查看:123
本文介绍了在Vagrant Provisioning中使用Sed附加行以归档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 Vagrant 来设置运行Ubuntu的开发计算机.我想在.profile中添加几行,以便使用sed将目录添加到$PATH变量中.为此,我将这些行添加到了计算机的Vagrantfile:

I'm currently using Vagrant to set up a development machine running Ubuntu. I want to add a few lines to my .profile that add directories to my $PATH variable, using sed. To this end, I added these lines to my machine's Vagrantfile:

config.vm.provision "shell", inline:
    "sudo sed -i \'$ a if [ -d \\\"/usr/local/lib\\\" ]; then\n    PATH=\\\"/usr/local/lib:$PATH\\\"\nfi\' /home/vagrant/.profile"

据我所知和测试,我已经逃脱了让Vagrant运行此有效的shell命令所需的所有字符:

To the best of my knowledge and testing, I've escaped all the characters needed to have Vagrant run this valid shell command:

sudo sed -i '$ a if [ -d \"/usr/local/lib\" ]; then\n    PATH=\"/usr/local/lib:$PATH\"\nfi' /home/vagrant/.profile

将这些行添加到我的.profile:

if [ -d "usr/local/lib" ]; then
    PATH="usr/local/lib:$PATH"
fi

但是,当我执行vagrant up时,它尝试运行命令时出现以下错误:

However, when I do a vagrant up I get the following error when it tries to run the command:

==> default: Running provisioner: shell...
default: Running: inline script
==> default: stdin: is not a tty
==> default: sed: -e expression #1, char 44: extra characters after command
The SSH command responded with a non-zero exit status.

我在Vagrantfile代码中某个地方犯了错误,还是这里出了什么问题?

Have I made a mistake somewhere in my Vagrantfile code, or is there something else going wrong here?

推荐答案

要修复"sed:-e表达式#1,char 44:命令后有多余字符",请转义\ n,即在所有\ n中添加另一个反斜杠:

To fix "sed: -e expression #1, char 44: extra characters after command", escape \n, that is add another backslash to all \n:

config.vm.provision "shell", inline:
"sudo sed -i \'$ a if [ -d \\\"/usr/local/lib\\\" ] then\\n    PATH=\\\"/usr/local/lib:$PATH\\\"\\nfi\' /home/vagrant/.profile"

这篇关于在Vagrant Provisioning中使用Sed附加行以归档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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