Bash – How to Check Command Success

bashcpddshell-script

I'm currently writing a small script to backup bulks of floppy disks and format them afterward for later use.

I use dd to copy the disk's image and cp to copy all the files on the disk.

Here are the commands I use to do so:

# Copying disk image
dd if="/dev/fd0" of="/path/to/backup/folder" &>/dev/null && sync

# Copying disk files
cp -R "/mnt/floppy/." "/path/to/backup/folder/" &>/dev/null

After this process, the script needs to format the floppy disk. My problem is that I want my script to format the floppy disk only if both backup commands (dd and cp) were successful.

For example, if dd couldn't copy all 1.44MB of the floppy disk because of bad blocks, then do not format the floppy disk.

How can I test if both commands were successful (They must be tested seperatly, as I do not always backup both disk's image and files)?

Best Answer

I'd do:

ok=true
if dd ...; then
  sync
else
  ok=false
fi

cp ... || ok=false

if "$ok"; then
  mformat...
fi
Related Question