Netcat 聊天 bash 脚本问题

Netcat Chat bash script problems

我正在使用 netcat 为聊天创建一个 sh 脚本。 这是代码:

#!/bin/bash

clear

echo
echo "-----------------------"
echo "| handShaker Chat 2.0 |"
echo "-----------------------"
echo

read -p 'Server or Client setUp? (s or c) > ' type

if [ $type == 's' ] || [ $type == 'S' ] || [ $type == 'server' ]
then
    read -p 'Port (4321 Default) > ' port
    if [ $port -gt 2000 ] && [ $port -lt 6500 ]
    then
        echo
        echo "Started listening on port $port."
        echo "Stream (Press ctrl + shift to end session) >"
        echo
        awk -W interactive '[=11=]="Anonymous: "[=11=]' | nc -l $port > /dev/null
    else
        echo "handShaker Error > The port $port is not a in the valid range (2000 ... 6500)."
    fi
elif [ $type == 'c' ] || [ $type == 'C' ] || [ $type == 'client' ]
then
    read -p 'Port (4321 Default) > ' port
    if [ $port -gt 2000 ] && [ $port -lt 6500 ]
    then
        read -p 'Destination IP > ' ip
        if [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]
        then
            echo
            echo "Started streaming $ip on port $port."
            echo "Stream (Press ctrl + shift to end session) >"
            echo
            awk -W interactive '[=11=]="Anonymous: "[=11=]' | nc $ip $port > /dev/null
        else
            echo "handShaker Error > Invalid IP Address."
        fi
    else
        echo "handShaker Error > The port $port is not a in the valid range (2000 ... 6500)."
    fi
else
    echo "handShaker Error > $type is not a valid keyword."
fi

但我有以下问题:awk -W参数好像不存在,程序居然在运行客户端后就停止了。 我正在使用 macOS 终端。

有人可以帮我修复这个错误并改进我的脚本吗?

您的脚本错误且不必要地使用了带有 -W interactive 标志的 awk,这些标志未在 awk 的任何版本中定义。删除它应该可以解决您的问题。

您的脚本还定义了一堆 bash 变量,并且没有使用双引号。记得double quote variables prevent globbing and word splitting.