A short if for one command: A && B runs B only when A worked (exit code 0), and A || B only when A failed.
learner@reef:~$ grep -q cake menu.txt && echo "cake is on"
cake is on
learner@reef:~$ grep -q pie menu.txt || echo "no pie"
no pieFirst, ~/status.sh, two lines that note in ~/status.txt how the logs look:
-
Line 1: if
~/logs/register.loghas anERRORline, writeregister: checkto~/status.txt. Usegrep -q,&&and>. -
Line 2: if
~/logs/door.loghas noERRORline, adddoor: okto~/status.txt. Usegrep -q,||and>>. -
Run it and look:
textlearner@reef:~$ bash ~/status.sh learner@reef:~$ cat ~/status.txt register: check door: ok
Then ~/save.sh LOG. A chain A && B && C stops at the first command that fails:
-
After
#!/bin/bash, one line: make~/backupwithmkdir -p,&© the log into it,&&writesaved LOG. -
Make it runnable and try it:
textlearner@reef:~$ chmod u+x ~/save.sh learner@reef:~$ ./save.sh logs/register.log saved logs/register.log -
Try
./save.sh logs/oven.log:cpfails, so nosaved.
Objectives
- Line 1 of
~/status.sh:register: checkwhen the register log has anERROR. - Line 2: add
door: okwhen the door log has none. ./save.sh LOGcopies the log into~/backup, and writessaved LOGonly if the copy worked.