23 lines
1002 B
Bash
Executable File
23 lines
1002 B
Bash
Executable File
#!/opt/homebrew/bin/bash
|
|
|
|
# Create a random number between 32768 and 65535
|
|
RAND_NUM=$(($RANDOM % (65536 - 32768) + 32768))
|
|
|
|
# SQLite database setup
|
|
DB_PATH="database.db"
|
|
CREATE_TABLE="CREATE TABLE IF NOT EXISTS ids (id INTEGER PRIMARY KEY)"
|
|
INSERT_INTO="INSERT OR IGNORE INTO ids(id) VALUES ($1)"
|
|
SELECT_EXISTS="SELECT EXISTS (SELECT 1 FROM ids WHERE id = $1)"
|
|
|
|
# Check if the generated number already exists in the database
|
|
if [ $(sqlite3 "$DB_PATH" -header -separator "," -csv "SELECT $SELECT_EXISTS FROM ids WHERE id = '$RAND_NUM'" | awk '{print $1}') == 0 ]; then
|
|
# Number is unique, print it and insert into the database
|
|
echo "Unique random number: $RAND_NUM"
|
|
sqlite3 "$DB_PATH" -header -separator "," -csv "INSERT OR IGNORE INTO ids(id) VALUES ($RAND_NUM)"
|
|
else
|
|
# Number is not unique, generate a new random number and repeat the process
|
|
echo "Number already exists. Generating a new one..."
|
|
RAND_NUM=$(($RANDOM % (65536 - 32768) + 32768))
|
|
echo "Unique random number: $RAND_NUM"
|
|
fi
|