Blocos condicionais de código no bash do linux

3

Quase todo mundo sabe que é muito útil & & e || operadores, por exemplo:

rm myf && echo "File is removed successfully" || echo "File is not removed"

Eu tenho uma pergunta: como colocar um bloco de comandos depois de & & ou || operadores sem usar a função?

Por exemplo, eu quero fazer:

rm myf && \
  echo "File is removed successfully" \
  echo "another command executed when rm was successful" || \
  echo "File is not removed" \
  echo "another command executed when rm was NOT successful"

Qual é a sintaxe correta desse script?

    
por Arek 27.12.2010 / 16:15

1 resposta

8
rm myf && {
  echo "File is removed successfully" 
  echo "another command executed when rm was successful"
} || {
  echo "File is not removed" 
  echo "another command executed when rm was NOT successful"
}

ou melhor

if  rm myf ; then
      echo "File is removed successfully" 
      echo "another command executed when rm was successful"
else
      echo "File is not removed" 
      echo "another command executed when rm was NOT successful"
fi
    
por 27.12.2010 / 16:26