@@ -34,36 +34,32 @@ detect_os() {
3434check_port () {
3535 local port=$1
3636 local description=$2
37+ local optional=${3:- false}
3738 local os=$( detect_os)
3839 local port_in_use=false
39-
4040 case $os in
4141 " windows" )
42- # Use netstat on Windows (available by default)
43- if netstat -an | grep -q " :$port " ; then
42+ # Only consider LISTENING state
43+ if netstat -an | grep -E " :$port [[:space:]] " | grep -iq " LISTEN " ; then
4444 port_in_use=true
4545 fi
4646 ;;
4747 " macos" )
48- # Use netstat on macOS (available by default)
49- if netstat -an | grep -q " \\ .$port " ; then
48+ # Only consider LISTEN state
49+ if netstat -an | grep -E " \.$port [[:space:]] " | grep -iq " LISTEN " ; then
5050 port_in_use=true
5151 fi
5252 ;;
5353 " linux" )
54- # Try multiple methods on Linux
5554 if command -v ss & > /dev/null; then
56- # Use ss (modern replacement for netstat)
57- if ss -tuln | grep -q " :$port " ; then
55+ if ss -tuln | awk ' {print $4,$1}' | grep -E " [:.]$port [[:space:]]" | grep -iq " LISTEN" ; then
5856 port_in_use=true
5957 fi
6058 elif command -v netstat & > /dev/null; then
61- # Fall back to netstat
62- if netstat -tuln | grep -q " :$port " ; then
59+ if netstat -tuln | grep -E " :$port [[:space:]]" | grep -iq " LISTEN" ; then
6360 port_in_use=true
6461 fi
6562 elif command -v nc & > /dev/null; then
66- # Fall back to netcat
6763 if nc -z localhost $port 2> /dev/null; then
6864 port_in_use=true
6965 fi
@@ -73,7 +69,6 @@ check_port() {
7369 fi
7470 ;;
7571 * )
76- # Unknown OS - try netcat if available
7772 if command -v nc & > /dev/null; then
7873 if nc -z localhost $port 2> /dev/null; then
7974 port_in_use=true
@@ -84,6 +79,10 @@ check_port() {
8479 fi
8580 ;;
8681 esac
82+ return 0
83+ fi
84+ ;;
85+ esac
8786
8887 if [ " $port_in_use " = true ]; then
8988 echo -e " ${RED} ❌ PORT $port ($description ) is already in use${NC} "
0 commit comments