uncomment or comment one specific line in a config file

Hello.

I want comment or uncomment a ligne in a config file.

The file name : /etc/samba/smb.conf

Normaly the ligne is uncomment :so the line begin with a tab character
followed by passdb backend =[INDENT]\tpassdb backend =

[/INDENT]In that case I should comment this line and then the line :begin with #
followed by a tab character \t
followed by passdb backend =[INDENT]#\tpassdb backend =
[/INDENT]When I have finish my test then I want to uncomment this line which return in its initial state.[INDENT]\tpassdb backend =
[/INDENT]This line should be in any position in the file.

#!/usr/bin/sh
#
CMD="$1"

FLAG1="# passdb backend ="
FLAG2=" passdb backend ="
F_NAME="/etc/samba/smb.conf"

case "$CMD" in
"uncmt")
sed search "$FLAG1" replace by "$FLAG2" in file $F_NAME
exit
;;
"cmt")
sed search "$FLAG2" replace by "$FLAG1" in $F_NAME
exit
;;
*)
echo "Usage: comment_uncomment_smb-conf {uncmt|cmt}"
exit
esac

echo "done"

Thank you for your time

jcd

try this:

line="passdb backend ="

#to comment it out:
sed -i "/${line}/ s/^/# /" $F_NAME 


#uncomment:
sed -i "/${line}/ s/# *//" $F_NAME
1 Like

Great !

For my understanding
sed -i "/${line}/ s/^/# /" $F_NAME ==> in file $F_NAME search for occurence of $line and then look at the first position for # and replace it by a space ?

and
sed -i "/${line}/ s/# *//" $F_NAME ==> in file $F_NAME search for occurence of $line and then look somewhere for a space followed by everything and replace it by a # ?

That mean that the space should be not at the 1st position of the line ?

Anyway in my case that do the job.

Thank you very much

JCD