Reputation: 1
I have a problem with a script, if the file test.cfg doesn't exist then the correct Password will be written in the file test.cfg, but if the test.cfg exists then the Password won't get changed.
Here is my script test.sh -password mysecret
#!/bin/bash
while test $# -gt 0; do
case "$1" in
"-password" )
password="$2"
shift
;;
esac
shift
done
grep "password = " test.cfg &>/dev/null
if [[ $? -eq 0 ]]; then # Vorhanden!
sed -i test.cfg -e "s/^\(password = \).\"*$/\1${password}\";/"
else # Nicht vorhanden!
echo "password = \"${password}\";" >> test.cfg
fi
When the test.cfg does not exist it will be created with Content
password = "mysecret";
but now the line exists, then the password won't get changed to test.sh -password mynewsecret
It's only get changed to:
password = mynewsecret;
Upvotes: 0
Views: 147
Reputation: 754620
The problem is that your replacement string doesn't include the double quotes that you remove, and your code doesn't match what you expect either. Use this — it moves the first \"
to the correct place.
sed -i -e "s/^\(password = \).*$/\1\"${password}\";/" test.cfg
As an aside, you must be using GNU sed
. Mac OS X sed
complains sed: -i may not be used with stdin
because it expects an argument to -i
, so it treats test.cfg
as the suffix to be added to the backup file (and therefore there is no file name argument, so it is reading from standard input). I still think it is best to put option arguments before non-option arguments, even though GNU getopt(1)
(or getopt_long(1)
) supports interleaving option and non-option arguments. I've reordered the command line appropriately. It still won't work on Mac OS X; the suffix is not optional with the -i
there.
Upvotes: 0
Reputation: 242038
\"*
means *double quote zero or more times. You need a bit different regular expression:
sed -e "s/^\(password = \)\".*\"$/\1\"$password\";/"
Note that case
is suitable if there is more then one possibility, here if
would be more suitable:
while (( $# )) ; do
if [[ $1 == '-password' ]] ; then
password=$2
shift
fi
shift # What does this do, anyway?
done
if grep -q 'password = ' test.cfg ; then
sed -i~ -e "s/^\(password = \)\".*\"$/\1\"$password\";/" test.cfg
else
echo 'password = "'"$password"'";' >> test.cfg
fi
Upvotes: 1