page contents

shell脚本中“$?”标记的用途是什么?

轩辕小不懂 发布于 2022-07-12 14:06
阅读 493
收藏 0
分类:中间件
3908
Nen
Nen
- 程序员

在写一个shell脚本时,如果你想要检查前一命令是否执行成功,在if条件中使用“$?”可以来检查前一命令的结束状态。简单的例子如下:

root@localhost:~# ls /usr/bin/shar

/usr/bin/shar

root@localhost:~# echo $?

0

如果结束状态是0,说明前一个命令执行成功。

root@localhost:~# ls /usr/bin/share

ls: cannot access /usr/bin/share: No such file or directory

root@localhost:~# echo $?

2

如果结束状态不是0,说明命令执行失败。

请先 登录 后评论