Bash – assign output of command to array

arraybashlinuxshell-script

When I run this command:

cat output | grep -i state | sort | uniq | awk '{print $ 3}')

the output is:

00x1
00x5
0080

To assign them to an array, I did this:

STATUS_ARRAY=($(cat output | grep -i state | sort | uniq | awk '{print $ 3}')) 

but it didn't work. For every, system the output of that command is different and I want to check every single one of them.
For example — there are 21 types of status! — this code:

for STATUS in "${STATUS_ARRAY=[@]}"
do
  if [ "$STATUS" == '00x1' ] && [ "$STATUS" == '00x5' ];
  then
    echo " everything is normal"
  else [ "$STATUS" == '0080' ];
    echo " check your system "
  fi 
done  

but when array doesn't work it won't return anything. What is wrong with this?

The contents of output are:

State                                = 00x1
State                                = 00x5
State                                = 0080

Best Answer

This is one way how you can solve this:

Create the array

mapfile -t array < <(awk '{printf("%s\n", $NF)}' output)

Then, loop through indices and do whatever you want based on the index. e.g:

for status in "${array[@]}"
 do 
  if [[ $status == @(00x1|00x5) ]]
   then echo "All ok"
  else echo "All NOT ok"
  fi 
 done
Related Question