首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >bash脚本中出现未终止的地址正则表达式错误

bash脚本中出现未终止的地址正则表达式错误
EN

Stack Overflow用户
提问于 2017-07-24 16:43:00
回答 1查看 122关注 0票数 0

我想使用bash脚本编辑sshd_config文件。然而,我一直收到一个错误。如果能得到任何帮助,我将不胜感激。

这是我的脚本:

代码语言:javascript
运行
复制
if ! grep "IgnoreRhosts" "/etc/ssh/sshd_config"; then
    sed -i /etc/ssh/sshd_config -e '/# Don't read the user's ~/.rhosts and ~/.shosts files/a IgnoreRhosts yes'
    printf "\e[32m IGNORERHOSTS YES ADDED\e[0m\n"
elif grep "#IgnoreRhosts yes" "/etc/ssh/sshd_config"; then
    sed -i 's/^#IgnoreRhosts yes/IgnoreRhosts yes/' /etc/ssh/sshd_config
    printf "\e[32mSUCCESSFULLY CHANGED\e[0m\n"
else
    printf "\e[32mNO CHANGES NEEDED\e[0m\n"
fi

这是我得到的错误:

代码语言:javascript
运行
复制
sed: -e expression #1, char 7: unterminated address regex

脚本错误行:

代码语言:javascript
运行
复制
 sed -i /etc/ssh/sshd_config -e '/# Don't read the user's ~/.rhosts and ~/.shosts files/a IgnoreRhosts yes'
EN

回答 1

Stack Overflow用户

发布于 2017-07-24 17:19:15

这个应该行得通:

代码语言:javascript
运行
复制
sed -i /etc/ssh/sshd_config -e "\|# Don't read the user's ~/.rhosts and ~/.shosts files|a IgnoreRhosts yes"

说明:在此脚本中,我使用|作为分隔符,以使其更具可读性。该分隔符由first \ (more info here)定义

然后,我使用双引号而不是单引号,以避免在单词Don'tuser's中转义撇号

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45275947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档