Search code examples
androidfor-loopif-statementshtermux

Shell script with if-else returns command not found error while executing the last line


I have a shell script to set the CPU frequency to maximum on all cores on Android devices. The contents are:

#!/system/bin/sh
cpucores=`grep -c ^processor/proc/cpuinfo`
cpusint=`echo $((cpucores-1))`
littlefreq=`cat /sys/devices/system/cpu/cpu$cpusint/cpufreq/scaling_max_freq`
bigfreq=`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq`
cpuhalf=$((cpucores/2))
cpuhalfint=`echo $((cpufalf-1))`
if [$bigfreq  -ne $littlefreq]; then
        for i in `seq 0 $cpuhalfint`; do
            chmod 0644 /sys/devices/system/cpu/cpu$i/cpufreq/scaling_governor
            echo "performance" > /sys/devices/system/cpu/cpu$i/cpufreq/scaling_governor
            chmod 0644 /sys/devices/system/cpu/cpu$i/cpufreq/scaling_min_freq
            echo $bigfreq > /sys/devices/system/cpu/cpu$i/cpufreq/scaling_min_freq
        done
        for x in `seq $cpuhalfint $cpusint`; do
            chmod 0644 /sys/devices/system/cpu/cpu$x/cpufreq/scaling_governor
            echo "performance" > /sys/devices/system/cpu/cpu$x/cpufreq/scaling_governor
            chmod 0644 /sys/devices/system/cpu/cpu$x/cpufreq/scaling_min_freq
            echo $littlefreq > /sys/devices/system/cpu/cpu$x/cpufreq/scaling_min_freq
        done
else
        for i in `seq 0 $cpusint`; do
            chmod 0644 /sys/devices/system/cpu/cpu$i/cpufreq/scaling_governor
            echo "performance" > /sys/devices/system/cpu/cpu$i/cpufreq/scaling_governor
            chmod 0644 /sys/devices/system/cpu/cpu$i/cpufreq/scaling_min_freq
            echo $bigfreq > /sys/devices/system/cpu/cpu$i/cpufreq/scaling_min_freq
        done
fi

The script works fine, but it returns this:

./setcpu.sh[28]: [1248000: not found

(1248000 is my CPU frequency, and setcpu.sh is the name of the shell script file)

Whereas it shouldn't output ANYTHING at all. Does someone know why this is happening?

Thanks for answering.


Solution

  • [ is an alias for test. It's a command name, so it must be separated from its arguments just like test or echo needs.

    ] must also be provided as an entire argument.

    Therefore,

    if [$bigfreq  -ne $littlefreq]
    

    needs to be replaced with

    if [ $bigfreq  -ne $littlefreq ]
    

    Thanks to @oguz ismail.