简体   繁体   English

sed无法使用bash脚本注释文件中的一行

[英]sed is not working for commenting a line in a file using bash script

I have created a bash script that is used to modify the ulimit of open files in the RHEL server. 我创建了一个bash脚本,该脚本用于修改RHEL服务器中打开文件的ulimit。

so i have reading the lines in the file /etc/security/limits.conf and if the soft/hard limit of the open files are less than 10000 for '*' domain i am commenting the line and adding a new line with soft/hard limit as 10000. 因此,我已阅读文件/etc/security/limits.conf中的行,并且如果'*'域的打开文件的软/硬限制小于10000,我将注释该行并添加一个新行,其中包含soft /硬限制为10000。

The Script is working as designed but the sed command to comment a line in the script is not working. 脚本正在按设计工作,但是sed命令注释脚本中的一行不起作用。

Please find the full script below :- 请在下面找到完整的脚本:

#!/bin/sh
#This script would be called by '' to set ulimit values for open files in unix servers.
#
configfile=/etc/security/limits.conf
help(){
echo "usage: $0 <LimitValue>"
echo -e "where\t--LimitValue= No of files you want all the users to open"
exit 1
}
modifyulimit()
{
grep '*\s*hard\s*nofile\s*' $configfile | while read -r line ; do
        firstChar="$(echo $line |  xargs | cut -c1-1)"
        if [ "$firstChar" != "#" ];then
        hardValue="$(echo  $line | rev | cut -d ' ' -f1 | rev)"
            if [[ "$hardValue" -ge "$1" ]]; then
            echo ""
            else
            sed -i -e 's/$line/#$line/g' $configfile 
            echo "*          hard     nofile         $1" >> $configfile
            fi
        else
            echo ""
        fi
    done

grep '*\s*soft\s*nofile\s*' $configfile | while read -r line ; do
        firstChar="$(echo $line |  xargs | cut -c1-1)"
        if [ "$firstChar" != "#" ];then
        hardValue="$(echo  $line | rev | cut -d ' ' -f1 | rev)"
            if [[ "$hardValue" -ge "$1" ]]; then
            echo ""
            else
            sed -i -e 's/$line/#$line/g' $configfile 
            echo "*          hard     nofile         $1" >> $configfile
            fi
        else
            echo ""
        fi
    done
}
deleteEofTag(){
sed -i "/\b\(End of file\)\b/d" $configfile
}
addEofTag()
{
echo "#################End of file###################" >> $configfile
}
#-------------Execution of the script starts here ----------------------
if [ $# -ne 1 ];
then
help
else
modifyulimit $1
deleteEofTag
addEofTag
fi

The command sed -i -e 's/$line/#$line/g' $configfile when executed from the terminal is working absolutely fine and it is commenting the line but it is not working when i am executing it from the unix shell script. 从终端执行时,命令sed -i -e's / $ line /#$ line / g'$ configfile绝对正常,并且正在注释该行,但是当我从unix shell执行该命令时,该命令不起作用脚本。

interpolation does not work in single quote use double quote and try 插值不适用于单引号,请使用双引号并尝试

sed -i -e 's/$line/#$line/g' sed -i -e's / $ line /#$ line / g'

sed -i -e "s/$line/#$line/g"

您也可以尝试:sed -i -es / $ {line} /#$ {line} / g,因为这将告诉脚本使用变量的值而不是变量的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM