sed 失败,并显示“'s' 的未知选项";错误 [英] sed fails with "unknown option to `s'" error
问题描述
我正在尝试使用
sed -i -e "s/.*seb.*/ \"$ftp_login_template\"/" $ftp_dir
但是我收到此错误:
sed: -e expression #1, char 34: unknown option to `s'
我不明白为什么,因为这非常有效:
I don't understand why since this works perfectly:
sed -i -e "s/.*wbspassword.*/ \"wbspassword\": \"$password\",/" $user_conf
对我做错了什么有什么想法吗?
Any ideas as to what I'm doing wrong?
这可能是问题吗?ftp_login_template=\${user}:${password}:24:86::\/var\/lib\/clit.${user}\/downloads:\/bin\/false\"
推荐答案
问题在于斜杠:你的变量包含斜杠,最终的命令将类似于 sed "s/string/path/to/something/g"
,包含太多斜线.
The problem is with slashes: your variable contains them and the final command will be something like sed "s/string/path/to/something/g"
, containing way too many slashes.
由于 sed
可以将任何字符作为分隔符(无需声明新的分隔符),您可以尝试使用另一个没有出现在替换字符串中的字符:
Since sed
can take any char as delimiter (without having to declare the new delimiter), you can try using another one that doesn't appear in your replacement string:
replacement="/my/path"
sed --expression "s@pattern@$replacement@"
请注意,这不是防弹的:如果替换字符串稍后包含 @
它将出于相同的原因而中断,并且任何反斜杠序列如 \1
仍将是根据 sed
规则解释.使用 |
作为分隔符也是一个不错的选择,因为它的可读性类似于 /
.
Note that this is not bullet proof: if the replacement string later contains @
it will break for the same reason, and any backslash sequences like \1
will still be interpreted according to sed
rules. Using |
as a delimiter is also a nice option as it is similar in readability to /
.
这篇关于sed 失败,并显示“'s' 的未知选项";错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!