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

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!

主站蜘蛛池模板: 嘉兴市| 包头市| 婺源县| 沭阳县| 和田市| 儋州市| 承德市| 保靖县| 石泉县| 瑞昌市| 保康县| 宜城市| 西畴县| 高阳县| 阳春市| 云安县| 桑植县| 八宿县| 武山县| 敦煌市| 纳雍县| 平江县| 南乐县| 阿尔山市| 依安县| 台中县| 英吉沙县| 泸西县| 诸城市| 绥滨县| 车致| 理塘县| 广德县| 同德县| 石狮市| 敦煌市| 永春县| 天水市| 大石桥市| 康马县| 繁峙县|