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

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!

主站蜘蛛池模板: 鄱阳县| 金沙县| 类乌齐县| 苏尼特左旗| 西乌珠穆沁旗| 泰和县| 伊通| 利川市| 昌吉市| 乐至县| 桦南县| 延川县| 周宁县| 奎屯市| 普洱| 兴义市| 化隆| 抚松县| 景东| 德化县| 错那县| 固阳县| 新乡市| 勐海县| 鸡泽县| 太湖县| 田林县| 阜康市| 遵义市| 淅川县| 吴川市| 蕲春县| 鹿邑县| 肇东市| 马边| 南郑县| 顺昌县| 运城市| 武安市| 凤冈县| 梅河口市|