Reading values into a shell script for lookup

Discussion in 'OS X Mountain Lion (10.8)' started by haravikk, Apr 11, 2013.

  1. haravikk macrumors 65816

    May 1, 2005
    Okay, so I've written myself a simple little script that looks for structures within volumes, and then syncs files into them as necessary.

    The problem with this is that whenever it runs it causes all volumes to wake from sleep while it checks whether the target directory exists. What I'd like to do is instead only perform the check if the volume path hasn't been added a file, and to add volumes to this file if they turn out to be invalid targets.

    1. Load cache file into one or more variables
    2. For each volume:
      1. If volume is listed in cache file then skip it.
      2. Else, look to see if it has a valid target folder, if so then perform sync, otherwise add the volume to the cache file.

    What is the easiest way to do this? I've worked with lists in shell scripting before, but what I really need is something more like a map where I can take the current volume path and test to see if it is in the cache file (or the contents of the file loaded into a variable).

    Also, I've seen that bash4 supports a type of map/dictionary, but I'm not sure if I can use it in a shell script?
  2. haravikk thread starter macrumors 65816

    May 1, 2005
    Found a solution!

    For those interested here's what I did:

    # Map support functions
    function map_put { alias "${1}$2"="$3"; }
    function map_get {
        if type -p "${1}$2"
                alias "${1}$2" | awk -F "'" '{ print $2; }';
    # Load our .backup cache
    if [ -e "$CACHE" ] 
            for line in $(cat "$CACHE")
                map_put "CACHE" "$line" 1
    for f in /Volumes/*
        HASH=`md5 -q -s "$f"`
        SKIP=`map_get "CACHE" "$HASH"
        if [ -z "$SKIP" ]
                // Work out where we're syncing to
                if [ -e "$file" ]
                        // Sync some stuff
                        echo "$HASH" >> "$CACHE"
    Then it just needs some logic for deciding when to recreate the cache file, nice and easy :)
  3. robzr macrumors member

    May 4, 2006
    Looks like some nice shell script work. Have you looked into rsync? For syncing directory structures, it's hugely powerful.

  4. haravikk thread starter macrumors 65816

    May 1, 2005
    That's what I'm using, but I just needed to control which drives it would try to backup to.

    Basically it's for things like my 1Password keychain and other portable stuff that I want to keep safe; so whenever a volume is mounted the script looks for a .backup file in the root directory that tells it where to copy files to, and it then uses rsync to move across the differences. Means that whenever my iPod or USB stick are plugged in they automatically get the latest copy of the files.

    I've vastly overcomplicated the solution, as I could have just hard coded specific volume names, but I liked the idea of making it automatic; only problem was that without caching excluded volumes it touches every volume connected to my machine whenever a change is detected, which wakes them up if they're sleeping, which was pretty annoying :)

Share This Page