BSD 环境;当脚本 运行 时出现错误 "unescaped newline inside substitute pattern"
BSD sed; error "unescaped newline inside substitute pattern" when run from script
我正在尝试使用 (BSD) sed
修改我的 /etc/gettytab
。目标是修改这个条目:
P|Pc|Pc console:\
:ht:np:sp#9600:
到此条目:
P|Pc|Pc console:\
:ht:np:sp#115200:\
:cl=\E[H\E[2J:
如果我发出下面的命令(它在两行上),它会完美地工作。
# sed -in ' /P|Pc|Pc console/,/^#/ s/9600:/115200:\\
:cl=\E[H\E[2J:/' /etc/gettytab
但是,如果我在脚本中使用完全相同的命令(字面意思是 copy/paste),我会收到一条错误消息:
sed: 1: " /P|Pc|Pc console/,/^#/ ...": unescaped newline inside substitute pattern
通过搜索,我找到了这个 post: ,它谈到了尾随 /
,但我的模式中有它。
如果有人可以帮助解决我做错的事情,我将不胜感激。
在您的脚本中,您使用 \
转义换行符,并且转义要嵌入到输出中的 \
,以便按字面解释。如果我的数学是正确的,那就是三个,而不是四个反斜杠。
$ cat i
P|Pc|Pc console:\
:ht:np:sp#9600:
$ cat i.sh
#!/bin/sh
# ┏━━━ escapes the next character,
# ┃┏━━ literal backslash for output,
# ┃┃┏━ escapes the newline.
sed -ne '/^P|/,/^#/ s/9600:/115200:\\
:cl=\E[H\E[2J:/' -e p i
$ ./i.sh
P|Pc|Pc console:\
:ht:np:sp#115200:\
:cl=E[HE[2J:
$
我正在尝试使用 (BSD) sed
修改我的 /etc/gettytab
。目标是修改这个条目:
P|Pc|Pc console:\
:ht:np:sp#9600:
到此条目:
P|Pc|Pc console:\
:ht:np:sp#115200:\
:cl=\E[H\E[2J:
如果我发出下面的命令(它在两行上),它会完美地工作。
# sed -in ' /P|Pc|Pc console/,/^#/ s/9600:/115200:\\
:cl=\E[H\E[2J:/' /etc/gettytab
但是,如果我在脚本中使用完全相同的命令(字面意思是 copy/paste),我会收到一条错误消息:
sed: 1: " /P|Pc|Pc console/,/^#/ ...": unescaped newline inside substitute pattern
通过搜索,我找到了这个 post: /
,但我的模式中有它。
如果有人可以帮助解决我做错的事情,我将不胜感激。
在您的脚本中,您使用 \
转义换行符,并且转义要嵌入到输出中的 \
,以便按字面解释。如果我的数学是正确的,那就是三个,而不是四个反斜杠。
$ cat i
P|Pc|Pc console:\
:ht:np:sp#9600:
$ cat i.sh
#!/bin/sh
# ┏━━━ escapes the next character,
# ┃┏━━ literal backslash for output,
# ┃┃┏━ escapes the newline.
sed -ne '/^P|/,/^#/ s/9600:/115200:\\
:cl=\E[H\E[2J:/' -e p i
$ ./i.sh
P|Pc|Pc console:\
:ht:np:sp#115200:\
:cl=E[HE[2J:
$