【发布时间】:2015-03-09 12:06:36
【问题描述】:
我正在尝试创建一个脚本来“自动”修改 build.prop 文件。 我希望脚本检查我定义的条目,并且: 1.如果他们不存在添加他们 2. 如果它们确实存在,请检查该值,如果它与我定义的不匹配,请修改它。
我创建了一个脚本。 它运行但什么也不输出,不改变 build.prop
我已附上脚本。对吗?
#!/system/bin/sh
# Definitions
file=/system/build.prop
tmpf=/system.buildprop.bak
line_list="wifi.supplicant_scan_interval=120 ro.sf.lcd_density=480"
# Function to get args as needed for loop
getargs() {
par=$1
line=`echo $par |cut -d"=" -f1`
arg=`echo $par |cut -d"=" -f2`
}
# Loop to make all changes in line_list
for x in $line_lst; do
# Get all needed arguments
getargs $x
# Write this change to a tmp file to check on it
oldarg=`grep $line $file |cut -d"=" -f2`
sed "s/$line=.*/$line=${arg}/g" $file > $tmpf
# Check if the change was made
chknewarg=`grep $line $tmpf |cut -d"=" -f2`
if [ "$chknewarg" = "$arg" ]; then
cp $tmpf $file
if [ -f $tmpf ]; then
rm $tmpf
fi
echo "File edited"
else
if [ -f $tmpf ]; then
rm $tmpf
fi
echo "Expected $arg, got $chknewarg instead"
exit 1
fi
# If it doesn't exist at all append it to the file
chkexists=`grep -c $line $file`
if [ $chkexists -eq 0 ]; then
echo "$x" >> $file
fi
done
exit 0
【问题讨论】:
-
使用
sh -x运行脚本以更轻松地调试它。此外,将代码缩进一点不会有什么坏处,它肯定会使其更具可读性。