-
Notifications
You must be signed in to change notification settings - Fork 19
Linux Bash
xxooxxooxx edited this page Sep 17, 2019
·
23 revisions
Linux Bash 环境中 0 为 true 非 0 为 false(这和c语言是相反的)。
while [ 1 ] 或者 while [ 0 ] 他们的返回结果都为真,因为[ 1 ] 中的的 1 是一个字符,[ ] 等价于 test 命令,test 1 含义是测试字符 1 的长度是不是不为零。
man test 片段
-n STRING
the length of STRING is nonzero
STRING equivalent to -n STRING
test 1 的执行结果返回为 0,而 0 为 true。 正确的写法应该使用 true false 命令
while true
while false
当然也可以使用[ ],或者(())来返回假
while [ ]
while [ "" ]
while test
while ((1))
验证一下
root@debian:~# [ ]
root@debian:~# echo $?
1
root@debian:~# [ "" ]
root@debian:~# echo $?
1
root@debian:~# true
root@debian:~# echo $?
0
root@debian:~# false
root@debian:~# echo $?
1
root@debian:~# test
root@debian:~# echo $?
1
root@debian:~# test 0
root@debian:~# echo $?
0
root@debian:~# ((1))
root@debian:~# echo $?
0
结论就是Bash环境下,除非是算术表达式中的整数,否则默认均为字符串。
test 测试命令测试类型
- 整数测试
整数测试 | 描述 |
---|---|
-eq | = |
-ne | != |
-gt | > |
-lt | < |
-ge | >= |
-le | <= |
- 逻辑测试
逻辑测试 | 描述 |
---|---|
! | nor |
-a | and |
-o | or |
- 字符测试
字符测试 | 描述 |
---|---|
-n | nonzero |
-z | zero |
= | = |
!= | = |
- 文件测试
略