정수 grep이 문자열과 일치하지 않으면 어떤 종료 코드가 반환됩니까?
일치하면 0을 반환하고 일치하지 않으면 1을 반환한다는 것을 알고 있습니다.
그렇죠?
답변1
man grep
돕다:
EXIT STATUS
The grep utility exits with one of the following values:
0 One or more lines were selected.
1 No lines were selected.
>1 An error occurred.
또한,GNU 그렙:
However, if the -q or --quiet or --silent option is used and a line is
selected, the exit status is 0 even if an error occurred. Other grep
implementations may exit with status greater than 2 on error.
그리고 구현에 따라 다음과 같습니다.
Normally, exit status is 0 if matches were found, and 1 if
no matches were found. (The -v option inverts the sense
of the exit status.)
자신을 테스트하려면 다음을 스크립트에 넣고 실행하세요.
#!/bin/bash
echo -n "Match: "
echo grep | grep grep >/dev/null; echo $?
echo -n "Inverted match (-v): "
echo grep | grep -v grep; echo $?
echo -n "Nonmatch: "
echo grep | grep grepx; echo $?
echo -n "Inverted nonmatch (-v): "
echo grep | grep -v grepx >/dev/null; echo $?
echo -n "Quiet match (-q): "
echo grep | grep -q grep; echo $?
echo -n "Quiet nonmatch (-q): "
echo grep | grep -q grepx; echo $?
echo -n "Inverted quiet match (-qv): "
echo grep | grep -qv grep; echo $?
echo -n "Inverted quiet nonmatch (-qv): "
echo grep | grep -qv grepx; echo $?