Tue Dec 26 14:39:08 CST 2023

This commit is contained in:
Adolfo Delorenzo 2023-12-26 14:39:08 -06:00
parent 0cc3e99279
commit 2cee14cb6a
3 changed files with 22 additions and 0 deletions

0
database.db Normal file
View File

22
gen_ports.sh Executable file
View File

@ -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

Binary file not shown.