-2

I run this as part of a 'unlock' bash script, but it fails on the first command -

# Variables 
CHUNK="/media/backup/obnam-home"
BIGNUM="17580577608458113855"
LOGTO="/home/boudiccas/logs/unlock.txt"
####################################################

sudo rm $CHUNK/chunklist/lock;  sudo rm $CHUNK/$BIGNUM/lock; sudo rm      $CHUNK/chunksums/lock; sudo rm $CHUNK/chunks/lock>>'$(date -R)' $LOGTO

How can I get it to continue onto the second, and further commands, even if 'x' command fails?

boudiccas
  • 247
  • 3
  • 12
  • See http://stackoverflow.com/questions/7251619/how-can-i-try-to-do-something-and-then-detect-if-it-fails-in-bash – Floris Oct 04 '13 at 13:13
  • 2
    Commands delimited by `;` are processed in sequence, the second command IS executed even if the first fail. I think you didn't describe your problem correctly. – KurzedMetal Oct 04 '13 at 13:15

1 Answers1

0

I think this is what you want:

# Variables 
CHUNK="/media/backup/obnam-home"
BIGNUM="17580577608458113855"
LOGTO="/home/boudiccas/logs/unlock-$(date -R).txt"
####################################################

{
    sudo rm $CHUNK/chunklist/lock
    sudo rm $CHUNK/$BIGNUM/lock
    sudo rm $CHUNK/chunksums/lock
    sudo rm $CHUNK/chunks/lock
} 2>> $LOGTO

Each of the four rm commands will run, regardless of which ones succeed and which fail. Any error messages from all 4 will be redirected (2>>, not >>) to the named file. I'm assuming you want the current timestamp in the file name, so I moved the call to date to the definition of LOGTO.

chepner
  • 389,128
  • 51
  • 403
  • 529
  • Thanks for this, I've created a 'lock' file at /$CHUNK/chunklist/ but it hasn't been deleted, and the script fails saying '/home/boudiccas/bin/unlock: line 36: $LOGTO: ambiguous redirect'. Sorry :( – boudiccas Oct 04 '13 at 14:00
  • After further tweaking and looking at past working scripts, this works `LOGTO="/home/boudiccas/logs/unlock-$(/bin/date +%Y%m%d).txt"` Thanks for putting me on the right track chepner. :) – boudiccas Oct 04 '13 at 14:19