How to send text to a process in a shell script?

So, I have a Linux program that works after a while (true), which waits for user input, processes it and displays the result on stdout.

I want to write a shell script that opens this program, feed the lines from the txt file, one line at a time and save the program output for each line to a file.

So, I want to know if there is any command for:
  - open a program
- send text to a process
- get output from this program

Many thanks.

+3
source share
3 answers

It looks like you want something like this:

cat file | while read line; do
    answer=$(echo "$line" | prog)
done

prog . prog, answer script .

" " , . , done:

while read line; do
    answer=$(echo "$line" | prog)
done < file
+1

? . / .

+1

, script, bash.

  • , .
  • , |, < ( )
  • To get the output, you use >to redirect the output to some file or >>to redirect, but add the results instead of trimming the file

To achieve the desired result in bash, you can write:

#/bin/bash

cat input_file | xargs -l1 -i{} your_program {} >> output_file

This calls your_programfor each row of input_fileand adds the results tooutput_file

+1
source

Source: https://habr.com/ru/post/1737062/


All Articles