I am trying to do something simple however I'm not sure how to achieve my goal here.
I am trying to extract the: USER, TTY and FROM values that are given by the w
command on the console. In bash I am trying to take this output and get these values into a multidimensional array (or just an array with a space delimiter).
#!/bin/bash
w|awk '{if(NR > 2) print $1,$2,$3}' | while read line
do
USERS+=("$line")
echo ${#USERS[@]}
done
echo ${#USERS[@]}
I have found my way to the point of reading in the values by line in a single array however I cannot seem to get the USERS array value out of the scope of the while loop. It prints the values 1,2,3,4 and then 0 after the loop. Every example I read they use the variable outside the scope perfectly fine but I cannot seem to.
while read col1 col2 col3 _; do ...; done < <(w)