From 180629c8ddb30b3dd8fd6c66a901444fcedc227e Mon Sep 17 00:00:00 2001 From: Sofia <253282481+sofia-riese@users.noreply.github.com> Date: Sun, 18 Jan 2026 09:14:43 +0800 Subject: [PATCH] perf: optimize port validation with efficient grep usage - Use 'grep -q' to avoid unnecessary output and sub-shell usage --- scripts/menus/check_port.sh | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/scripts/menus/check_port.sh b/scripts/menus/check_port.sh index 1a3c288f..728ea1ba 100644 --- a/scripts/menus/check_port.sh +++ b/scripts/menus/check_port.sh @@ -1,17 +1,17 @@ #!/bin/sh # Copyright (C) Juewuy -check_port(){ - if [ "$1" -gt 65535 -o "$1" -le 1 ]; then - echo -e "\033[31m输入错误!请输入正确的数值(1-65535)!\033[0m" - return 1 - elif [ -n "$(echo "|$mix_port|$redir_port|$dns_port|$db_port|" | grep "|$1|")" ]; then - echo -e "\033[31m输入错误!请不要输入重复的端口!\033[0m" - return 1 - elif [ -n "$(netstat -ntul | grep -E ":$1[[:space:]]")" ]; then - echo -e "\033[31m当前端口已被其他进程占用,请重新输入!\033[0m" - return 1 - else - return 0 - fi +check_port() { + if [ "$1" -gt 65535 ] || [ "$1" -le 1 ]; then + echo -e "\033[31m输入错误!请输入正确的数值(1-65535)!\033[0m" + return 1 + elif echo "|$mix_port|$redir_port|$dns_port|$db_port|" | grep -q "|$1|"; then + echo -e "\033[31m输入错误!请不要输入重复的端口!\033[0m" + return 1 + elif netstat -ntul | grep -q ":$1[[:space:]]"; then + echo -e "\033[31m当前端口已被其他进程占用,请重新输入!\033[0m" + return 1 + else + return 0 + fi }