Bedingte Codeblöcke in Linux Bash

Bedingte Codeblöcke in Linux Bash

Fast jeder kennt sehr nützliche &&- und ||-Operatoren, zum Beispiel:

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

Ich habe eine Frage: Wie kann ich einen Befehlsblock nach den Operatoren && oder || einfügen, ohne die Funktion zu verwenden?

Ich möchte beispielsweise Folgendes tun:

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"

Was ist die richtige Syntax dieses Skripts?

Antwort1

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"
}

oder besser

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

verwandte Informationen