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/


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

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

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

function elementExists()

if [ -z "$1" ]

for i in ${excluded[@]}
if [ $i == $1 ]
return 1

return 0

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


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