Wednesday, July 4, 2012

gzip script to Zip/Move and Erase Source File

I'm writing a program (in BBx which sits on top of Solaris 8) to repair damaged files. After a file has been repaired, I need to zip and move the broken file to an Archive directory. It seems that if one passes a target directory/name to gzip, it leaves the Source file there - which takes a lot of much-needed space. I'm guessing the answer would be to have a UNIX script that I could pass the parameters to. (The two parameters would be SOURCE_FILE_PATH_&_NAME$ and TARGET_DIR) 

Can anyone offer a script I could use for this purpose? (I know, very simple. But my expertise is elsewhere.) 
You want to gzip the source file and the target file and then "cmp" them. 
Once you have success in the cmp you could then delete the source file


No - the archiving of the Source file is already an extra precaution. Zipping source and target would take too much space & CPU for our purposes. I am looking for a UNIX script that I can pass the two parameters: Source path & name, and Target path, and the script I need would: 

> GZip the Source & move it to the Target directory. And then: 
> Remove the Source file. 
You will be operating one file at a time. If you have enough room at the 
source for one gzip you will be able to continue. 
1. make a list of the files to be archived to a temporary file: 
Process files one at a time. 
This can be done with a do while read loop 
2. send a gzip copy to the Target 
3. gzip source file 
4. cmp source target 
5. if cmp is successful, delete the source.gzip file. 

Once all files are transferred delete the temporary file.
Okay here is what I came up with. I'm doing the copy one time, and then if the compare fails, I try the copy one more time and then quit. I'm doing this simple approach for two reasons - 1) if the copy fails both times, then chances are there is a bigger issue at hand (like out of disk) and 2) because I'm a beginner at UNIX scripting. 

Any suggestions/ comments welcome. Thx! 
- Cole 

das01$ cat gzip_and_move 
gzip -f -q -9 $2$1 # gzip source path/filename 
notsame=0 
while [ "$notsame" != 1 ] 
do 
cp $2$1.gz $3$1.gz # copy zipped file from Source_Path to Target_Path 
cmp -s $2$1.gz $3$1.gz || cp $2$1.gz $3$1.gz # Compare Source_Path/Filename to 
Target_Path/Filename. If Result is bad, try the copy one more time. 
rm $2$1.gz # Remove the Source_Path/Filename. 
notsame=1 
done
Using the positional parameters of $1, $2, $3 usually not recommended within the script itself. I'd try 
FILE1=$1 
SOURCEPATH=$2 
TARGETPATH=$3 
and use those variables inside your loop. 


Okay here is what I came up with. I'm doing the copy one time, and then if the compare fails, I try the copy one more time and then quit. I'm doing this simple approach for two reasons - 1) if the copy fails both times, then chances are there is a bigger issue at hand (like out of disk) and 2) because I'm a beginner at UNIX scripting. 

Any suggestions/ comments welcome. Thx! 
- Cole 

das01$ cat gzip_and_move 
gzip -f -q -9 $2$1 # gzip source path/filename 
notsame=0 
while [ "$notsame" != 1 ] 
do 
cp $2$1.gz $3$1.gz # copy zipped file from Source_Path to Target_Path 
cmp -s $2$1.gz $3$1.gz || cp $2$1.gz $3$1.gz # Compare Source_Path/Filename to 
Target_Path/Filename. If Result is bad, try the copy one more time. 
rm $2$1.gz # Remove the Source_Path/Filename. 
notsame=1 
done 

0 comments:

Post a Comment

 
Design by BABU | Dedicated to grandfather | welcome to BABU-UNIX-FORUM