首先,首先是代码:
# build.prop Tweaks
FILE=/system/build.prop
#Properties to change Array
PROPARR=('net.tcp.buffersize.default' 'net.tcp.buffersize.wifi' 'net.tcp.buffersize.umts' 'net.tcp.buffersize.gprs' 'net.tcp.buffersize.edge' 'wifi.supplicant_scan_interval' 'ro.HOME_APP_ADJ' 'dalvik.vm.dexopt-flags' 'dalvik.vm.execution-mode' 'dalvik.vm.heapstartsize' 'dalvik.vm.heapgrowthlimit' 'dalvik.vm.heapsize' 'dalvik.vm.heaptargetutilization' 'dalvik.vm.heapminfree' 'dalvik.vm.heapmaxfree' 'debug.sf.hw');
#Property Values
PROPVALARR=('4096,87380,256960,4096,16384,256960' '4096,87380,256960,4096,16384,256960' '4096,87380,256960,4096,16384,256960' '4096,87380,256960,4096,16384,256960' '4096,87380,256960,4096,16384,256960' '300' '1' 'm=y,v=n,o=v,u=n' 'init:jit' '16m' '128m' '512m' '0.75' '12m' '16m' '1');
# this variable is just a place holder for the future line number of the prop values
lineNum=
# Loop through the properties I want to change
for i in $PROPARR; do
prop=$PROPARR($i);
arg=$PROPVALARR($i);
if grep -Fq $prop $FILE ; then
lineNum=`sed -n "/${prop}/=" $FILE`;
sed -i "${lineNum} c${prop}=${arg}" $FILE;
else
echo $prop=$arg >> $FILE;
fi;
done;
现在,在循环中,我正在访问数组:prop=$PROPARR[$i];
arg=$PROPVALARR[$i];
但它在build.prop
的末尾写了奇怪的代码,它只是数组中的第一项
所以,然后我像我发布的那样尝试了,现在我得到了syntax error near unexpected token '('
因此,对于我遇到的第一个问题,我觉得可以安全地假设#1循环没有正确写入,#2我试图写入的文本写得不正确,#3是正确的方式来访问项目
所以问题是,如何解决这个问题,以便循环在数组中正确循环,并将正确的行写入build.prop
(如果找到则更新行)
答案 0 :(得分:1)
您引用数组值的方式存在一些问题。以下代码应该可以使用。
# build.prop Tweaks
FILE=/System/build.prop
#Properties to change Array
PROPARR=('net.tcp.buffersize.default' 'net.tcp.buffersize.wifi' 'net.tcp.buffersize.umts' 'net.tcp.buffersize.gprs' 'net.tcp.buffersize.edge' 'wifi.supplicant_scan_interval' 'ro.HOME_APP_ADJ' 'dalvik.vm.dexopt-flags' 'dalvik.vm.execution-mode' 'dalvik.vm.heapstartsize' 'dalvik.vm.heapgrowthlimit' 'dalvik.vm.heapsize' 'dalvik.vm.heaptargetutilization' 'dalvik.vm.heapminfree' 'dalvik.vm.heapmaxfree' 'debug.sf.hw');
#Property Values
PROPVALARR=('4096,87380,256960,4096,16384,256960' '4096,87380,256960,4096,16384,256960' '4096,87380,256960,4096,16384,256960' '4096,87380,256960,4096,16384,256960' '4096,87380,256960,4096,16384,256960' '300' '1' 'm=y,v=n,o=v,u=n' 'init:jit' '16m' '128m' '512m' '0.75' '12m' '16m' '1');
# this variable is just a place holder for the future line number of the prop values
lineNum=
# Loop through the properties I want to change
for i in ${!PROPARR[@]}; do
prop=${PROPARR[$i]};
arg=${PROPVALARR[$i]};
if grep -Fq $prop $FILE ; then
lineNum=`sed -n "/${prop}/=" $FILE`;
sed -i "${lineNum} c${prop}=${arg}" $FILE;
else
echo $prop=$arg >> $FILE;
fi;
done;
编辑:好的,您还需要对sed语句进行一些更改。写入/ System /你也需要root权限。这是更新的代码:
# build.prop Tweaks
FILE=/System/build.prop
#Properties to change Array
PROPARR=('net.tcp.buffersize.default' 'net.tcp.buffersize.wifi' 'net.tcp.buffersize.umts' 'net.tcp.buffersize.gprs' 'net.tcp.buffersize.edge' 'wifi.supplicant_scan_interval' 'ro.HOME_APP_ADJ' 'dalvik.vm.dexopt-flags' 'dalvik.vm.execution-mode' 'dalvik.vm.heapstartsize' 'dalvik.vm.heapgrowthlimit' 'dalvik.vm.heapsize' 'dalvik.vm.heaptargetutilization' 'dalvik.vm.heapminfree' 'dalvik.vm.heapmaxfree' 'debug.sf.hw');
#Property Values
PROPVALARR=('4096,87380,256960,4096,16384,256960' '4096,87380,256960,4096,16384,256960' '4096,87380,256960,4096,16384,256960' '4096,87380,256960,4096,16384,256960' '4096,87380,256960,4096,16384,256960' '300' '1' 'm=y,v=n,o=v,u=n' 'init:jit' '16m' '128m' '512m' '0.75' '12m' '16m' '1');
# this variable is just a place holder for the future line number of the prop values
# Loop through the properties I want to change
for i in ${!PROPARR[@]}; do
prop=${PROPARR[$i]};
arg=${PROPVALARR[$i]};
if grep -Fq $prop $FILE ; then
sed -i.bak s/$prop.*/$prop=$arg/ $FILE;
else
echo $prop=$arg >> $FILE;
fi;
echo "$prop $arg"
done;
值得注意的是,for循环的每次迭代都会覆盖sed语句的-i选项。因此,在运行此脚本后,您将无法备份原始文件。也许在运行脚本之前手动进行备份,或者将其作为其一部分添加。
答案 1 :(得分:1)
更好的解决方案,更清晰,更具可读性,利用关联数组,这是Bash中内置的功能。
declare -A PROPERTIES
PROPERTIES['net.tcp.buffersize.default']="4096,87380,256960,4096,16384,256960"
PROPERTIES['net.tcp.buffersize.wifi']="4096,87380,256960,4096,16384,256960"
PROPERTIES['net.tcp.buffersize.umts']="4096,87380,256960,4096,16384,256960"
PROPERTIES['net.tcp.buffersize.gprs']="4096,87380,256960,4096,16384,256960"
PROPERTIES['net.tcp.buffersize.edge']="4096,87380,256960,4096,16384,256960"
PROPERTIES['wifi.supplicant_scan_interval']=300
PROPERTIES['ro.HOME_APP_ADJ']=1
PROPERTIES['dalvik.vm.dexopt-flags']=m=y,v=n,o=v,u=n
PROPERTIES['dalvik.vm.execution-mode']=init:jit
PROPERTIES['dalvik.vm.heapstartsize']=16m
PROPERTIES['dalvik.vm.heapgrowthlimit']=128m
PROPERTIES['dalvik.vm.heapsize']=512m
PROPERTIES['dalvik.vm.heaptargetutilization']=0.75
PROPERTIES['dalvik.vm.heapminfree']=12m
PROPERTIES['dalvik.vm.heapmaxfree']=16m
PROPERTIES['debug.sf.hw']=1
# Loop through the properties I want to change
for prop in ${!PROPERTIES[@]}; do
arg=${PROPERTIES["$prop"]}
echo $prop = $arg
done;
在这个片段中,我只关注如何循环关联数组。 您应该能够将自己的逻辑挂钩到循环中。
如需完整参考: