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

Command1 || command2

The second command is only started if the first command fails. The shell checks
the exit status of the first command and starts the second command only if that
exit status is not equal to 0:

    $ ls /root  ||  echo "Command execution failed"

Example:

    $ ls || echo "command ls failed"

In the preceding example, if ls runs successfully, then echo will not be called. If the ls command fails, such as $ ls /root, and if the user is not the root, then ls will fail and the echo command will print command ls failed.

When && or || are used, the exit status of the first command is checked first, and then the decision to perform the next will be taken.

For example:

    $ ls
    $ echo $?
      0
    $ ls /root
        ls: /root: Permission denied
    $ echo $?
      1
    $ tar cvzf /dev/st0 /home /etc | | mail -s "Something went wrong with the backup" root

If we give the command as follows:

    $ cd /home/student/work/temp/; rm -rf *

Initially, the shell will change to the /home/student/work/temp folder, and then it will delete all files and folders.

If we enter the command as follows:

    cd /backup/ol/home/student/work/temp/ && rm * -rf

This will first change to the required folder, and then the rm command will be called for deletion. The problem with ; is that even if the shell fails to change to the required folder, the rm command will execute and it will delete all the files and folders from your original folder. This will be really dangerous.

For example:

    $ [[ "a" = "b" ]]; echo ok
    ok

In this case, the [[ ]] expression will evaluate to false. Since the semicolon will not check the status of the earlier command, ok will be printed even if the first [[ ]] expression fails.

    $ [[ "a" = "b" ]] && echo ok

In this case, the [[ ]] expression will evaluate to false. As the first expression is false, the "&&" operator will not proceed to execute the next command.

In this case, ok will be printed only if [[ ]] is true.

主站蜘蛛池模板: 万安县| 锡林浩特市| 福泉市| 柯坪县| 通河县| 三原县| 都江堰市| 甘肃省| 岳普湖县| 温宿县| 田东县| 巴南区| 罗江县| 太康县| 靖远县| 盐津县| 潮安县| 双辽市| 托克逊县| 金寨县| 德钦县| 莱阳市| 六枝特区| 永春县| 长阳| 谷城县| 同仁县| 东台市| 元阳县| 明溪县| 乌兰县| 乌兰浩特市| 芦溪县| 荆州市| 青神县| 文山县| 县级市| 武清区| 稷山县| 大化| 墨竹工卡县|