Whooh, that's a long script. I don't think I'm going to bother to read through it completely at this point.
But after a quick glance through it, I can give you some starting advice.
1)
As I asked before, do you need for this to be a portable, posix-based script? You called it a "bash" script, but
#!/bin/sh means it runs in posix-compliant, lowest-common-denominator mode. If would certainly help things if you can specify the use of
bash specifically (
#!/bin/bash), and take advantage of its more powerful features.
And if so, what version of bash do you have available?
2)
It's considered bad practice to combine code and data in the same script, at least outside of trivial amounts. Most of your
heredoc blocks would be better served if stored in separate files, and sourced or redirected as necessary. You could then make edits to the data files without worrying about corrupting the script itself, not to mention that it would also help improve readability.
3)
Speaking of which, a bit more effort on formatting would be helpful. A few more blank line breaks to separate sections and some clearer indentation would make things more readable and easily debuggable.
And
comments! You really need to add some comments explaining the
process of your script, i.e. what the code is supposed to be doing and how. It may seem perfectly clear to you now, but I guarantee you that a year or two down the line you're going to be wondering "just WTF is this bit here for?".
4)
You have at least one very long series of
sed commands, which should probably also be set aside as a single separate external script to be called on when needed (using sed's
-f option). Or at the very least use sed's
-e option to specify multiple expressions in a single command.
The number of individual expressions could be greatly reduced too, with effective regex use. To pull out a single example (after removing the incorrect backslash terminators, as I demonstrated in my last post):
Code:
sed '/net.rmnet0.dns1/d' |
sed '/net.rmnet0.dns2/d' |
sed '/net.tiwlan0.dns1/d' |
sed '/net.tiwlan0.dns2/d' |
#revised:
sed -r -e '/net[.](rmnet|tiwlan)0[.]dns[12]/d' |
(Not to mention that, as written, it involves a
Useless Use Of Cat. sed can read the filename directly.)
5)
FUNCTIONS! I see a lot of unnecessarily duplicated command lines that would be better handled through the use of a few functions. Write once, use many.
6)
Even outside of functions, there's an overwhelming number of areas where a few loops and variables would save you a huge amount of unnecessary duplication. I'd say most of the script, in fact. For example there's a series like this:
Code:
if [ "`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq`" -eq 200000 ] && [ "`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq`" -eq 1200000 ]; then
echo "4" > /sys/devices/system/cpu/cpufreq/lulzactive/pump_up_step;
elif [ "`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq`" -eq 200000 ] && [ "`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq`" -eq 1400000 ]; then
echo "5" > /sys/devices/system/cpu/cpufreq/lulzactive/pump_up_step;
...etc...
You should do the "
cat"-ing only ONCE, at the beginning, save the values into variables, and test those. (and if you use bash, you don't even need
cat, as
< behaves like cat when inside
$() ).
Code:
scaling_min_freq=$( </sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq )
scaling_max_freq=$( </sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq )
pump_up_step="/sys/devices/system/cpu/cpufreq/lulzactive/pump_up_step"
if (( scaling_min_freq == 200000 && scaling_max_freq == 1200000 )); then
echo "4" > "$pump_up_step"
elif (( scaling_min_freq == 200000 && scaling_max_freq == 1400000 )); then
echo "5" > "$pump_up_step"
...etc...
7)
As demonstrated in the above example, if you can use bash, the old
[..] test brackets should ideally be replaced with
[[..]] or
((..)) depending on if it's a string/file comparison or an integer evaluation.
8)
There are also areas that look like they could benefit from the use of arrays. Specifically there's one area where you have a series of variables,
$PROCESS_1...
$PROCESS_18, that could better be handled with an array and a few simple loops. Again, this would require the use of bash, as the posix specification doesn't include arrays.
Another example is this:
Code:
if [ -f "/system/build.prop.unsmurfed" ]; then
rm /system/build.prop.unsmurfed;
fi
$sleep
if [ -f "/system/bin/build.prop.unsmurfed" ]; then
rm /system/bin/build.prop.unsmurfed;
fi
$sleep
...etc...
But with an array:
Code:
smurfiles=(
/system/build.prop.unsmurfed
/system/bin/build.prop.unsmurfed
/data/local.prop.unsmurfed
...etc...
)
for file in "${smurfiles[@]}"; do
[[ -f $file ]] && rm "$file"
sleep
done
Anyway, that's enough for now. Try applying the suggestions I've made so far, and post back with the revised script. Then we can have another go at optimizing it. I'm sure that with just a bit of effort your script will be much cleaner, lighter, and faster.
Check out these links too for more advice:
http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
http://www.grymoire.com/Unix/Sed.html
http://www.grymoire.com/Unix/Regular.html