Skip to main content
edited title
Link
terdon
  • 252.8k
  • 69
  • 481
  • 720

Bash - MultidimentionalMultidimensional Arrays and Extracting variables from output

typos, Show appreciation through votes. Thanks not necessary on SO/SX http://meta.stackoverflow.com/a/3021/186664
Source Link
Anthon
  • 81.4k
  • 42
  • 174
  • 228

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'w command on the console. In bash I am trying to take this output and get these values into a multidimentionalmultidimensional 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.

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

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.

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

Thanks.

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

Thanks.

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

Tweeted twitter.com/#!/StackUnix/status/343294063002406912
Source Link
russ
  • 91
  • 1
  • 1
  • 2
Loading