Put file contents on stdin without sending eof

A typical way to put the contents of a file on stdin is as follows:

./command.sh < myfile 

This puts the entire contents of myfile in stdin and then sends eof. I want to post content to stdin without adding EOF.

For various interactive programs, I want to start the program with a sequence of commands, but then continue interacting with the program. If eof is not sent, the program will wait for more input, which I could then enter interactively.

Is this possible in bash? My current solution is to drop the contents into the clipboard and then just paste them. Is there a better way to do this?

+6
source share
3 answers

Just merge the file with stdin with the cat :

 ./command.sh < <(cat myfile -) 

or

 cat myfile - | ./command.sh 

cat team

cat denote con cat enate :

 man cat NAME cat - concatenate files and print on the standard output SYNOPSIS cat [OPTION]... [FILE]... DESCRIPTION Concatenate FILE(s), or standard input, to standard output. ... 

(please R ead T he F ine M anual ;-)

You can write

 cat file1 file2 file3 ... fileN 

and

 cat file1 - file2 cat - file1 cat file1 - 

depending on your need ...

+3
source

The solution for this is using fifo .

 test -p /tmp/fifo || mkfifo /tmp/fifo while true; do read line < /tmp/fifo echo "$line" [[ $line == break ]] && $line done 

and to feed fifo :

 echo foobar > /tmp/fifo 

To stop listening:

 echo break > /tmp/fifo 

See man mkfifo

+2
source

Another way is to use another script to enter your data:

inputer.sh :

 cat $1 while read line; do echo $line done 

Usage :

 sh inputer.sh input_file | sh your-script.sh 
+2
source

All Articles