February 2020
Intermediate to advanced
292 pages
8h 54m
English
One important aspect of the return status of the commands is that it can be used to (conditionally) run the next command. Two important operators are used for this purpose: && (AND) and || (OR).
In the two commands here, the second is run if—and only if—the first succeeds (the && operator). file.txt is removed if it is copied to the project folder:
cp file.txt ~/projects && rm -f file.txt
Let's have a look at a second example:
cp file.txt ~/projects || echo 'copy failed!'
In the preceding example, the second command is run only if the first fails (the || operator). copy failed! is printed if the copy fails.
In this recipe, we just showed that commands can be combined on a shell script to create a more complex command, and ...
Read now
Unlock full access