官术网_书友最值得收藏!

The exit status

In shell scripting, we need to check if the last command has successfully executed or not, for example, whether a file or directory is present or not. As per the result, our shell script will continue processing.

For this purpose, the bash shell has one status variable ?. The status of the last command execution is stored in ?. The range of numerical values stored in ? will be from 0 to 255. If successful in execution, then the value will be 0; otherwise, it will be non-zero, which is as follows:

    $ ls
    $ echo $?
    0

Here, zero as the return value indicates success.

In the next case, we see the following:

    $ ls /root
    ls: cannot open directory /root: Permission denied
    $ echo $?
    2

Here, a non-zero value indicates an error in the last command execution.

In the next case, we see:

    $ find / -name hello.c
    $ echo $?

The return value will indicate if the hello.c file is present or not!

主站蜘蛛池模板: 仁怀市| 天峻县| 台安县| 巢湖市| 扎兰屯市| 宁蒗| 项城市| 广平县| 南川市| 辽阳市| 汝城县| 盘锦市| 蓬安县| 方正县| 永胜县| 吉木萨尔县| 永顺县| 叙永县| 靖远县| 土默特左旗| 凯里市| 久治县| 灯塔市| 固阳县| 旌德县| 略阳县| 襄垣县| 巴彦淖尔市| 彰化县| 绥芬河市| 新泰市| 永仁县| 青龙| 华阴市| 宜城市| 咸宁市| 永新县| 蓬莱市| 东海县| 尚义县| 尖扎县|