You're mixing character classes (a list of characters inside square brackets) with the smb.conf share names which are surrounded by square bracket literals. Also, the
echo command is not well-formed: in the case where
sed exits with a non-zero status, the shell will attempt to invoke the command
A few suggestions:
- Remove the character class (outer brackets)
- Remove the leading
.* which will remove leading comments, etc.
- Run a single
sed command with multiple statements
- Fix the error case message
sed -i 's/\[CMI\]/\[CMI\$\]/;
s/\[NATIONAL\]/\[NATIONAL\$\]/' $smbconffile && echo "Success" || echo "Failed"`
You could use backreferences to avoid retyping the original prefix in the replacement expression (e.g., `[CMI'), but that can make the expressions even harder to read.
Another suggest is to use Perl which has readable grouping and backreferences IMO:
perl -pi -e 's/(\[(?:CMI|LOCAL|NATIONAL))\]/$1\$]/g' $smbconffile \
&& echo "Success" || echo "Failed"
Edit: To clarify, the problem with the original expression with the outer brackets is that you are creating a character class which matches any string containing (in the first case)
] in any order followed by a
]. For example:
$ echo '[IMC]' | sed 's/[\[CMI\]]/[something_else]/'
$ echo '[FOOM]' | sed 's/[\[CMI\]]/[something_else]/'
Removing the outer brackets removes the character class so the expression will match the exact substring provided.