2

I am making a bash script. I first grab a line with grep from a realtime updating output (top, airodump-ng) then I am trying to parse that line and extract certain substrings. My problem is the ${VAR:start:length} syntax does not work and is unpredictable. I will show some examples,

My original unparsed string is this, I am trying to extract the two Mac addresses (which I made up)

VAR="12:34:56:78:91:23 23:45:67:89:12:34 -37 24e-24e 0 23"

In the script, var is defined by VAR="airodump-ng | grep ... but for this post it is a string literal.

So maybe there is some random whitespace characters messing with it from grep. Anyways, when I try to take substrings of VAR this is what happens,

SUB=${VAR:1:25}
12:34:56:78:91:23 23:45:

Good

SUB${VAR:1:24}
12:34:56:78:91:23 23:45

Good

SUB${VAR:1:23}
12:34:56:78:91:

Bad

You can see now out of nowhere the substring is much shorter than expected, going from 24->23 does not make it one character shorter but 9 shorter. This patter continues, for example, 12, 13, 14 work but 15 is way to short again. The same thing happens with the cut command echo $VAR | cut -c1-25 with the same indexes.

So how can I fix this and why is it happening. Or are there any better solutions to extract these substrings?

                                        .
  • How do you assign and print the substring? You show what I think are assignments with a missing `=`, and then the content of the variable, but how do you generate that string? – Benjamin W. Dec 03 '22 at 03:55
  • I do ```SUB=${VAR:1:23}``` then just ```echo $SUB```, but I intend to use SUB as an argument to future commands in the bash script as in ```CMD $SUB $SUB2 ...``` @BenjaminW. – Lorde213423432e43434 Dec 03 '22 at 04:03
  • 1
    If you run `declare -p VAR` on your original variable and then with `declare -p SUB`, do you see any unexpected characters? Also, beware: [I just assigned a variable, but echo $variable shows something else](https://stackoverflow.com/q/29378566/3266847) – Benjamin W. Dec 03 '22 at 04:08
  • Also, your outputs don't seem right even for the good cases; `${VAR:1:...}` should cut off the first character. – Benjamin W. Dec 03 '22 at 04:10
  • Thats also part of the problem, the first characters seem to constantly change. I ran this for loop, ```for ((i = 0; i<${#RAW}; i++)); do echo "$i ------ ${RAW:0:$i}" done``` and this is the first few lines, ```0 ------ 1 ------ ------ 5 ------ ------ 9 ------ 10 ------ 2 11 ------ 24 12 ------ 24:``` Additionally it changes, sometimes it will correctly format and sometimes it does not. I tried running it in a new shell to clear any stdout, "bash -c "..."" but it normally works the first time then stops. @BenjaminW. – Lorde213423432e43434 Dec 03 '22 at 04:49
  • Does not seem to print out any weird characters. Also I looked at that post but could not connect any dots. @BenjaminW. Additionally my initial string is obtained with this in my bash script, ```RAW=$(airodump-ng wlan0 --essid MYSSID -a 2>&1 | grep -m 1 D0:23:54)``` I know this may be a lot to ask for but is there any way you could try to run this script and extract the MACS? I think all these string problems are happening because airodump-ng does not print line by line but updates the output realtime like the command ```top```. Also thank you for your help so far. Im a newbie and need it. – Lorde213423432e43434 Dec 03 '22 at 05:00
  • 1
    I'm pretty sure you have some weird/nonprinting characters in the variable. Try printing it with `printf '%s\n' "$RAW" | LC_ALL=C cat -vt` to convert normally-invisible things to visible representations. – Gordon Davisson Dec 03 '22 at 05:00
  • Yep... I get ```^[[0K^[[1B ``` and sometimes shorter versions of it like just ```^[[0K``` its different every time. This seems to be where the varying length issue comes from. @GordonDavisson Now to get rid of them ... – Lorde213423432e43434 Dec 03 '22 at 05:11

0 Answers0