Skip to Content
Learning Linux Shell Scripting - Second Edition
book

Learning Linux Shell Scripting - Second Edition

by Ganesh Sanjiv Naik
May 2018
Beginner
332 pages
7h 28m
English
Packt Publishing
Content preview from Learning Linux Shell Scripting - Second Edition

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 $? ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Linux Shell Scripting

Learning Linux Shell Scripting

Ganesh Sanjiv Naik
Linux Shell Scripting Cookbook - Third Edition

Linux Shell Scripting Cookbook - Third Edition

Clif Flynt, Sarath Lakshman, Shantanu Tushar

Publisher Resources

ISBN: 9781788993197Supplemental Content