Bash: Check If Element Exists in Array

Actually, more accurately, I’m looking to check if an element does not exist in an array, but I’m sure that’s a simple extention once I know how to check if it does exist.

I’m using S3Sync, which is a Ruby script for syncing files/directories to Amazon S3, similar to what rsync would do for a remote server, but with less options.

Unfortunately, one of the options it lacks is an exclude list. My whitelist is way too long to script it for that, so I wanted to create my own exclude list.

I thought of looping through directories, and comparing with a blacklist to see if it can upload or not. For instance, it would loop through /, but ignore directories like /dev, /mnt, /proc, etc.

So do you have any idea how I could do something like this?

The code I have so far, which is very incomplete, follows.

cd /root/s3sync/

dirs='/*'

excluded=( /backups /dev /lost+found /media /mnt /proc /sys /tmp )

for dir in $dirs
do
  ruby s3sync.rb -rs --delete $dir azavia:backups/daily
done

Nevermind. I found a way of doing this right after posting here.

function elementExists()
{

if [ -z "$1" ]
then
return
fi

for i in ${excluded[@]}
do
if [ $i == $1 ]
then
return 1
fi
done

return 0
}

If you have any suggestions to improve it though, I’d like to hear them. Thanks.

Devbanana,

The new scripting looks clean. Can’t find of a better way than that.