Shell Script中的间接变量引用
function write_xml()
{
change=${!1}
if [ $change ]; then
if [ $change = "sr_null" ];then
#sed -i "s/\"$control\">.*</\"$control\"></" $path
sed -i "/\"$1\"/s/\">.*</\"></" $path
else
#sed -i "s/\"$control\">.*</\"$control\">$change</" $path
sed -i "/\"$1\"/s/\">.*</\">$change</" $path
fi
echo "$1 = $change"
#else
#echo "$control is not be setted"
fi
}
我们具体看一下各指令的含义:
change=${!1} --> “1”代表第一个参数,"${!1}"代表间接引用"1"的值。比如:a=2, b=${!a},那么b=2
sed -i "/\"$1\"/s/\">.*</\">$change</" $path
其中 sed -i 代表字符串的查找替换。"$1"代表获取第一个参数,"\">.*</"代表的是字符串:">.*</,用">$change</来替换。 查找文件中"$1"字符串,把该字符串后面的字符用$change所代表的内容替换。
if [ $change ]; then #判断是否获取到值。
if [ $change = "sr_null" ];then #判断获取到的值是否为空值。
以上内容仅代表个人理解,如有不适之处,还望不吝赐教!