I have an executable that starts a user-interactive shell. I would like to, upon launch of the shell, inject a few commands first, then allow the user to have their interactive session. I can do this easily using echo:
echo "command 1ncommand 2ncommand3" | ./shell_executable
This almost works. The problem is that the echo command that is feeding the process’s stdin hits EOF once it’s done echoing my commands. This EOF causes the shell to terminate immediately (as if you’d pressed Ctrl+D in the shell).
Is there a way to inject these commands into stdin without causing an EOF afterwards?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Found this clever answer in a similar question at stackoverflow
(echo -e "cmd 1ncmd 2" && cat) | ./shell_executable
This does the trick. cat will pump in the output of echo into input stream of shell_executable and wait for more inputs until EOF.
Method 2
The cleanest way to do this is probably to look for something like bash‘s --rcfile option. Put your custom commands in your custom file and pass it to the interactive shell to run on start-up.
If no such option exists you can also try the following:
cat custom_commands_file - | ./shell_executable
cat will interpret - as stdin.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0