Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Piping input to a shell command and keeping the created shell alive

Tags:

bash

shell

piping

My overarching program is a shell script. This shell script calls a C program that I need to pipe input to, and ultimately the C program will create a shell.

However, when I pipe my input into the C program within the shell script

Do_Other_Stuff
./my_prog < file1

I can't get the shell to stay alive. Running just,

Do_Other_Stuff
./my_prog

works, as I have to input the stdin myself, and the shell correctly spawns when my_prog exits. I'm pretty sure wrapping up the ./my_prog call in a C program, and compiling and running that would work, but I'm curious as to whether there's a cleaner way with shell.

I've tried several combinations of using cat file1 | ./my_prog and using & in different situations, and haven't had any success.

Thanks!

like image 236
user1679039 Avatar asked Dec 18 '25 03:12

user1679039


2 Answers

Try:

cat file1 - | ./myprog

Many programs recognize the "filename" - to mean stdin.

like image 171
Barmar Avatar answered Dec 20 '25 20:12

Barmar


Do you have access to the C program source code? My guess is that the C program is using istty(0) to determine if stdin is coming from a terminal. It probably only creates an interactive shell when that is the case. Using stdin redirection, whether from a file or a pipe, means that istty(0) returns false.

like image 44
cdarke Avatar answered Dec 20 '25 18:12

cdarke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!