\#Add another new line of text to hosts and send the output to
hosts_update.sh
sed '/localhost/a\
# Gateway
10.0.0.1 it20.it.cs.umb.edu it20
# Addresses for the Windows PCs
10.0.0.240 it21.it.cs.umb.edu it21\
10.0.0.241 it22.it.cs.umb.edu it22\
10.0.0.242 it23.it.cs.umb.edu it23\
10.0.0.243 it24.it.cs.umb.edu it24\
10.0.0.244 it25.it.cs.umb.edu it25\
10.0.0.245 it26.it.cs.umb.edu it26\
10.0.0.246 it27.it.cs.umb.edu it27\
10.0.0.247 it28.it.cs.umb.edu it28\
' hosts > hosts_update.sh
最佳答案
首先,您最初的几行代码看起来与 shell 脚本不符。看起来你的 hosts_update.sh
行应该是评论的一部分(而且评论不应该以 \
开头):
# Add another new line of text to hosts and send the output
# to hosts_update.sh
其次,您需要在每行的末尾添加一个\
,您要附加sed
,目前您只有它在某些选择行上。考虑到这一点,这个脚本可能就是您想要的:
# Add another new line of text to hosts and send the output
# to hosts_update.sh
sed '/localhost/a\
\
# Gateway\
10.0.0.1 it20.it.cs.umb.edu it20\
\
# Addresses for the Windows PCs\
10.0.0.240 it21.it.cs.umb.edu it21\
10.0.0.241 it22.it.cs.umb.edu it22\
10.0.0.242 it23.it.cs.umb.edu it23\
10.0.0.243 it24.it.cs.umb.edu it24\
10.0.0.244 it25.it.cs.umb.edu it25\
10.0.0.245 it26.it.cs.umb.edu it26\
10.0.0.246 it27.it.cs.umb.edu it27\
10.0.0.247 it28.it.cs.umb.edu it28\
' hosts > hosts_update.sh
你的情况实际发生了什么(没有 \
连续字符):
sed '/localhost/a\
# Gateway
10.0.0.1 it20.it.cs.umb.edu it20
是这样的:
- 在
localhost
之后追加一个空行; - 然后你有一个
sed
注释行; - 然后您告诉
sed
在第 10 行执行.
。
在那一点上,sed
正确地提示它不知道如何处理 .
命令:-)
我会说,根据经验,脚本的早期(工作)迭代只有 it21-28
行,有人(错误地)添加了 it20
和评论/空白行。这是基于只有那些行是错误的这一事实。然而,这只是(有根据的)推测,不会影响答案。
最后,您可能不想调用结果文件 hosts_update.sh
,人们几乎肯定会认为它是一个 shell 脚本而不是主机文件它实际上是。
关于linux - 有谁知道这是什么意思? -----> sed : -e expression #1, 字符 28:未知命令: `.',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47625596/