Linux netcat 的新线路问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11273999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:10:23  来源:igfitidea点击:

new line issue with netcat

linuxunix

提问by JohnG

I am using below command to send some strings to udp listening server.

我正在使用以下命令将一些字符串发送到 udp 侦听服务器。

echo "A 192.168.192.168" | nc -u 192.168.2.1 1234

echo "A 192.168.192.168" | nc -u 192.168.2.1 1234

but the server is getting trailing '\n' in echoed string.

但服务器在回显字符串中尾随 '\n'。

I have tried below command too, but failed

我也试过下面的命令,但失败了

echo "A 192.168.192.168" | nc -uC 192.168.2.1 1234

echo "A 192.168.192.168" | nc -uC 192.168.2.1 1234

How can I remove that trailing new line character ?? Do I have any special option in nc ??

如何删除尾随的换行符?我在 nc 有什么特别的选择吗??

回答by Levon

Try using

尝试使用

echo -n

so

所以

echo -n "A 192.168.192.168" | nc -u 192.168.2.1 1234

echo man pagesays: -n do not output the trailing newline

回声手册页说: -n do not output the trailing newline

回答by c00kiemon5ter

echousually provides -nflag. This is not standard.

echo通常提供-n标志。这不是标准的。

stringA string to be written to standard output. If the first operand is -n, or if any of the operands contain a backslash ( '\' ) char‐ acter, the results are implementation-defined.

On XSI-conformant systems, if the first operand is -n, it shall be treated as a string, not an option.

string要写入标准输出的字符串。如果第一个操作数是 -n,或者任何一个操作数包含反斜杠 ('\') 字符,则结果是实现定义的。

在符合 XSI 的系统上,如果第一个操作数是 -n,则应将其视为字符串,而不是选项。



I would suggest using printf

我建议使用 printf

printf "A 192.168.192.168" | nc -u 192.168.2.1 1234

printfdoesnt append a new line unless it is told to, and it's standard behavior.

printf除非被告知,否则不会追加新行,这是标准行为。