$의 의미? 쉘 스크립트의 (달러 물음표)
무엇을 하는가
echo $?
쉘 프로그래밍에서 의미 하는가?
마지막으로 실행 된 명령의 종료 상태입니다.
예를 들어이 명령은 true
항상 상태를 반환 0
하고 false
항상 상태를 반환합니다 1
.
true
echo $? # echoes 0
false
echo $? # echoes 1
매뉴얼에서 : (쉘을 호출 man bash
하여 가능)
$?
가장 최근에 실행 된 포 그라운드 파이프 라인의 종료 상태로 확장됩니다.
일반적으로 종료 상태는 0
성공 을 의미하고 0이 아닌 리턴 상태는 실패를 의미합니다. Wikipedia의 종료 상태 에 대해 자세히 알아보십시오 .
이 온라인 매뉴얼에서 볼 수 있듯이 이와 같은 다른 특수 변수가 있습니다 : https://www.gnu.org/s/bash/manual/bash.html#Special-Parameters
$?
마지막으로 실행 된 명령의 종료 값을 반환합니다. echo $?
콘솔에서 해당 값을 인쇄합니다. 0은 성공적인 실행을 의미하지만 0이 아닌 값은 다양한 실패 이유에 매핑됩니다.
그러므로 스크립팅 할 때; 다음 구문을 사용하는 경향이 있습니다
if [ $? -eq 0 ]; then
# do something
else
# do something else
fi
비교는 같 0
거나 같지 않아야 0
합니다.
** 업데이트 의견에 따라 : 이상적으로, 위의 코드 블록을 비교에 사용해서는 안됩니다. @tripleee 의견 및 설명을 참조하십시오.
명령의 마지막 상태 코드 (종료 값)가 있습니다.
에코 $? - 가장 최근에 실행 된 명령 의 EXIT STATUS를 제공합니다 . 이 EXIT STATUS는 ZERO가 성공을 암시 하고 실패를 나타내는 NON-ZERO 값 이 아닌 숫자 일 것입니다.
? -이것은 bash의 특수 매개 변수 / 변수 중 하나입니다.
$? -변수 "?"에 저장된 값을 제공합니다.
BASH의 유사한 특수 매개 변수는 1,2, *, #입니다 (일반적으로 echo 명령에서 $ 1, $ 2, $ *, $ # 등으로 표시됨).
에서 http://www.gnu.org/s/bash/manual/bash.html#Special-Parameters
?
Expands to the exit status of the most recently executed foreground pipeline.
3.4.2 특수 매개 변수 아래 의 Bash 매뉴얼을 참조하십시오 .
? -가장 최근에 실행 된 포 그라운드 파이프 라인의 종료 상태로 확장됩니다.
$?
(변수 이름은 "just" ?
) 로 표시되지 않으므로 찾기가 약간 어렵습니다 . 물론 종료 상태 섹션 도 참조하십시오 ;-)
행복한 코딩.
최소 POSIX C 종료 상태 예
를 이해하려면 $?
먼저 프로세스 종료 상태 개념을 이해해야합니다.
리눅스에서 :
프로세스가
exit
시스템 호출을 호출하면 커널은 프로세스가 종료 된 후에도 시스템 호출에 전달 된 값을 저장합니다.출구 시스템 호출에 의해 호출
exit()
ANSI C의 기능, 당신은 할 간접적 때return
부터main
.종종
fork
+를 사용 하여 종료 하위 프로세스 (Bash)를 호출 한 프로세스exec
는wait
시스템 호출로 하위의 종료 상태를 검색 할 수 있습니다.
Bash 코드를 고려하십시오.
$ false
$ echo $?
1
C "등가"는 다음과 같습니다.
false.c :
#include <stdlib.h> /* exit */
int main() {
exit(1);
}
bash.c :
#include <unistd.h> /* execl */
#include <stdlib.h> /* fork */
#include <sys/wait.h> /* wait, WEXITSTATUS */
#include <stdio.h> /* printf */
int main() {
if (fork() == 0) {
/* Call false. */
execl("./false", "./false", (char *)NULL);
}
int status;
/* Wait for a child to finish. */
wait(&status);
/* Status encodes multiple fields,
* we need WEXITSTATUS to get the exit status:
* http://stackoverflow.com/questions/3659616/returning-exit-code-from-child
**/
printf("$? = %d\n", WEXITSTATUS(status));
}
Bash에서 Enter 키를 누르면 위와 같이 fork + exec + wait가 발생하고 bash $?
는 분기 된 프로세스의 종료 상태 로 설정 됩니다.
Note: for built-in commands like echo
, a process need not be spawned, and Bash just sets $?
to 0 to simulate an external process.
Standards and documentation
POSIX 7 2.5.2 "Special Parameters" http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_05_02 :
? Expands to the decimal exit status of the most recent pipeline (see Pipelines).
man bash
"Special Parameters":
The shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed. [...]
? Expands to the exit status of the most recently executed foreground pipeline.
ANSI C and POSIX then recommend that:
0
means the program was successfulother values: the program failed somehow.
The exact value could indicate the type of failure.
ANSI C does not define the meaning of any vaues, and POSIX specifies values larger than 125: What is the meaning of "POSIX"?
Bash uses exit status for if
In Bash, we often use the exit status $?
implicitly to control if
statements as in:
if true; then
:
fi
where true
is a program that just returns 0.
The above is equivalent to:
true
result=$?
if [ $result = 0 ]; then
:
fi
And in:
if [ 1 = 1 ]; then
:
fi
[
is just an program with a weird name (and Bash built-in that behaves like it), and 1 = 1 ]
its arguments, see also: Difference between single and double square brackets in Bash
Outputs the result of the last executed unix command
0 implies true
1 implies false
참고URL : https://stackoverflow.com/questions/7248031/meaning-of-dollar-question-mark-in-shell-scripts
'IT' 카테고리의 다른 글
Git을위한 좋은 무료 시각적 병합 도구는 무엇입니까? (0) | 2020.06.16 |
---|---|
은행 거래 및 은행 잔고를 얻는 API가 있습니까? (0) | 2020.06.16 |
사용자 변수와 시스템 변수의 차이점은 무엇입니까? (0) | 2020.06.16 |
배치 파일을 Windows 서비스로 실행 (0) | 2020.06.16 |
믹스 인 대 특성 (0) | 2020.06.16 |