3

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
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • Can you show the content (a piece of) output file? Also, maybe you want "${STATUS_ARRAY[@]}". Don't capitalize normal variables. By convention only environment variables are in capitals – Valentin Bajrami Oct 20 '18 at 10:11
  • when i echo array it return one word.database.its weird. -_- – BlackCrystal Oct 20 '18 at 10:51
  • Can you please show the content of the file called output? Also did you read my comment regarding "${STATUS_ARRAY[@]}" ? See there is no = sign in there. If you want to know more about arrays, readarray or mapfile see https://mywiki.wooledge.org/BashFAQ/005 – Valentin Bajrami Oct 20 '18 at 11:04
  • the problem is not for and if. its just an example of why i need that array. the array dont return anything. i updated my post and added output content. – BlackCrystal Oct 20 '18 at 11:09

2 Answers2

3

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
2

How about using a "here string", "Parameter expansion", and a case ... esac contruct?

readarray -t TMP <<< $(grep -i "state" output | sort -u)
for STATUS in "${TMP[@]##* }"
  do    case $STATUS in
                00x[15]) echo "everything normal";;
                0080)    echo "check system";;
                *)       echo "error: status unknown";;
        esac
  done
check system
everything normal
everything normal
RudiC
  • 8,969
  • thank you for ur answer but it didnt work T-T – BlackCrystal Oct 20 '18 at 12:43
  • How and why "didn't it work"? It worked for me when I tested it. Without details like error msgs or description of "mis"behaviour nobody can help you. – RudiC Oct 20 '18 at 13:23
  • what should i say when it doesnt return anything? if ound the problem btw. thank you for this answer. ican use it in other scripts. – BlackCrystal Oct 21 '18 at 04:02
  • "what should i say when it doesnt return anything?" You can say, I tried this but it gave an empty return. "Didn't work" doesn't really point us to what the result was versus what was expected. Maybe it returned as if successful but actually wasn't, maybe it created an array, but incorrectly formatted, etc. – Adam Grant Jun 21 '19 at 19:14