将数据写入Shell脚本中可执行文件的标准输入。

huangapple go评论74阅读模式
英文:

Write data to stdin of an executable file in the shell script

问题

我正在做一个项目,我试图构建一些基本功能。我运行简单的C代码,从标准输入文件流中读取数据并打印。代码的简短片段如下所示。在我的应用程序中,我想启动一个C代码,它通过标准输入读取数据并对其进行一些操作,同时会有另一个进程定期向标准输入发送数据。请查看伪代码以获得更好的理解。因为我是一个初学者,对于shell脚本方面的知识不够,我不知道如何完成这个任务。所以,如果有人做过类似的事情或者知道如何做,请帮帮我。

#!/bin/sh
#我知道这不会起作用,因为运行代码和cat命令引用了不同的文件流
clear # 清屏
echo "运行shell命令" # 开始时的基本echo
./a.out & # 在后台运行编译后的C代码 
while true 
  do
    sleep 2 #2秒的延迟
    cat <<< "# 检查 $\n" # 将数据发送到标准输入
  done
exit 0
//常规的C文件,命令: cc 文件名.c 
#include <stdio.h>
#include <unistd.h>
int FnReceiveCharacter(void)
{      
   unsigned char c = 0, d;
   d = read(STDIN_FILENO, &c, 1);
   fflush(stdin);   
   return c;
}
int main( )
{
  while(1) printf("%c",FnReceiveCharacter( ));
}
英文:

I'm working on a project where I'm trying to build some basic functionality. I'm running simple c code, which reads the data from the stdin file stream and prints. The short snippet of the code is attached below. In my application, I want to start a c code which reads the data through stdin and performs some operation on it, and there will be another process running which sends the data to stdin periodically. Check the pseudo shell script for a better understanding. As I'm a beginner with shell scripting, I don't know how to achieve this task. So, please help me if anyone has done something similar or has clues on how to do it.

#!/bin/sh
#I know this won&#39;t work because running code and cat command are referring to a different file stream
clear # clearing the screen
echo &quot;running the shell command&quot; #basic eco in the beginning
./a.out &amp; # Run the compiled c code in the background 
while true 
  do
    sleep 2 #2-sec delay
    cat &lt;&lt;&lt; &quot;# check $\n&quot; # send the data to the stdin
  done
exit 0

.

//regular c file, command: cc filename.c 
#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;
int FnReceiveCharacter(void)
{      
   unsigned char c = 0, d;
   d = read(STDIN_FILENO, &amp;c, 1);
   fflush(stdin);   
   return c;
}
int main( )
{
  while(1) printf(&quot;%c&quot;,FnReceiveCharacter( ));
}

答案1

得分: 3

以下是翻译好的内容:

最好我能告诉你的是:

$ cat tst.sh
#!/usr/bin/env bash

./a.out < <(
    while true; do
        sleep 2
        echo '# check'
    done
)
$ ./tst.sh
# check
# check
...

请参阅https://mywiki.wooledge.org/BashFAQ/001中“输入源选择”部分的底部,了解为什么我在这里使用< <(...)重定向,而不是管道,有关进程替代构造的信息。

英文:

Best I can tell all you need is:

$ cat tst.sh
#!/usr/bin/env bash

./a.out &lt; &lt;(
    while true; do
        sleep 2
        echo &#39;# check&#39;
    done
)

<p>

$ ./tst.sh
# check
# check
...

See the bottom of the "Input source selection" section in https://mywiki.wooledge.org/BashFAQ/001 for why I'm using that &lt; &lt;(...) redirection from process substitution construct instead of a pipe.

huangapple
  • 本文由 发表于 2023年5月17日 22:22:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273154.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定