Asuswrt-Merlin-Linux-Shell-Scripts
Asuswrt-Merlin-Linux-Shell-Scripts copied to clipboard
Size of Variable reporting inaccurate?
Minor quibble for a variable of length 2999
'wc -m' or 'wc -c' appears to incorrectly report the NVRAM variable?
e.g.
echo $(nvram get jffs2_on)
1
Now print the size using your method
echo $(nvram get jffs2_on | wc -m)
2
Thank you for the feedback. Looks like wc is counting the return or extra line? I updated the code to account for the extra character.
word_count=$(nvram get dhcp_staticlist | wc -m) # wc appears to count line return or extra line? word_count=$((word_count - 1))
I'll do more searching online to see how others have resolved the issue.
Yes it's just because the string returned by "nvram get" is terminated with a linefeed.
admin@RT-AC68U:/# nvram get jffs2_on
1
admin@RT-AC68U:/# nvram get jffs2_on | hd
00000000 31 0a |1.|
I now use the following (having been prompted by you to review my code to make it POSIX complaint and more efficient ;-)
`NVRAM_DATA=$(nvram get "$NVRAM_VAR")
NVRAM_DATA_SIZE=${#NVRAM_DATA}`
although previously I used the slower (i.e. calling an external utility)
NVRAM_DATA_SIZE=$(echo "$NVRAM_DATA" | awk '{print length()}')
which conveniently ignores the LF character!