I have a file called config.ini in say /tmp location. With the following content.
[vivek@localhost ~]$ cat /tmp/config.ini
[site]
"name"="Site 0"
[node]
"name"="Node 0"
[systemsecurity]
[systemsecurity\webserver]
"level"="high"
[tacacs]
"interface"="0"
"enable"=dword:00000000
"port"=dword:00000031
"allowPersistentTCPConnections"=dword:00000001
[misc]
"adminsyspassword"="$ADMIN_PASSWD"
"adminuser"="admin"
[vivek@localhost ~]$
And I need to replace the "adminsyspassword" with the value present in the /etc/shadow file value. And I used the below shell script with awk command to get the new value for "adminsyspassword" key of the ini file.
[vivek@localhost ~]$ cat /tmp/a.sh
#! /bin/sh
echo "Fetching the admin encrypted Password from the /etc/shadow file"
ADMIN_PASSWD=`sudo awk -F: '/admin/ { print $2}' /etc/shadow`
PARAM_TO_CHANGE='^\(\"adminsyspassword\"\=\)'
ANYTHING='.*'
SEARCH_PATTERN=${PARAM_TO_CHANGE}${ANYTHING}
REPLACE_PATTERN="\"${ADMIN_PASSWD}\""
sed 's|$SEARCH_PATTERN|\\1\\$REPLACE_PATTERN|' /tmp/config.ini
[vivek@localhost ~]$
Assume the value received via awk command is stored in the ADMIN_PASSWD variable. And the value of $ADMIN_PASSWORD is captured while running the shell script in debug format.
[vivek@localhost ~]$ bash -x /tmp/a.sh
+ echo 'Fetching the admin encrypted Password from the /etc/shadow file'
Fetching the admin encrypted Password from the /etc/shadow file
++ sudo awk -F: '/admin/ { print $2}' /etc/shadow
+ ADMIN_PASSWD='$6$2qeizEuc$082jxqqkpJJPQuUbJD/aO2zA5SSmibjSY7cEDpNxj9eQhKxhO2NT40O9FmQEe1TbN0KWTNbt.9EZxMQXZNo1A0'
+ PARAM_TO_CHANGE='^\(\"adminsyspassword\"\=\)'
+ ANYTHING='.*'
+ SEARCH_PATTERN='^\(\"adminsyspassword\"\=\).*'
+ REPLACE_PATTERN='"$6$2qeizEuc$082jxqqkpJJPQuUbJD/aO2zA5SSmibjSY7cEDpNxj9eQhKxhO2NT40O9FmQEe1TbN0KWTNbt.9EZxMQXZNo1A0"'
+ echo 'sed '\''s|^\(\"adminsyspassword\"\=\).*|\1\"$6$2qeizEuc$082jxqqkpJJPQuUbJD/aO2zA5SSmibjSY7cEDpNxj9eQhKxhO2NT40O9FmQEe1TbN0KWTNbt.9EZxMQXZNo1A0"|'\'' /tmp/config.ini'
sed 's|^\(\"adminsyspassword\"\=\).*|\1\"$6$2qeizEuc$082jxqqkpJJPQuUbJD/aO2zA5SSmibjSY7cEDpNxj9eQhKxhO2NT40O9FmQEe1TbN0KWTNbt.9EZxMQXZNo1A0"|' /tmp/config.ini
+ sed 's|$SEARCH_PATTERN|\\1\\$REPLACE_PATTERN|' /tmp/config.ini
[site]
"name"="Site 0"
[node]
"name"="Node 0"
[systemsecurity]
[systemsecurity\webserver]
"level"="high"
[tacacs]
"interface"="0"
"enable"=dword:00000000
"port"=dword:00000031
"allowPersistentTCPConnections"=dword:00000001
[misc]
"adminsyspassword"="$ADMIN_PASSWD"
"adminpassword"="MD5-71293d5fd498273ca795a80ea9fa73c7"
"adminuser"="admin"
[vivek@localhost ~]$
I am not able to understand why the sed command is not working for me.
Where I am going wrong?
Update:
I tried adding the echo command in the shell script, to see what is actually passed.
echo "sed 's|$SEARCH_PATTERN|\\1\\$REPLACE_PATTERN|' /tmp/config.ini"
sed 's|$SEARCH_PATTERN|\\1\\$REPLACE_PATTERN|' /tmp/config.ini
Now expanded version is working, but How to make it in shell script?
[vivek@localhost ~]$ sed 's|^\(\"adminsyspassword\"\=\).*|\1\"$6$2qeizEuc$082jxqqkpJJPQuUbJD/aO2zA5SSmibjSY7cEDpNxj9eQhKxhO2NT40O9FmQEe1TbN0KWTNbt.9EZxMQXZNo1A0"|' /tmp/config.ini
[site]
"name"="Site 0"
[node]
"name"="Node 0"
[systemsecurity]
[systemsecurity\webserver]
"level"="high"
[tacacs]
"interface"="0"
"enable"=dword:00000000
"port"=dword:00000031
"allowPersistentTCPConnections"=dword:00000001
[misc]
"adminsyspassword"="$6$2qeizEuc$082jxqqkpJJPQuUbJD/aO2zA5SSmibjSY7cEDpNxj9eQhKxhO2NT40O9FmQEe1TbN0KWTNbt.9EZxMQXZNo1A0"
"adminpassword"="MD5-71293d5fd498273ca795a80ea9fa73c7"
"adminuser"="admin"
[vivek@localhost ~]$
Thanks for all help. I got the solution, Need to change in this format.
[vivek@localhost ~]$ cat /tmp/a.sh
#! /bin/sh
echo "Fetching the admin encrypted Password from the /etc/shadow file"
ADMIN_PASSWD=`sudo awk -F: '/admin/ { print $2}' /etc/shadow`
PARAM_TO_CHANGE='^\(\"adminsyspassword\"\=\)'
ANYTHING='.*'
SEARCH_PATTERN=${PARAM_TO_CHANGE}${ANYTHING}
REPLACE_PATTERN="\"${ADMIN_PASSWD}\""
sed "s|$SEARCH_PATTERN|\\1\\$REPLACE_PATTERN|" /tmp/config.ini