카테고리 없음

종료 상태 변수

노트에버 2018. 9. 10. 18:07

#출처https://jupiny.com/2017/07/10/shell-script-basic-1/

종료 상태 변수($?)

$? 은 바로 전에 실행한 명령어의 종료 상태를 나태내는 변수이다. 0 은 정상 종료를 의미하고, 그 외의 값(1 ~ 255)은 이상 종료를 의미한다.

#!/bin/bash

dirname="directory"  
filename="file.txt"

touch_new_file() {  
  cd $dirname
  touch $filename
  echo "Hello World" > $filename
}

error_message() {  
  echo "The directory already exists."
}

mkdir $dirname  
if [ $? = 0 ]  
then  
  touch_new_file
else  
  error_message
fi  

종료 상태 변수를 사용하지 않고 &&|| 을 사용하여 더 간단하게 표현할 수 도 있다.