This script will read the old and new value from the user, and then use sed to find and replace them in the file. For example, if I entered TTz and BBz, it would look for T \ T \ z in the file and replace it with B \ B \ z. This works, but I tried to make it more concise.
I do not need the intermediate variables $ ESC_OLD_PW and $ ESC_NEW_PW. Is there a reasonable way to do this?
read -sp "Old:" OLD_PW && echo
read -sp "New:" NEW_PW && echo
printf -v ESC_OLD_PW "%q" "${OLD_PW}"
printf -v ESC_NEW_PW "%q" "${NEW_PW}"
printf -v ESC_ESC_OLD_PW "%q" "${ESC_OLD_PW}"
printf -v ESC_ESC_NEW_PW "%q" "${ESC_NEW_PW}"
sed -i -e s/"${ESC_ESC_OLD_PW}"/"${ESC_ESC_NEW_PW}"/g $1
I tried the following:
~$ OLD_PW="T*T*z"
~$ printf "%q" $OLD_PW | xargs printf "%q"
printf: %q: invalid conversion specification
~$
And I tried a lot of variations on things in printf ... Any suggestions?
source
share