【发布时间】:2016-11-24 19:39:30
【问题描述】:
我正在使用 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 终端。
有人可以帮我修复这个错误并改进我的脚本吗?
【问题讨论】:
-
脚本是否在没有
awk的情况下运行?似乎使用了不正确的语法。您是否尝试将其注释掉?nc $ip $port的输出是什么。你能用nc -l $port > /dev/null修改没有awk部分的行吗?我认为在那里使用 awk 没有任何意义—— -
无需 awk 即可完美运行。我评论了这一行,并把 nc -l $port 和 nc $ip $port 改为。现在我想使用 awk 所以我会得到正确的输出......
-
awk需要来自nc命令输出的什么输出? -
现在我每行都收到一条消息,但我想要一个像朋友:消息这样的东西,每次来自另一台计算机的人在流中写入时
-
不属于这个问题的范围。请在
awk标签下询问您的输入和预期输出。