#!/bin/bash # print the first available bittorrent port for aria2 ports=$( netstat -4tlpn 2>&1 | tail -n+5 | awk '{print $4}' | cut -d: -f2 | sort -n) topPort=22000 for port in {22000..22022}; do if echo "$ports" | grep "$port" -; then continue fi topPort=$port break done echo $topPort