mardi 30 juin 2020

Bash if statement: Comparison not working with assignment

In the following test_if2() works as expected but test_if not. The difference is that in test_if() I do a variable assignment. Where is the error?

test_if() {
  declare file_path
  if [[ -n ${file_path=$(echo "")} || -n ${file_path=$(echo "Hi")} ]]; then
    echo Non null: ${file_path}
  else
    echo null
  fi      
}

test_if2() {
  declare file_path
  declare greet="Hello"
  if [[ -n $(echo "") || -n $(echo "Hi") ]]; then
    echo Non null
  else
    echo null
  fi      
}

test_if #prints null
test_if2 #prints Non null

Aucun commentaire:

Enregistrer un commentaire