문제

시도하고 싶 트랩 신호 1 실

#!/bin/bash
# capture an interrupt # 0
trap 'echo "Exit 0 signal detected..."' 0
trap 'echo "Exit 1 signal detected..."' SIGHUP

# display something
echo "This is a checkpoint 1"
exit 1

echo "This is checkpoint 2"
# exit shell script with 0 signal
exit 0

Output--
kithokit@15:02:55 trunk (master) $ ./test.sh 
This is a checkpoint 1
Exit 0 signal detected...
kithokit@15:03:44 trunk (master) $ 

는 경우에도 그것은 1 번 출구,그것은 항상 트랩 트랩 0,어떤 방법을 알고 이를 해결하기 위해?

감사

도움이 되었습니까?

해결책

exit 1 를 보내지 않 SIGHUP.그것이 종료와 함께 반환 코드(일명 종료 상태)1.를 보내 SIGHUP 사용 kill:

#!/bin/bash
# capture an interrupt # 0
trap 'echo "Signal 0 detected..."' 0
trap 'echo "SIGHUP detected..."' SIGHUP

# display something
echo "This is a checkpoint 1"
kill -1 $$

echo "This is checkpoint 2"
# exit shell script with 0 signal
exit 0

$$ ID 현재의 과정입니다.그래서, kill -1 $$ 보내는 신호를 1(SIGHUP)해 현재의 과정입니다.출력 위의 스크립트:

This is a checkpoint 1
SIGHUP detected...
This is checkpoint 2
Signal 0 detected...

는 방법을 확인하는 코드를 반환에 종료

는 경우 목표는 체크의 반환 코드(으로도 알려진 종료 상태)보를 잡는 특수 신호음이 모든 우리가 할 필요가 있는 상태를 확인 변수 $? 에 종료:

#!/bin/bash
# capture an interrupt # 0
trap 'echo "EXIT detected with exit status $?"' EXIT

echo "This is checkpoint 1"
# exit shell script with 0 signal
exit "$1"
echo "This is checkpoint 2"

실행하면 명령행에서,이 생성합니다:

$ status_catcher 5
This is checkpoint 1
EXIT detected with exit status 5
$ status_catcher 208
This is checkpoint 1
EXIT detected with exit status 208

참고는 trap 문화 bash 기능을 포함할 수 있습 임의로 복잡한 문장을 다른 반환 프로세스 코드는 다릅니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top