使用脚本编辑android build.prop

时间:2015-01-11 17:20:38

标签: android shell scripting

我正在尝试创建一个脚本来“自动”修改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

1 个答案:

答案 0 :(得分:0)

我已经多做了一些游戏并从根本上改变了剧本。

#/system/bin/sh

 mount -o remount,rw /system

FILE=/system/build.prop
TMPFILE=$FILE.tmp

line1=wifi.supplicant_scan_interval
line2=ro.sf.lcd_density
line3=ro.media.enc.jpeg.quality

line1Arg=120
line2Arg=390
line3Arg=100

lineNum=

prop=$line1     
arg=$line1Arg   
if grep -Fq $prop $FILE ; then  
lineNum=`sed -n "/${prop}/=" $FILE`   
echo $lineNum
sed -i "${lineNum} c${prop}=${arg}" $FILE   
else
echo "$prop does not exist in build.prop"   
echo "appending to end of build.prop"
echo $prop=$arg >> $FILE
fi

prop=$line2     
arg=$line2Arg     
if grep -Fq $prop $FILE ; then  
lineNum=`sed -n "/${prop}/=" $FILE`    
echo $lineNum
sed -i "${lineNum} c${prop}=${arg}" $FILE    
else
echo "$prop does not exist in build.prop"   
echo "appending to end of build.prop"
echo $prop=$arg >> $FILE
fi

prop=$line3     
arg=$line3Arg     
if grep -Fq $prop $FILE ; then  
lineNum=`sed -n "/${prop}/=" $FILE`    
echo $lineNum
sed -i "${lineNum} c${prop}=${arg}" $FILE    
else
echo "$prop does not exist in build.prop"   
echo "appending to end of build.prop"
echo $prop=$arg >> $FILE
fi

mount -o remount,r /system

但是我收到'mount:invalid argument'错误?

此外,如何循环使用line和lineArg变量来竞争操作?