gpt4 book ai didi

ruby - 如何防止 Capistrano 替换换行符?

转载 作者:数据小太阳 更新时间:2023-10-29 08:46:54 25 4
gpt4 key购买 nike

作为我的 capistrano 设置的一部分,我想远程运行一些 shell 脚本。为了测试该功能,我使用了这段代码:

execute <<SHELL
cat <<TEST
something
TEST
SHELL

然而,那实际上是在运行/usr/bin/env cat <<TEST; something; TEST这显然是行不通的。我如何告诉 capistrano 执行我编写的 heredoc,而不将换行符转换为分号?

我有 Capistrano 版本:3.2.1(Rake 版本:10.3.2)并且对 ruby​​ 不是特别了解,所以我可能错过了一些明显的东西。

最佳答案

认为cat 的参数指定为第二个,呃,execute 的参数可能会起作用:

cat_args = <<SHELL
<<TEST
something
TEST
SHELL

execute "cat", cat_args

从@DavidGrayson 发布的代码来看,似乎只有命令(execute 的第一个参数)被净化了。

不过,我同意 David 的观点,更简单的方法可能是将数据放在一个文件中,即 what the SSHKit documentation suggests :

Upload a file from a stream

on hosts do |host|
file = File.open('/config/database.yml')
io = StringIO.new(....)
upload! file, '/opt/my_project/shared/database.yml'
upload! io, '/opt/my_project/shared/io.io.io'
end

The IO streaming is useful for uploading something rather than "cat"ing it, for example

on hosts do |host|
contents = StringIO.new('ALL ALL = (ALL) NOPASSWD: ALL')
upload! contents, '/etc/sudoers.d/yolo'
end

This spares one from having to figure out the correct escaping sequences for something like "echo(:cat, '...?...', '> /etc/sudoers.d/yolo')".

这似乎非常适合您的用例。

关于ruby - 如何防止 Capistrano 替换换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25186584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com