You are writing a nightly backup script that first creates a compressed archive of /data and, if that command fails, should immediately run a fallback script located at /usr/local/bin/backup-fallback.sh. Which single-line shell command correctly uses the || operator to satisfy this requirement?
tar czf /backup/primary.tar.gz /data || /usr/local/bin/backup-fallback.sh
tar czf /backup/primary.tar.gz /data | /usr/local/bin/backup-fallback.sh
tar czf /backup/primary.tar.gz /data && /usr/local/bin/backup-fallback.sh
/usr/local/bin/backup-fallback.sh || tar czf /backup/primary.tar.gz /data
In Bash, the || operator is a logical OR that executes the command on its right only when the command on its left exits with a non-zero status (failure). Therefore, the line "tar czf /backup/primary.tar.gz /data || /usr/local/bin/backup-fallback.sh" performs the fallback only if tar fails. Using && would run the fallback on success, reversing the intent; reversing the order of commands would try the fallback first; and a pipe (|) merely sends stdout to the next command regardless of exit status.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
What does the '||' operator do in a shell script?
Open an interactive chat with Bash
What is the difference between '||' and '&&' operators in shell scripts?
Open an interactive chat with Bash
Why is '|', the pipe operator, incorrect in this context?