Created
July 21, 2022 05:09
-
-
Save smartwatermelon/d6f7a7e9a45c05f9b95ce7a3a23eeded to your computer and use it in GitHub Desktop.
@cassidoo's 20220717 interview question
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eu -o pipefail | |
# substitute asterisks for all but first and last characters of string | |
obscureString () { | |
str="$1" | |
ret="${str:0:1}$( printf "%0.s*" $( seq 1 $(( ${#str}-2 )) ) )${str: -1}" | |
echo $ret | |
} | |
# check for one param split with '@' | |
if [ $# -eq 0 ] || [ $# -gt 2 ] || [[ ! "$1" =~ ^.*@.*[^@]$ ]]; then | |
echo "$(basename "$0"): enter a valid email address" | |
exit 1 | |
fi | |
local_part=$( cut -d '@' -f 1 <<< "$1" ) | |
domain=$( cut -d '@' -f 2 <<< "$1" ) | |
# let's make sure the domain is legit | |
if dig $domain | grep "ANSWER: 0" 1>/dev/null; then | |
echo "$domain is not valid" | |
exit 1 | |
fi | |
# substitute asterisks for middle portion of local_part | |
local_part_subbed=$( obscureString "$local_part" ) | |
if [ $# -gt 1 ] && [[ "${2,,}" == "hidefull" ]]; then | |
# substitute asterisks for middle portion of domain | |
domain_subbed=$( obscureString "$domain" ) | |
echo "${local_part_subbed}@${domain_subbed}" | |
else | |
echo "${local_part_subbed}@${domain}" | |
fi |
Author
smartwatermelon
commented
Jul 21, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment