mercredi 11 décembre 2019

Removing files using if statement in basch script when a certation condition is met

I am currently using the follwoing code:

#!/bin/bash

rm /media/external/archive/auth-settings.tar.raw
rm /media/external/archive/bak-settings.tar.raw
rm /media/external/archive/cont-settings.tar.raw
rm /media/external/archive/data-data.tar.raw
rm /media/external/archive/data-settings.tar.raw
rm /media/external/archive/mon-data.tar.raw
rm /media/external/archive/mon-settings.tar.raw
rm /media/external/archive/mail-data.tar.raw
rm /media/external/archive/mail-settings.tar.raw
rm /media/external/archive/portal-settings.tar.raw
rm /media/external/archive/webserver-data.tar.raw
rm /media/external/archive/webserver-settings.tar.raw

for f in /media/external/archive/*.tar.raw;
        do mv "$f" "${f%.*.tar.raw}.tar.raw";
done

to remove old backups once the new ones have been archived. However if for some reason the archiving fails, this script will be run regardlessly and it will remove all the archives, leaving nothing behind.

How can I modify the script in a way so that the rm command are only done if the the corresponding archive exists with a count number in its file name skipping the deletion if the numbered archive does not exist. For example:

auth-setting.tar.raw

should be removed only if there is a

auth-settings.number.tar.raw

Aucun commentaire:

Enregistrer un commentaire