Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 1\ncommand 2\ncommand3" | ./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?

share|improve this question
    
Have you tried adding ./shell_executable to the end of the list of commands? That might keep it going, although you'll have two running instances (parent & child). –  TAFKA 'goldilocks' Dec 5 '13 at 17:31
add comment

2 Answers

up vote 7 down vote accepted

Found this clever answer in a similar question at stackoverflow

(echo -e "cmd 1\ncmd 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.

share|improve this answer
    
@jw013 You are right. I edited the post. Thanks. When I experimented w/o the parentheses, I saw cmd 1 written back but did not realize that it should have been the o/p generated by cmd 1 –  Gowtham Dec 5 '13 at 18:53
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.