0

In a bash shell program, I'm capturing the 'i2cdetect -y 1' into a string (or, at least I think I am...):

i2cdetect -y 1 > i2cdetect_string

I'm able to use awk and parse through the string for specific information so, I know it's there.

However, if I try to get it's length:

echo "The i2cdetect_string length is: ${#i2cdetect_string}" 

I get a 0 result!

I also tried:

echo "The i2cdetect_string length is: ${#i2cdetect_string[@]}" 

thinking it may be an array but, that didn't work.

What's going on here? Are there special characters in the i2cdetect string that confuse the length capture algorithm?

Thanks for any help - or insight - provided.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Eebigdog
  • 5
  • 2
  • 1
    `> i2cdetect_string` is writing the output to a ***file*** named `i2cdetect_string`; `echo ${#i2cdetect_string[@]}` ... couple issues ... there is no variable with this name so length of an undefined variable is `0` ... the `[@]` is used when referencing an array, which in this case is (also) undefined – markp-fuso Oct 19 '21 at 16:50
  • Why do you think the problem is with the length operator? Did you try a simple `echo "$i2cdetect_string"` to see what its value is? Then you would have seen that you didn't actually set the variable. – Barmar Oct 19 '21 at 16:51
  • perhaps back up a bit and tell us what you intend to do with the output from `i2cdetect -y 1`? right now you're writing to a file (which `awk` is successfully reading from); what is the purpose of determining the `length` of the output? – markp-fuso Oct 19 '21 at 16:53

0 Answers0