views:

1330

answers:

3

Need some help with this as my shell scripting skills are somewhat less than l337 :(

I need to gzip several files and then copy newer ones over the top from another location. I need to be able to call this script in the following manner from other scripts.

exec script.sh $oldfile $newfile

Can anyone point me in the right direction?

EDIT: To add more detail:

This script will be used for monthly updates of some documents uploaded to a folder, the old documents need to be archived into one compressed file and the new documents, which may have different names, copied over the top of the old. The script needs to be called on a document by document case from another script. The basic flow for this script should be -

  1. The script file should create a new gzip archive with a specified name (created from a prefix constant in the script and the current month and year e.g. prefix.september.2009.tar.gz) only if it does not already exist, otherwise add to the existing one.
  2. Copy the old file into the archive.
  3. Replace the old file with the new one.

Thanks in advance,
Richard

EDIT: Added mode detail on the archive filename

+2  A: 

A good refference for any bash scripting is Advanced Bash-Scripting Guide.

This guide explains every thing bash scripting.

The basic approach I would take is:

Move the files you want to zip to a directory your create.
   (commands mv and mkdir)

zip the directory. (command gzip, I assume)

Copy the new files to the desired location (command cp)

In my experience bash scripting is mainly knowing how to use these command well and if you can run it on the command line you can run it in your script.

Another command that might be useful is

pwd - this returns the current directory
lillq
And don't forget error conditions. cp, mv, gzip, etc. can all fail. Any script you write should be as robust as possible (good habit to get into). Check whether $oldfile and $newfile exist before trying to operate on them. Check whether each command you write succeeds. Perl has a nice way to do this: "some_command || die "some_command failed", but we can do that in bash too.
Tim
Good point - however I need to figure out how to get the script working before I can add in error handling :)
Frozenskys
+1  A: 

Here's the modified script based on your clarifications. I've used tar archives, compressed with gzip, to store the multiple files in a single archive (you can't store multiple files using gzip alone). This code is only superficially tested - it probably has one or two bugs, and you should add further code to check for command success etc. if you're using it in anger. But it should get you most of the way there.

#!/bin/bash

oldfile=$1
newfile=$2

month=`date +%B`
year=`date +%Y`

prefix="frozenskys"

archivefile=$prefix.$month.$year.tar

# Check for existence of a compressed archive matching the naming convention
if [ -e $archivefile.gz ]
then
    echo "Archive file $archivefile already exists..."
    echo "Adding file '$oldfile' to existing tar archive..."

    # Uncompress the archive, because you can't add a file to a
    # compressed archive
    gunzip $archivefile.gz

    # Add the file to the archive
    tar --append --file=$archivefile $oldfile

    # Recompress the archive
    gzip $archivefile

# No existing archive - create a new one and add the file
else
    echo "Creating new archive file '$archivefile'..."
    tar --create --file=$archivefile $oldfile
    gzip $archivefile
fi

# Update the files outside the archive
mv $newfile $oldfile

Save it as script.sh, then make it executable:

chmod +x script.sh

Then run like so:

./script.sh oldfile newfile

something like `frozenskys.September.2009.tar.gz, will be created, and newfile will replace oldfile. You can call this script with exec from another script if you want. Just put this line in your second script:

exec ./script.sh $1 $2
ire_and_curses
That's almost it :) - but I need to be able to keep adding more files to the archive. I've updated the Question so that it better explains the flow of what I'm trying to do.
Frozenskys
I've updated the Question to add even more detail.
Frozenskys
You are my Hero, that's perfect!
Frozenskys
A: 

Why don't you use version control? It's much easier; just check out, and compress.

(apologize if it's not an option)

erenon
Good idea, and that's what I use for my code updates - using the excellent beanstalkapp service, but that's not an option for these documents.
Frozenskys