0

First things first, here's the code:

# 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;

Now, orignally in the loop I was accessing the array like this: prop=$PROPARR[$i]; arg=$PROPVALARR[$i];

but it was writing weird code to the end of build.prop, and it was only the 1st item in the array(s)

So, then I tried it like I posted, and now I am getting a syntax error near unexpected token '('

So with the first issue that I was having, I feel it is safe to assume that #1 the loop isn't correctly written, #2 the text I am attempting to write is incorrectly written, #3 is the proper way to access the items

So the question is, how can I fix this so the loop loops correctly through the array, and writes the proper lines to build.prop (or updates the line if found)

1
  • Define yourself an array array=(hello world) and 1. print the second element, 2. loop over and print each element. When you're not familiar with a language, it really helps to start small and fixing one error at a time rather than writing a complete program and then trying to fix everything at once. Commented Jun 26, 2014 at 17:23

2 Answers 2

1

There was some problems with how you were referencing the array values. The following code should work.

# 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;

Edit: Ok you need some changes to your sed statements as well. Also to write /System/ you will need root permissions. Here is the updated code:

# 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;

It is also worth noting that the -i option to the sed statement will be overwritten with every iteration of the for loop. So you won't have a backup of the original after running this script. Perhaps manually take a backup before running the script or add it in as part of it.

Sign up to request clarification or add additional context in comments.

4 Comments

ok, no more errors, however it's not writing the changes to build.prop
I will try your edit. Not worried about backing up, and yeah this needs root anyways, due to this happenning during the flashing of a mod in a custom recovery :)
YIKES! now instead of modyfing a exiting property it is appending to it... for instance: dalvik.heapstartsize=16m=16m is what I am ending up with
Did you run the script on the file twice? That would end up with a double append. OK we can fix that too. Modify the line "sed -i.bak s/$prop/$prop=$arg/ $FILE;" to be "sed -i.bak s/$prop.*/$prop=$arg/ $FILE;". That should fix things. I've edited the post - the second script has the additional pattern match now.
1

A better solution, cleaner and more readable, that leverages associative arrays, a feature built-in in 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;

In this snippet I'm just focusing on how to loop over an associative array. You should be able to hook your own logic into the loop.

For a full reference:

http://www.gnu.org/software/bash/manual/bashref.html#Arrays

3 Comments

declare -A: invalid option and am getting syntax error: invalid arithmetic operator (error token is ".tcp.buffersize.default") where the .tcp.buffersize.default changes for each line, but it happens for each line
:( no associative arrays supported in your bash version
apparently not. I know there is for Linux, but I'm sure bash for Android is crippled

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.