Netcat聊天bash脚本问题

时间:2016-11-24 19:39:31

标签: bash awk terminal netcat

我正在使用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 '$0="Anonymous: "$0' | 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 '$0="Anonymous: "$0' | 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终端。

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

1 个答案:

答案 0 :(得分:2)

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

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