ruby - 在 Vagrant 配置期间使用 Sed 将行附加到文件

标签 ruby linux string

我目前正在使用 Vagrant设置运行 Ubuntu 的开发机器。我想在我的 .profile 中添加几行,使用 sed 将目录添加到我的 $PATH 变量中。为此,我将这些行添加到我机器的 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 命令所需的所有字符:

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 时,当它尝试运行命令时出现以下错误:

==> 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 代码中的某个地方犯了错误,还是这里出了什么问题?

最佳答案

要修复“sed: -e expression #1, char 44: extra characters after command”,转义\n,即向所有\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"

关于ruby - 在 Vagrant 配置期间使用 Sed 将行附加到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32437006/

相关文章:

ruby - Ruby 2.7 中的 SimpleDelegator 在哪里?

ruby - 如何在 Ruby 中条件格式化字符串?

javascript - JavaScript 中字符串连接的性能很糟糕?

ruby-on-rails - Rails中的网络爬虫从网页中提取链接和下载文件

python - 呃!堆栈错误: Command failed: python2 -c import platform;

c - 一个简单的linux设备驱动程序

linux - 创 build 备文件 : Linux device driver

php - Crontab PHP 中的全局库

c - 从c中的stdin读取字符串

c++ - 将字符串拆分为 vector C++