I am working on a bash script to update passwords in a config file.
- The configuration file,
settings.py
, is a python file - The password in the file is a variable, that looks like this:
TEST_PASSWORD = 'SomeValue'
What I need to do is enter in a command into the Linux terminal to scan into that file, and replace 'SomeValue'
with what I enter.
I imagine some pseudocode would look like:
update_password(){
echo "What do you want to update the password to?"
new_pass = -> Read in user value into
-> update settings.py and change:
TEST_PASSWORD = 'SomeValue'
to
TEST_PASSWORD = "'" + new_pass + "'"
}
(it must be enclosed as a string in the settings.py
file)
So far, I have tried a one-liner:
sed -i 's/TEST_PASSWORD = 'test'/TEST_PASSWORD = 'new_val'/g' test/settings.py
and nothing happens. As in, the file doesn't change at all. (referenced from here)
The complete program looks like:
#!/bin/bash
read -p "Enter a password: " password
echo "Got password"
echo " "
sed -i 's/TEST_PASSWORD = / TEST_PASSWORD = $password/g' test/settings.py
But in settings.py
, it changes:
TEST_PASSWORD = 'test'
to TEST_PASSWORD = $password'test'
How can I change the value of the TEST_PASSWORD
in settings.py
to the value that the user enters, without knowing what the current password is?
For Reference:
awk -F= '/^NAME/{print $2}' /etc/os-release
yields: "Red Hat Enterprise Linux Server"