围绕nc命令编写的bash脚本。如何在每行之前添加文本?

时间:2021-06-29 01:47:57

I'm using the nc command in an unusual way. I have two bash scripts that initiate a chat session if two users run them at approximately the same time. These scripts are built around the nc command. I want the stream of text to display a value appended after each carriage return (when the user presses enter).

我以不寻常的方式使用nc命令。我有两个bash脚本,如果两个用户几乎同时运行它们,则启动聊天会话。这些脚本是围绕nc命令构建的。我希望文本流在每次回车后显示一个值(当用户按下回车键时)。

Here is how the scripts work interactively and collaboratively: On workstation 1, I invoke this: nc -l 44444

以下是脚本如何以交互方式和协作方式工作:在工作站1上,我调用它:nc -l 44444

On workstation 2, someone else invokes this: nc x.x.x.x 44444 (where x.x.x.x is the IP address of workstation 1)

在工作站2上,其他人调用此命令:nc x.x.x.x 44444(其中x.x.x.x是工作站1的IP地址)

I can chat away with someone else. Whatever either user types is mirrored to the other terminal. My question: how do I write a bash script to append text? I have the text I want in a variable (e.g., "said user1").

我可以和别人聊天。无论哪种用户类型镜像到另一个终端。我的问题:我如何编写一个bash脚本来附加文本?我在变量中有我想要的文本(例如,“所说的user1”)。

I want this to be displayed automatically (without the users manually typing it in).

我想让它自动显示(没有用户手动输入)。

If user on workstation 1 (with IP addresss x.x.x.x) during the chat session types "this is just a test" and presses enter, I want this to be the result:

如果在聊天会话期间工作站1上的用户(IP地址为x.x.x.x)键入“这只是一个测试”并按下回车键,我希望这是结果:

this is just a test said user1.

这只是一个测试说user1。

If user on workstation 2 (with IP addresss y.y.y.y) during the chat session types "oh, ok" and presses enter, I want this to be the result:

如果聊天会话期间工作站2上的用户(IP地址为y.y.y.y)键入“哦,确定”并按回车键,我希望这是结果:

oh, ok said user2.

哦,确定用户2。

The user names are in variables in the scripts. I can construct the string without a problem. I just don't know how to get this variable string to display automatically. Can it some text be displayed every time enter is pressed only when the scripts are running? The stream of text is mirrored when a bash script is running. I'm not sure how to accomplish this.

用户名在脚本中的变量中。我可以毫无问题地构造字符串。我只是不知道如何让这个变量字符串自动显示。每次只在脚本运行时按下输入,是否可以显示一些文本?运行bash脚本时会镜像文本流。我不知道如何做到这一点。

2 个解决方案

#1


0  

Try this:

sed -u "s/$/said user2 /" | nc xxx.xxx.xxx.xxx 44444
sed -u "s/$/ said user1 /" | nc -l 44444

#2


0  

When nc is running interactively (i.e. running as a foreground job), its standard input is connected to the terminal, and Bash can't just send input to it too. IMHO, the easiest approach would be to (for each line) read the input in Bash and add the suffix there, then pipe both of it to a new invocation of nc.

当nc以交互方式运行时(即作为前台作业运行),其标准输入连接到终端,而Bash也不能仅向其发送输入。恕我直言,最简单的方法是(对于每一行)读取Bash中的输入并在那里添加后缀,然后将它们都管道到新的nc调用。

Keep in mind that nc will ignore end-of-file (EOF) when reading from standard input, see nc(1). The -q option tells nc to close the connection after EOF on the client side, and you could just run nc -l in a loop on the server side.

请记住,当从标准输入读取时,nc将忽略文件结束(EOF),请参阅nc(1)。 -q选项告诉nc在客户端EOF之后关闭连接,你可以在服务器端的循环中运行nc -l。

#1


0  

Try this:

sed -u "s/$/said user2 /" | nc xxx.xxx.xxx.xxx 44444
sed -u "s/$/ said user1 /" | nc -l 44444

#2


0  

When nc is running interactively (i.e. running as a foreground job), its standard input is connected to the terminal, and Bash can't just send input to it too. IMHO, the easiest approach would be to (for each line) read the input in Bash and add the suffix there, then pipe both of it to a new invocation of nc.

当nc以交互方式运行时(即作为前台作业运行),其标准输入连接到终端,而Bash也不能仅向其发送输入。恕我直言,最简单的方法是(对于每一行)读取Bash中的输入并在那里添加后缀,然后将它们都管道到新的nc调用。

Keep in mind that nc will ignore end-of-file (EOF) when reading from standard input, see nc(1). The -q option tells nc to close the connection after EOF on the client side, and you could just run nc -l in a loop on the server side.

请记住,当从标准输入读取时,nc将忽略文件结束(EOF),请参阅nc(1)。 -q选项告诉nc在客户端EOF之后关闭连接,你可以在服务器端的循环中运行nc -l。