diff --git a/database.db b/database.db new file mode 100644 index 0000000..e69de29 diff --git a/gen_ports.sh b/gen_ports.sh new file mode 100755 index 0000000..1a61c82 --- /dev/null +++ b/gen_ports.sh @@ -0,0 +1,22 @@ +#!/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 diff --git a/numbers_db.sqlite b/numbers_db.sqlite index 151b57e..6b9f7f9 100644 Binary files a/numbers_db.sqlite and b/numbers_db.sqlite differ