You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ruby通过SSH调用WP-CLI创建WordPress文章时遭遇语法错误的解决方案咨询

Ruby通过SSH调用WP-CLI创建WordPress文章时遭遇语法错误的解决方案咨询

我之前也碰到过类似的头疼问题——当用Ruby通过SSH调用WP-CLI传递大段带特殊字符的HTML时,shell的转义和解析简直是噩梦!你遇到的syntax error near unexpected token '<'本质上是多层shell环境下的引号混乱+特殊字符被误解析:bash会把HTML里的<当成重定向符号,嵌套的单/双引号也会打乱命令的结构,哪怕总字节数没超bash的参数限制也没用。

下面给你几个亲测有效的解决办法,按推荐程度排序:


方案1:通过标准输入传递文章内容(最省心)

WP-CLI原生支持从标准输入读取--post_content的内容,只需要把参数设为--post_content=-就行。这样你完全不用处理HTML里的任何转义,直接把内容喂给远程命令就行:

require 'open3'

# 直接写你的HTML,不用转义任何引号或特殊字符
post_content = <<~HTML
<div id="posting-content">
<header> 
<h1>Understanding the Cost to Paint Interior Spaces</h1> 
</header> 
<main> 
<section> 
<h2>What Influences Interior Painting Costs?</h2> 
<p>Interior painting costs can vary significantly based on a number of factors. The size of the area to be painted is a primary consideration. Naturally, a larger space will require more materials and labor time, resulting in higher costs. Additionally, the type of paint and the quality selected will also impact the final price. Top-tier paints offer lasting durability and vibrant colors, often justifying their higher expense.</p> 
</section> 
<section> 
<h2>Labor Costs and Professional Services</h2> 
<p>Hiring professional painters can fluctuate in cost depending on the local market. Labor costs encompass not only the painters' wages but also preparatory efforts such as wall repairs and priming. If you're looking to find residential house painters near your area, conducting a thorough research and obtaining several quotes is advisable. This ensures you get competitive pricing and quality service.</p> 
</section> 
<section> 
<h2>Additional Factors to Consider</h2> 
<p>Besides paint and labor, other elements like the complexity of the project, the number of coats required, and the time frame can also influence the overall cost. High ceilings, intricate trims, or detailed moldings might demand additional time and expertise, subsequently affecting pricing. Engaging with experienced painters who understand these nuances can result in a more accurate and reliable cost estimate.</p> 
</section> 
<section> 
<h2>Budgeting for Your Interior Painting Project</h2> 
<p>A detailed approach to budgeting for your interior painting project involves understanding these various cost components and planning accordingly. The cost to paint interior walls isn't solely about the price per square foot but also how characteristics unique to your home might affect the overall expense. The key is to balance quality with cost-effectiveness to achieve satisfactory results that meet your aesthetic and financial needs.</p> 
</section> 
</main> 
<footer> 
<p>For more insights on painting costs and to ensure quality results, exploring available options from local painters can be highly beneficial.</p> 
</footer>
</div>
HTML

# 构建命令,用--post_content=-让WP-CLI读标准输入
ssh_cmd = "ssh -p 3875 greena@greena.org 'cd public_html; wp post create --post_type=post --post_title=\"Understanding The Cost To Paint Interior Spaces\" --post_content=- --post_status=publish'"

# 用Open3执行命令,把内容写入标准输入
stdin, stdout, stderr, wait_thr = Open3.popen3(ssh_cmd)
stdin.write(post_content)
stdin.close

# 检查执行结果
if wait_thr.value.success?
  puts "文章创建成功啦!"
else
  puts "出问题了:#{stderr.read}"
end

这个方法是我现在最常用的,完全不用纠结转义,代码也清爽。

方案2:用参数数组绕过shell解析(更安全)

如果你不想用标准输入,可以用Ruby的Open3.popen3传递参数数组,这样能绕过本地shell的解析步骤,只需要处理远程shell的单引号转义:

require 'open3'

post_content = <<~HTML
<!-- 这里是你的文章HTML内容 -->
HTML

# 转义HTML里的单引号,避免远程shell把它当成命令结束符
escaped_content = post_content.gsub("'", "\\\\'")

# 把ssh命令拆成参数数组,最后一个参数是远程要执行的完整命令
cmd_args = [
  "ssh", "-p", "3875", "greena@greena.org",
  "cd public_html && wp post create --post_type=post --post_title='Understanding The Cost To Paint Interior Spaces' --post_content='#{escaped_content}' --post_status=publish"
]

# 执行命令
stdin, stdout, stderr, wait_thr = Open3.popen3(*cmd_args)

# 检查结果
if wait_thr.value.success?
  puts "文章创建成功!"
else
  puts "错误详情:#{stderr.read}"
end

这里的核心是gsub("'", "\\\\'")——因为远程shell用单引号包裹命令,所以HTML里的单引号要转成\',这样远程shell才会把它当成内容的一部分,而不是命令的结束。

方案3:先传文件再读取(最稳妥)

如果上面的方法都踩坑了,还可以先把HTML传到远程的临时文件,再让WP-CLI读取这个文件的内容:

require 'net/scp'

post_content = <<~HTML
<!-- 你的文章HTML -->
HTML

# 用SCP把HTML内容传到远程临时文件
Net::SCP.start('greena.org', 'greena', port: 3875) do |scp|
  scp.upload!(StringIO.new(post_content), '/tmp/post_content.html')
end

# 执行WP-CLI命令读取文件,最后删掉临时文件
success = system("ssh -p 3875 greena@greena.org 'cd public_html; wp post create --post_type=post --post_title=\"Understanding The Cost To Paint Interior Spaces\" --post_content=\"$(cat /tmp/post_content.html)\" --post_status=publish; rm /tmp/post_content.html'")

puts success ? "文章创建成功!" : "执行失败了"

这个方法完全不用处理任何转义,缺点是多了一步SCP上传,但胜在稳妥,适合特别复杂的内容。


总结一下:优先用方案1,它是WP-CLI官方推荐的处理大内容的方式,能彻底解决转义问题。如果因为环境限制不能用标准输入,再考虑方案2或3。

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.07 10:09:30