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

使用pdsh执行sed命令时遇“unterminated `s' command”错误求助

Fixing "unterminated `s' command" Error When Running sed via pdsh

Let's break down why this is happening and walk through practical fixes:

The Root Cause

When you run the sed command locally, your shell handles special characters (like / and ::) correctly. But pdsh passes commands through two layers of shell processing: your local shell first, then the remote shell on each target machine. This often mangles the sed command's syntax—especially if your target string contains characters that clash with sed's default delimiter (/), causing sed to think the command ends prematurely.

Solutions

1. Switch Sed's Delimiter to Avoid Conflicts

The simplest fix is to use a delimiter that doesn't appear in your target string (like |, #, or @) instead of the default /. This eliminates the need to escape slashes entirely.

Example:

First, confirm the corrected sed command works locally:

sed 's|.*blubb.*|blubb./123::adsf|' test

Then run it via pdsh, wrapping the entire command in single quotes to protect it from local shell parsing. Use escaped single quotes inside to preserve the sed command's quotes for the remote shell:

pdsh -w host1,host2,hostN 'sed '\''s|.*blubb.*|blubb./123::adsf|'\'' test'

2. Escape Special Characters and Use Proper Quoting

If you prefer to stick with sed's default / delimiter, escape every / in your target string with \/, and ensure the entire command is properly quoted for pdsh.

Example:

Local working command (with escaped slash):

sed 's/.*blubb.*/blubb.\/123::adsf/' test

Pdsh command with nested quoting to preserve the escaped syntax:

pdsh -w host1,host2,hostN 'sed '\''s/.*blubb.*/blubb.\/123::adsf/'\'' test'

This ensures the remote shell receives the correctly formatted sed command.

3. Use a Script File (Most Reliable for Complex Commands)

For commands with lots of special characters, save the sed logic to a script file to avoid quoting headaches entirely:

  1. Create a script named replace_blubb.sh:
#!/bin/sh
sed 's|.*blubb.*|blubb./123::adsf|' test
  1. Copy this script to all target machines using pdcp (pdsh's companion tool):
pdcp -w host1,host2,hostN replace_blubb.sh /tmp/
  1. Run the script via pdsh:
pdsh -w host1,host2,hostN 'chmod +x /tmp/replace_blubb.sh && /tmp/replace_blubb.sh'

This method bypasses shell parsing issues entirely since the script runs as-is on each remote machine.


内容的提问来源于stack exchange,提问作者Charlie28000

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:27:06