【问题标题】:editing android build.prop with a script使用脚本编辑 android build.prop
【发布时间】: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 运行脚本以更轻松地调试它。此外,将代码缩进一点不会有什么坏处,它肯定会使其更具可读性。

标签: android shell scripting


【解决方案1】:

我做了更多的演奏并从根本上改变了剧本。

#/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 变量循环来竞争动作?

【讨论】:

  • 'mount: invalid argument 是因为只读重新挂载应该是mount -o remount,ro /system
猜你喜欢
  • 1970-01-01
  • 2014-12-15
  • 1970-01-01
  • 2014-08-29
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 2012-08-03
  • 1970-01-01
相关资源
最近更新 更多