Check Port Function.
This simple bash script goes through a process of checking certain variables, to see if a port is in use before continuing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/bash
port_check ()
{
process=0
echo "Enter port for the server between 20000-30000 but not default: "
read port_choice
if [[ $port_choice != '^[0-9]+$' ]] && [[ $port_choice -ge 20000 && $port_choice -le 30000 ]]; then
echo "Checking $port_choice."
sleep 0.2
else
echo "Is not a valid number, or choice is empty."
port_check
sleep 0.2
fi
if netstat -tuln | grep -q ":$port_choice "; then
echo "Port $port_choice is in use."
port_check
sleep 0.2
else
echo "Port $port_choice is free."
echo "Port check completed successfully."
process=$(($process + 1))
sleep 0.2
fi
}
if [[ $process = 0 ]]; then
port_check
sleep 0.2
else
echo "Passed Check."
sleep 0.2
fi