When I run this command:

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

the output is:


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=[@]}"
  if [ "$STATUS" == '00x1' ] && [ "$STATUS" == '00x5' ];
    echo " everything is normal"
  else [ "$STATUS" == '0080' ];
    echo " check your system "

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
  • 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 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

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[@]}"
  if [[ $status == @(00x1|00x5) ]]
   then echo "All ok"
  else echo "All NOT ok"
  • would u please explain the part 'create array' ?...instead of output can i write a different command for example ' df -h | grep data ' ? – BlackCrystal Oct 20 '18 at 11:51
  • Sure. Instead of awk you can write any command you want. See the link i posted in my previous comment – Valentin Bajrami Oct 20 '18 at 12:01
  • thank you soo much for your help my friend but it didnt work. – BlackCrystal Oct 20 '18 at 12:22
  • 1
    Can you run your script with bash -x your_script so we can see some debugging. Also does your file output have some weird line endings? What does sed -n l output say? – Valentin Bajrami Oct 20 '18 at 13:23
  • thank you it really helped. an error was saving in array. without awk and with sed -n i saw it and fixed it. :) – BlackCrystal Oct 21 '18 at 4:00

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";;
check system
everything normal
everything normal
  • 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 4: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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.