Asuswrt-Merlin-Linux-Shell-Scripts icon indicating copy to clipboard operation
Asuswrt-Merlin-Linux-Shell-Scripts copied to clipboard

Size of Variable reporting inaccurate?

Open MartineauUK opened this issue 5 years ago • 3 comments

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

MartineauUK avatar Jun 29 '19 12:06 MartineauUK

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.

Xentrk avatar Jun 30 '19 01:06 Xentrk

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.|

ColinTaylorUK avatar Jun 30 '19 02:06 ColinTaylorUK

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!

MartineauUK avatar Jun 30 '19 06:06 MartineauUK