检测某个端口是否被占用的小脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/env bash

n=$(ss -lntp|egrep :80 |wc -l)

if [ "${n}" -eq 0 ]; then

echo -e "\033[31m The machine does not start the web service, you can continue \033[0m"

main

else

echo -e "\033[31m Port 80 of this machine is occupied, please exit and try again \033[0m"

fi

脚本执行效果如下:

1
2
3
4
5
6
7
8
[root@lnmp /tmp 12:02:52]#sh -x port.sh
++ ss -lntp
++ egrep :80
++ wc -l
+ n=1
+ '[' 1 -eq 0 ']'
+ echo -e '\033[31m Port 80 of this machine is occupied, please exit and try again \033[0m'
Port 80 of this machine is occupied, please exit and try again