这是我要编辑的XML文件
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<boolean name="public_checkins" value="false" />
<string name="checkins">[{"storeName":"Rundle Street","prize":"price_fwh","checkinTime":1352717951195,"prizeClaimed":false,"storeId":57,"expired":false},{"storeName":" Street","prize":"price_fmf","checkinTime":1352717723886,"prizeClaimed":false,"storeId":57,"expired":false}]</string>
*<string name="uuid">30212345-0c1e-dcb-974e-5effa7f016be</string>*
</map>
我正在尝试创建一个shell脚本来编辑字符串UUID,并在每次运行脚本时用随机生成的数字序列替换UUID。
以下是我想到的脚本。
#!/system/bin/sh
set number=$RANDOM
echo "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<boolean name="public_checkins" value="false" />
<string name="checkins">[{"storeName":" Street","prize":"price_fwh","checkinTime":1352717951195,"prizeClaimed":false,"storeId":57,"expired":false},{"storeName":"Rundle Street","prize":"price_fmf","checkinTime":1352717723886,"prizeClaimed":false,"storeId":57,"expired":false}]</string>
<string name="uuid">302$number-0c1e-dcb-974e-5effa7f016be</string>
</map>
" > /data/data/com.app/shared_prefs/app.xml
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="uuid">302%1$s-0c1e-dcb-974e-5effa7f016be</string>
</map>
现在您可以使用此格式来获取如下
的字符串String uuid = String.format(getResources().getString(R.string.uuid), YourUUIDNumber);
您将收到完整的字符串
302xxxxxx-0c1e-dcb-974e-5effa7f016be
其中xxxxxxx是您以字符串格式方法传递的值。
perl更适合此
file=/data/data/com.app/shared_prefs/app.xml
mv $file $file.bak
{
arr=({0..9} {a..f})
while IFS=> read -d< tag str; do
if [[ $tag = *'name="uuid"'* ]]; then
randomstr=
for ((i=0;i<32;i++)); do
if ((i%4==0 && i/4>=2 && i/4<6)); then
randomstr=$randomstr-
fi
randomstr=$randomstr${arr[RANDOM%16]}
done
printf "%s" "$tag${str:+>}$randomstr<"
else
printf "%s" "$tag${str:+>}$str<"
fi
done
printf "%sn" "$tag>"
} <$file.bak >$file
# rm $file.bak #