Published Book on Amazon
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1 | |
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2 |
출판된 한글판 도서
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상) | |
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하) |
Original Book Contents
10.5.2 "test" Command – Logical Operation
This command checks the status of the file or performs a logical operation to specify exit status according to whether the result is "True" or "False".
[Command Format]
test < file operation expression | logical operation expression > |
[Command Overview]
■ The exit status is specified according to the result of condition expression on the specified operation expression.
■ User privilege -- Normal user.
[Detail Description]
■ The values for "True" and "False" are as follows:
■ True -- 0 -- zero
■ False -- 1 -- non zero
■ If the condition expression is not specified, "False" is returned.
■ operation expression
logical | ( EXPRESSION ) | EXPRESSION is true |
| ! EXPRESSION | EXPRESSION is false |
| EXPRESSION1 -a EXPRESSION2 | both EXPRESSION1 and EXPRESSION2 are true |
| EXPRESSION1 -o EXPRESSION2 | either EXPRESSION1 or EXPRESSION2 is true |
string | -n STRING | the length of STRING is nonzero |
| -z STRING | the length of STRING is zero |
| STRING1 = STRING2 | the strings are equal |
| STRING1 != STRING2 | the strings are not equal |
arithmatic | INTEGER1 -eq INTEGER2 | INTEGER1 is equal to INTEGER2 |
| INTEGER1 -ge INTEGER2 | INTEGER1 is greater than or equal to INTEGER2 |
| INTEGER1 -gt INTEGER2 | INTEGER1 is greater than INTEGER2 |
| INTEGER1 -le INTEGER2 | INTEGER1 is less than or equal to INTEGER2 |
| INTEGER1 -lt INTEGER2 | INTEGER1 is less than INTEGER2 |
| INTEGER1 -ne INTEGER2 | INTEGER1 is not equal to INTEGER2 |
file | FILE1 -ef FILE2 | FILE1 and FILE2 have the same device and inode numbers |
| FILE1 -nt FILE2 | FILE1 is newer (modification date) than FILE2 |
| FILE1 -ot FILE2 | FILE1 is older than FILE2 |
| -b FILE | FILE exists and is block special |
| -c FILE | FILE exists and is character special |
| -d FILE | FILE exists and is a directory |
| -e FILE | FILE exists |
| -f FILE | FILE exists and is a regular file |
| -g FILE | FILE exists and is set-group-ID |
| -G FILE | FILE exists and is owned by the effective group ID |
| -h FILE | FILE exists and is a symbolic link (same as -L) |
| -k FILE | FILE exists and has its sticky bit set |
| -L FILE | FILE exists and is a symbolic link (same as -h) |
| -O FILE | FILE exists and is owned by the effective user ID |
| -p FILE | FILE exists and is a named pipe |
| -r FILE | FILE exists and read permission is granted |
| -s FILE | FILE exists and has a size greater than zero |
| -S FILE | FILE exists and is a socket |
| t FD | - file descriptor FD is opened on a terminal |
| -u FILE | FILE exists and its set-user-ID bit is set |
| -w FILE | FILE exists and write permission is granted |
| -x FILE | FILE exists and execute (or search) permission is granted |
[Main Option]
--help | display this help and exit |
--version | output version information and exit |
[Used Example]
The following is to check if a file called "test_data" exists and check how exit status is handled accordingly.
pi@raspberrypi ~ $ ls -l |
drwxr-xr-x 2 pi pi 4096 Apr 29 14:47 backups drwxr-xr-x 2 pi pi 4096 Apr 28 15:34 Desktop drwx------ 2 pi pi 4096 Apr 11 18:58 Downloads drwxrwxr-x 2 pi pi 4096 Mar 24 11:37 python_games drwxr-xr-x 2 pi pi 4096 May 1 11:16 Script drwxr-xr-x 5 pi pi 4096 Apr 25 17:11 testdata |
pi@raspberrypi ~ $ echo $? |
0 |
pi@raspberrypi ~ $ test -e test-data |
pi@raspberrypi ~ $ echo $? |
1 |
The exit status before the check is 0, and after the check, the file is not found and the exit status is set to "False" value of "1".