Skip to content

Instantly share code, notes, and snippets.

@realyukii
Last active August 9, 2024 04:24
Show Gist options
  • Save realyukii/01f147331f7a438907606d4ac908ced7 to your computer and use it in GitHub Desktop.
Save realyukii/01f147331f7a438907606d4ac908ced7 to your computer and use it in GitHub Desktop.
check if list of users exists on SQL table
#!/bin/bash
# note
# you can use grep to count occurrences of each character in a string
# check total of non-exists and exists user with: grep -c '0' output_file.txt
email_file="emails.txt" # File containing emails, assuming delimeted by CRLF
database="evaluasi"
# Loop through each email in the file
tr -d '\r' < "$email_file" | while IFS= read -r email; do
# Query to check if the email exists in the users table
query="SELECT COUNT(*) FROM users WHERE email = '$email';"
# Execute the query using mysql client
result=$(mysql --defaults-extra-file=config.cnf "$database" -se "$query")
echo "$email -> $result"
done
@realyukii
Copy link
Author

example content of config.cnf:

[client]
user = "username"
password = "pw"
host = "localhost"

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