我在/tmp 位置有一个名为 config.ini 的文件。内容如下。
[vivek@localhost ~]$ cat /tmp/config.ini
[site]
"name"="Site 0"
[node]
"name"="Node 0"
[systemsecurity]
[systemsecuritywebserver]
"level"="high"
[tacacs]
"interface"="0"
"enable"=dword:00000000
"port"=dword:00000031
"allowPersistentTCPConnections"=dword:00000001
[misc]
"adminsyspassword"="$ADMIN_PASSWD"
"adminuser"="admin"
[vivek@localhost ~]$
我需要将"管理员密码"替换为/etc/shadow 文件值中的值。我使用以下带有awk命令的shell脚本来获取ini文件的"adminsyspassword"键的新值。
[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 ~]$
假设通过 awk 命令接收的值存储在 ADMIN_PASSWD 变量中。并且 $ADMIN_PASSWORD 的值是在以调试格式运行 shell 脚本时捕获的。
[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]
[systemsecuritywebserver]
"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 ~]$
我无法理解为什么 sed 命令对我不起作用。
我哪里出错了?
更新:
我尝试在 shell 脚本中添加 echo 命令,以查看实际传递的内容。
echo "sed 's|$SEARCH_PATTERN|\1\$REPLACE_PATTERN|' /tmp/config.ini"
sed 's|$SEARCH_PATTERN|\1\$REPLACE_PATTERN|' /tmp/config.ini
现在扩展版本可以工作了,但是如何在shell脚本中制作它?
[vivek@localhost ~]$ sed 's|^("adminsyspassword"=).*|1"$6$2qeizEuc$082jxqqkpJJPQuUbJD/aO2zA5SSmibjSY7cEDpNxj9eQhKxhO2NT40O9FmQEe1TbN0KWTNbt.9EZxMQXZNo1A0"|' /tmp/config.ini
[site]
"name"="Site 0"
[node]
"name"="Node 0"
[systemsecurity]
[systemsecuritywebserver]
"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 ~]$
感谢所有的帮助。我得到了解决方案,需要以这种格式进行更改。
[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
调用 sed
命令时使用单引号会阻止 shell 扩展SEARCH_PATTERN
并REPLACE_PATTERN
shell 变量。这会导致这些字符串从字面上传递到sed
程序。
sed 's|$SEARCH_PATTERN|\1$REPLACE_PATTERN|' /tmp/config.ini
要使 shell 进行参数扩展,应删除单引号或用双引号替换。我将简化 sed
命令以搜索/tmp/config.ini
中的文字字符串$ADMIN_PASSWD
并将其替换为 ADMIN_PASSWD
shell 变量的内容:
sed s/$ADMIN_PASSWD/$ADMIN_PASSWD/ /tmp/config.ini