ind*_*man 23 shell bash command test
当我在终端输入这个
test 4 -lt 6
Run Code Online (Sandbox Code Playgroud)
我没有得到任何输出。为什么不?我需要那个 0 或 1
man*_*ork 18
你得到 0 或 1。在退出代码中。
bash-4.2$ test 4 -lt 6
bash-4.2$ echo $?
0
bash-4.2$ test 4 -gt 6
bash-4.2$ echo $?
1
Run Code Online (Sandbox Code Playgroud)
更新:要存储退出代码供以后使用,只需将其分配给一个变量:
bash-4.2$ test 4 -lt 6
bash-4.2$ first=$?
bash-4.2$ test 4 -gt 6
bash-4.2$ second=$?
bash-4.2$ echo "first test gave $first and the second $second"
first test gave 0 and the second 1
Run Code Online (Sandbox Code Playgroud)
另一种方式是
test 4 -lt 6 && echo 1 || echo 0
Run Code Online (Sandbox Code Playgroud)
但在这种情况下要小心。如果test返回成功和echo 1失败echo 0将被执行。
| 归档时间: |
|
| 查看次数: |
25745 次 |
| 最近记录: |