Bash与执行的二进制文件通信。怎么样?

时间:2011-11-18 13:56:02

标签: bash

我在bash脚本中运行二进制文件就像要与它通信...(二进制文件使用stdin,stdout)

#!/bin/bash
touch kacsa.txt
echo `./bbox 2`>>kacsa.txt
echo 2
echo 2
read -r KACSA >> kacsa.txt

我想用选项2运行bbox,然后在读取输出后给出一个简单的输入(2,2),并将其放在文本文件(kacsa.txt)中请帮助

示例:我可以像这样运行bbbox

>>./bbox 2   //it runs now
2          // it was written by
2          //it was written by me
4            // bbx write in console
I want to do the same thing with bash... the outup should goes tho an text file "kacsa.txt"

2 个答案:

答案 0 :(得分:1)

我认为你可以使用mkfifo

mkfifo passthrough.fifo
  • 流程1

    for a in 1 2 3 4 5; 
    do 
        echo hello world $a; 
        sleep 1; 
    done >> passthrough.fifo &
    
  • 流程2

    while read greeting target number < passthrough.fifo
    do
         echo received a greeting directed at "$target" saying: "'$hello'" (the number is $number)
    done
    

任何一个进程(或两者)都可以是二进制代替,当然

联合程序

或者查看 bash 4+ coproc

答案 1 :(得分:1)

试试这个:

#!/bin/bash
touch kacsa.txt
(echo 2 ; echo 2) | ./bbox 2 >> kacsa.txt

./bbox的标准输入由管道输送。

./bbox的stdout放入文件中。