Linux bash의 조건부 코드 블록

Linux bash의 조건부 코드 블록

거의 모든 사람들이 매우 유용한 &&와 || 예를 들면 다음과 같습니다.

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

질문이 있습니다. && 또는 || 뒤에 명령 블록을 넣는 방법이 있습니다. 함수를 사용하지 않고 연산자를 사용합니까?

예를 들어 다음을 수행하고 싶습니다.

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"

해당 스크립트의 올바른 구문은 무엇입니까?

답변1

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

또는 더 나은

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

관련 정보