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

时间:2017-07-24 08:43:00

标签: bash redhat

我想使用bash脚本编辑sshd_config文件。但是,我一直收到错误。我很感激任何帮助。

这是我的剧本:

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

这是我得到的错误:

sed: -e expression #1, char 7: unterminated address regex

脚本错误行:

 sed -i /etc/ssh/sshd_config -e '/# Don't read the user's ~/.rhosts and ~/.shosts files/a IgnoreRhosts yes'

1 个答案:

答案 0 :(得分:0)

这个应该有效:

sed -i /etc/ssh/sshd_config -e "\|# Don't read the user's ~/.rhosts and ~/.shosts files|a IgnoreRhosts yes"

说明:  在这个脚本中,我使用|作为分隔符,使其更具可读性。此分隔符由第一个\(更多信息here

定义

然后,我使用双引号而不是单引号来避免在单词Don'tuser's

中转义撇号