sed错误“未终止的”命令“故障排除 [英] sed error "unterminated 's' command" troubleshooting

查看:470
本文介绍了sed错误“未终止的”命令“故障排除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个脚本,将在其他的东西,用文件夹路径替换XML文件中的模式。

I am building a script that will, among other things, replace a pattern in an XML file with a folder path.

sed 命令我试图使用是:

SEDCMD="s|PATHTOEXPORT|$2|"

其中 $ 2 是命令行参数那里有文件夹路径。

where $2 is the command-line parameter that has the folder path in it.

以后调用:

sed -e $SEDCMD $FILTER > $TEMPFILTER

但是,在运行命令时,我收到一个unterminated's'命令错误。

However, on running the command, I am getting an "unterminated 's' command" error.

我该如何解决?我已经尝试更改用于分隔正则表达式的字符(从 / | )。我试过引用(以不同的方式)命令行参数。

How can I get around this? I've tried changing the characters used to separate the regex (from / to |). And I've tried quoting (in different ways) the command-line parameter.

推荐答案

shell正在看到解析内容的 $ SEDCMD 。如果您使用的是Shell脚本(包括Makefile),则应始终使用双引号保护所有扩展变量。双引号将强制可变插值,但保护任何shell元字符进一步解释。

The shell is seeing the parsing the contents of $SEDCMD. If you’re using this from a shell script, including a Makefile, you should always protect all your expanded variables with double quotes. The double quotes will force variable interpolation but protect any shell metacharacter from further interpretation.

sed -e "$SEDCMD" "$FILTER" > "$TEMPFILTER"

我认为 $ FILTER $ TEMPFILTER 是文件名?我也引用了他们,以防万一它们含有诸如空白或其他类型的shell元字符之类的邪恶的东西;很奇怪,是的,但它是已知的事情发生。经常在文件名上运行重命名'/ \s + / _ / g',以清除它们的空格,但对于其他人,您必须更加小心方法例如,如何处理星星与问号与括号和括号等。

I assume that $FILTER and $TEMPFILTER are filenames? I’ve quoted them, too, just in case they contain evil things like whitespace or other sorts of shell metacharacters; bizarre, yes, but it’s been known to happen. A regularly run rename 's/\s+/_/g' on filenames to clean them of whitespace, but for the others, you'll have to take a more careful approach; e.g., what to do with stars vs question marks vs brackets and parens, etc.

如果添加 -x 和/或 -v 到你的shell命令行,你会得到一些跟踪调试,我认为可能会显示你在这里的错误。

If you add -x and/or -v to your shell command line, you’ll get some trace debugging, which I think would likely have shown where you went amiss here.

这篇关于sed错误“未终止的”命令“故障排除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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