Search code examples
bashgrepopenwrt

Line count from grep into count range variable Bash OpenWRT


I am trying to get a range in the format of 0..# to be used in a for loop. I currently have this but it's not grabbing the count properly. I am trying to run this on OpenWRT.

count=echo $(( ${grep -c BSS /tmp/ScannedAPs.txt} - 1 ))

The Error: ${grep -c BSS /tmp/ScannedAPs.FLT}: bad substitution

range="0..$count"

echo "Count:"
echo "$count"
echo "Time for range"
echo "$range"

for index in $(eval echo "{$range}")
do
echo "${BSS[index]} ${SSID[index]} ${CHAN[index]}" >> /tmp/ScannedAPs_Parsed.txt
done

Solution

  • As you've indicated, the error is in:

    count=echo $(( ${grep -c BSS /tmp/ScannedAPs.txt} - 1 ))
    

    The proper syntax for Command Substitution is $(command). Say:

    count=$(( $(grep -c BSS /tmp/ScannedAPs.txt) - 1 ))
    

    Moreover, you could avoid using eval in:

    for index in $(eval echo "{$range}")
    

    by saying:

    for index in $(seq 0 $count)
    

    Alternatively, you could loop (as suggested by ruakh) by saying:

    for ((index=0;index<=count;index++)); do
      # Do something here
    done