Results 1 to 4 of 4
I've written the Bash script below to process a large amount of old backups. They have PAR2 volumes because they were originally stored on CD or DVD and the media ...
- 07-23-2009 #1Just Joined!
- Join Date
- May 2006
- Posts
- 3
rm will not work in Bash script
I've written the Bash script below to process a large amount of old backups. They have PAR2 volumes because they were originally stored on CD or DVD and the media has started to degrade. I've copied them all to a hard drive and I am trying to write a script that will run par2cmdline against them, and delete the PARs if the check comes back clean, or a repair is successful.
I call the script using find * -name "*.par2" -exec /Volumes/Backups/parcheck "{}" \;. However, the second "rm" statement will NOT work. Notice that I echo the exact statement to console on the line above; if I break the script and copy/paste the echoed rm line to a prompt, it will delete the files as expected. Note that the first "rm" statement always works, even though the file is in the same directory with the files called by the second "rm" statement. And yes, the case is right, the first one has a lowercase extension, the remaining ones have uppercase extensions.
As is the Unix way, I've spent more time on this stupid bug than it would've taken me to go through all the PARs by hand, but now that the script works perfectly otherwise, I really want to know why that "rm" statement does not work.
This is Bash on MacOS X 10.5.
And sample output with case in point...Code:#!/bin/bash # Expects to be called with the full path to one PAR2 file. echo "About to check $1 ..."; sleep 2s; # Store present directory CDIR="`pwd`"; echo "PWD is $CDIR"; # Break up path and basename and strip extension THISDIR=`dirname $1` THISFILE=`basename $1` THISFILE=${THISFILE%.par2} # Change to directory containing file echo "Changing to `dirname "$1"`..."; cd "`dirname "$1"`"; # Run "par2 r" on the file par2 r "$THISFILE"; # Sleep to show status, and if applicable, warn deleting files. if [ $? -eq 0 ]; then echo "Success - Deleting PAR2 files in 5 seconds..." #sleep 5s; echo "rm -v $THISFILE.par2 :"; rm -v "$THISFILE.par2" echo "rm -v $THISFILE.vol*.PAR2 :"; rm -v "$THISFILE.vol*.PAR2" fi # Change back to $CDIR echo "Changing back to $CDIR ..." cd "$CDIR"
Code:macbook:1995-05-03 mark$ ls 1995-05-03.par2 1995-05-03.part5.rar 1995-05-03.vol015+15.PAR2 1995-05-03.part1.rar 1995-05-03.vol000+01.PAR2 1995-05-03.vol030+27.PAR2 1995-05-03.part2.rar 1995-05-03.vol001+02.PAR2 1995-05-03.vol057+52.PAR2 1995-05-03.part3.rar 1995-05-03.vol003+04.PAR2 1995-05-03.part4.rar 1995-05-03.vol007+08.PAR2 macbook:1995-05-03 mark$ ../../../parcheck 1995-05-03.par2 About to check 1995-05-03.par2 ... PWD is /Volumes/Backups/mtp/Jord Raw/1995-05-03 Changing to .... par2cmdline version 0.4, Copyright (C) 2003 Peter Brian Clements. par2cmdline comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. See COPYING for details. Loading "1995-05-03.par2". Loaded 12 new packets Loading "1995-05-03.vol000+01.PAR2". Loaded 1 new packets including 1 recovery blocks Loading "1995-05-03.vol001+02.PAR2". Loaded 2 new packets including 2 recovery blocks Loading "1995-05-03.vol003+04.PAR2". Loaded 4 new packets including 4 recovery blocks Loading "1995-05-03.vol007+08.PAR2". Loaded 8 new packets including 8 recovery blocks Loading "1995-05-03.vol015+15.PAR2". Loaded 15 new packets including 15 recovery blocks Loading "1995-05-03.vol030+27.PAR2". Loaded 27 new packets including 27 recovery blocks Loading "1995-05-03.vol057+52.PAR2". Loaded 52 new packets including 52 recovery blocks There are 5 recoverable files and 0 other files. The block size used was 384000 bytes. There are a total of 1081 data blocks. The total size of the data files is 414188335 bytes. Verifying source files: Target: "1995-05-03.part1.rar" - found. Target: "1995-05-03.part2.rar" - found. Target: "1995-05-03.part3.rar" - found. Target: "1995-05-03.part4.rar" - found. Target: "1995-05-03.part5.rar" - found. All files are correct, repair is not required. Success - Deleting PAR2 files in 5 seconds... rm -v 1995-05-03.par2 : 1995-05-03.par2 rm -v 1995-05-03.vol*.PAR2 : rm: 1995-05-03.vol*.PAR2: No such file or directory Changing back to /Volumes/Backups/mtp/Jord Raw/1995-05-03 ... macbook:1995-05-03 mark$ rm -v 1995-05-03.vol*.PAR2 1995-05-03.vol000+01.PAR2 1995-05-03.vol001+02.PAR2 1995-05-03.vol003+04.PAR2 1995-05-03.vol007+08.PAR2 1995-05-03.vol015+15.PAR2 1995-05-03.vol030+27.PAR2 1995-05-03.vol057+52.PAR2 macbook:1995-05-03 mark$ ls 1995-05-03.part1.rar 1995-05-03.part3.rar 1995-05-03.part5.rar 1995-05-03.part2.rar 1995-05-03.part4.rar macbook:1995-05-03 mark$
- 07-23-2009 #2Linux Newbie
- Join Date
- Mar 2009
- Posts
- 228
The reason the second rm doesn't work is because you have the name of the file to delete is double quotes and are using a glob (*).
When the filename is in quotes it won't expand the * so therefore is looking for a file named '1995-05-03.vol*.PAR2'.Code:rm -v "$THISFILE.vol*.PAR2"
The thing to do is remove the double quotes. Only problem that can occur is if you have files that have spaces in their names. Do you?
- 07-23-2009 #3Just Joined!
- Join Date
- May 2006
- Posts
- 3
Ah! That makes sense. Yes, the files may have spaces. Is there a way to have my cake and eat it too? Scripts that get filenames from elsewhere always tend to need quotes, so I can't think of a way to do this and still keep the filenames working if they have spaces in them.
- 07-24-2009 #4Linux Newbie
- Join Date
- Mar 2009
- Posts
- 228
One way that I can think of is to use the 'find' command. Try this:
Code:find . -maxdepth 1 -type f -name "$THISFILE.vol*.PAR2" -exec rm -v "{}" \;


Reply With Quote
