Shell Script Compare multiline variable values with string

Asked

Viewed 388 times

0

Talk people! How do I compare a multiline variable (2 lines) with a string and have to validate one line at a time the contents of the variable is:

ambient_1 ambient_2

code test you:

#!/bin/bash

#valor da variável vem de 1 arquivo que contém as duas linhas
ambientes=$(<ambiente)                                                                                                                                                                                                                                                      
if [ ${ambientes} == "ambiente" ]; then                                                                                                                                                                                                                                            
echo "$ambientes"                                                                                                                                                                                                                                                    
else
       echo "O valor é: $ambientes"                                                                                                                                                                                                                                         
fi

Always error in the validation because it tries to compare with the 2 values at the same time, i need the script to compare first with 1 value and then the other understand that I will need to do some loop repetition but still do not know how to compare the value of each line separately. It is possible?

2 answers

0

I found the solution and it was actually quite simple. The for itself already makes this iteration.

#!/bin/bash
#valor da variável vem de 1 arquivo que contém as duas linhas
    
ambientes=$(<ambiente)

for i in $ambientes
  do      
      if [ "$i" == "ambiente_1" ]; then                                                                                                                            
            echo "O valor é igual: $i"                                                                                                                                        
      else
            echo "O valor é diferente: $i"                                                                                                                                         
      fi
  done

0

You will need a loop to read the variables and compare them

#!/bin/bash
#valor da variável vem de 1 arquivo que contém as duas linhas
    
#le o arquivo ambiente    
IFS=$' ' read -d '' -r -a LINES < ambiente
# laço processa cada variavel    
for i in "${LINES[@]}"; do
      if [ $i == "ambiente" ]; then                                                                                                                            
           echo "$i"                                                                                                                                        
      else
           echo "O valor é: $i"                                                                                                                              
      fi
done
  • Thanks for the answer! But even with this script he keeps comparing the 2 values at the same time.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.