Skip to content

Instantly share code, notes, and snippets.

@gregdhill
Created December 3, 2018 10:11
Show Gist options
  • Save gregdhill/f981eb6464a87f07a5fcb1eb4ff90549 to your computer and use it in GitHub Desktop.
Save gregdhill/f981eb6464a87f07a5fcb1eb4ff90549 to your computer and use it in GitHub Desktop.
Bash script to automate a secret santa name draw.
#! /bin/bash
# Instructions:
# Create two files names 'emails' and 'names'.
# Add your friends!
# Run this script in a terminal: `./secret-santa.sh`
is_mixed () {
while read -r a && read -r b <&3;
do
firstname=$(echo $b | awk '{print $1}')
lastname=$(echo $b | awk '{print $2}')
if $(echo $a | grep -i $firstname &> /dev/null);
then
return 1
fi
if $(echo $a | grep -i $lastname &> /dev/null);
then
return 1
fi
done < emails 3<names
return 0
}
shuffle () {
echo "Shuffling..."
cat names | shuf -o names
cat emails | shuf -o emails
}
shuffle
while ! is_mixed; do shuffle; done
while read -r a && read -r b <&3;
do
#echo -e "$a -> $b";
subject="Subject: Secret Santa \n\n"
name="${b//?/*}\n${b}\n${b//?/*}\n\n"
message=$(cat <<EOF
To add to this year's festivities, we have decided to do a secret santa!\n
Please find the name you have drawn below...\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
EOF
)
printf "${subject}${message}${name}" | ssmtp $a
done < emails 3<names
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment