Can someone please help me understand the following code segment?
set -- ${BACKUPDIR}/backup-???.tgz
lastname=${!#}
backupnr=${lastname##*backup-}
backupnr=${backupnr%%.*}
backupnr=${backupnr//\?/0}
backupnr=$[10#${backupnr}]
I found it in this backup script https://wiki.ubuntuusers.de/Skripte/inkrementelles_Backup/ where it is used to get the number of the filename of the last stored backup. The filenames of the backups are "backup-ccc.tgz" where ccc is a three digit number that is incremented every time a new backup is created.
I do understand lines three and four they cut off the rest of the filename so just the needed part with the number is left. What I don't get is how the first two lines are working. I read the manpage of set but now I'm confused more then I had been before. What does the command set -- filename
do? Also what exactly is the option ${!#} for, that is being stored in lastname. I think ${} is to dereference a given parameter, but which parameter does the option !# point to?
Secondly I don't get why I need the command in line 5. As I understand this is used to replace all question marks with an 0 that are stored in the variable backupnr, but why is this done? If there hasn't been an error in the commands before backupnr should be a three digit number, if there has been an error and the content of the variable is corrupt, you can't know what is the actual value of backupnr. So if I want to scan for corruption why don't I have to scan for everything thats not a number, but just for a question mark? Is this maybe somehow related to whats happening in the first two lines?