Skip to content

Instantly share code, notes, and snippets.

@at1as
Created June 12, 2016 03:29
Show Gist options
  • Save at1as/9774c4570e69ec484d9d94b7bdded702 to your computer and use it in GitHub Desktop.
Save at1as/9774c4570e69ec484d9d94b7bdded702 to your computer and use it in GitHub Desktop.
Generate a valid Social Security Number
#!/bin/bash
# Generate a valid Social Security Number
function generate() {
SSN=""
valid=0
for i in {1..9}; do
DIGIT=$[RANDOM%9+1]
SSN=$SSN$DIGIT
done
}
function validate() {
valid=1
# Validate the length is 9 characters
if [ ${#SSN} -ne 9 ]
then
valid=0
echo "Invalid: SSN length is not 9 characters"
fi
# No group can be all zeros
if [ ${SSN:0:3} -eq "000" ] || [ ${SSN:3:2} -eq "00" ] || [ ${SSN:5:4} -eq "0000" ]
then
valid=0
echo "Invalid: No SSN group may be comprised of all zeroes"
fi
# Cannot start with '9'
if [ ${SSN:0:1} -eq "9" ]
then
valid=0
echo "Invalid: SSN Cannot begin with a 9"
fi
# First group cannot be comprised of only 6
if [ ${SSN:0:3} -eq "666" ]
then
valid=0
echo "Invalid: Primary SSN group cannot be 666"
fi
if [ ${SSN} -lt "987654329" ] && [ ${SSN} -gt "987654320" ]
then
valid=0
echo "Invalid: Number range between 987654329 and 987654320 is not allowed"
fi
}
valid=0
while [ $valid -eq 0 ]
do
generate
echo -e "\nTesting generated number ${SSN} for validity..."
validate
done
echo -e "\nYour generated Social Security Number is:\n\n${SSN:0:3}-${SSN:3:2}-${SSN:5:4}\n"
@Colt2x3
Copy link

Colt2x3 commented Mar 9, 2025

Generate

@chickenlover727
Copy link

Generate

@L3bron131
Copy link

Generate

@L3bron131
Copy link

Generate

@isaiahhodel7
Copy link

Generate

@isaiahhodel7
Copy link

Generate

@ItszDomii
Copy link

Generate

@alexmotley15
Copy link

Generate

@thecondor2
Copy link

Generate

@pepitomaruhuana6767
Copy link

Generate

@dominic485
Copy link

Generate

@SirIdiocracy
Copy link

Generate

@donutkofi
Copy link

Generate

@nomotion-here
Copy link

Generate

@PargonaXxCraft
Copy link

Generate

@Elijahh12
Copy link

Generate

@kevinturner2008
Copy link

Generate

@Xizux
Copy link

Xizux commented Apr 24, 2025

Generate

@Dylannation19
Copy link

Generate

@sergplayz
Copy link

generate

@sp5der123123123
Copy link

Generate

@outxast4
Copy link

outxast4 commented May 9, 2025

Generate

@Xqlusive23
Copy link

Generate

@DoubleDeeFX
Copy link

Generate

@Frenchiex104x
Copy link

Generate

@toolmanU
Copy link

Generate

@kkosedd
Copy link

kkosedd commented May 25, 2025

Generate

@costoooo
Copy link

Generate

@youdedend
Copy link

Generate

@yvngsol0
Copy link

yvngsol0 commented Jun 4, 2025

Generate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment