상태 코드로 스크립트를 종료하려고 하는데 "예기치 않은 파일 끝" 메시지가 나타납니다.

상태 코드로 스크립트를 종료하려고 하는데 "예기치 않은 파일 끝" 메시지가 나타납니다.

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

여기서 동작은 약간 다르지만 null 값은 0과 동일한 것으로 처리됩니다( !=다른 것으로 간주됨).

관련 정보