상태 코드로 스크립트를 종료하려고 시도했지만 "예기치 않은 파일 끝"이 발생함

상태 코드로 스크립트를 종료하려고 시도했지만 "예기치 않은 파일 끝"이 발생함

Amazon Linux에서 bash 쉘을 사용하고 있습니다. 내 스크립트가 구문 오류로 인해 죽어가는 이유를 알 수 없습니다. 내 스크립트는 이렇게 끝납니다

chmod 775 $TFILE2
output_file=$( create_test_results_file "$TFILE2" )
(cat $TFILE2; uuencode $output_file $output_file) | mailx -s "$subject" "$to_email"
rm $output_file
echo "sent second email"

#Cleanup
rm $TFILE1
rm $TFILE2
echo "removed files"

# If node exited unsuccessfully, verify we alert the process above.
if [ $rc1 != 0 ]; then exit $rc1 fi
if [ $rc2 != 0 ]; then exit $rc2 fi

실행하면 마지막 두 개의 echo 문이 인쇄되지만 그 이후에는 죽는 것 같습니다.

sent second email
removed files
/home/jboss/.jenkins/jobs/springboard/workspace/automated-tests/nodejs/run_tests.sh: line 86: syntax error: unexpected end of file

예상치 못한 파일 끝 오류로 인해 왜 죽는지 말해 줄 수 있는 사람이 있나요?

답변1

짧은 대답은 fi별도의 명령이 필요하므로 세미콜론이 필요하다는 것입니다.

if [ $rc1 != 0 ]; then exit $rc1; fi
if [ $rc2 != 0 ]; then exit $rc2; fi

변수를 인용해야 하며 정수를 비교하므로 적절한 연산자를 사용하십시오.

if [ "$rc1" -ne 0 ]; then exit "$rc1"; fi
if [ "$rc2" -ne 0 ]; then exit "$rc2"; fi

여기서 동작은 약간 다르지만 빈 값은 0과 동일한 것으로 간주됩니다(어디에서는 !=서로 다른 것으로 간주함).

관련 정보