Reputation: 41
I have a program that users can run using the command line. Once running, it receives and processes commands from the keyboard. It's possible to start the program with input from disk like so: $ ./program < startScript.sh
. However, the program quits as soon as the script finishes running. Is there a way to start the program with input from disk using <
that does not quit the program and allows additional keyboard input to be read?
Upvotes: 0
Views: 80
Reputation: 8774
(cat foo.txt; cat) | ./program
I.e., create a subshell (that's what the parentheses do) which first outputs the contents of foo.txt and after that just copies whatever the user types. Then, take the combined output of this subshell and pipe it into stdin of your program.
Note that this also works for other combinations. Say you want to start a program that always asks the same questions. The best approach would be to use "expect" and make sure the questions didn't change, but for a quick workaround, you can also do something like this:
(echo y; echo $file; echo n) | whatever
Upvotes: 1
Reputation: 4526
That depends on how the program
is coded. This cannot be achieved from writing code in startScript.sh
, if that is what you're trying to achieve.
What you could do is write a callingScript.sh
that asks for the input first and then calls the program < startScript.sh
.
Upvotes: 0
Reputation: 1602
Why not try something like this
BODY=`./startScript.sh`
if [ -n "$BODY" ]
then cat "$BODY" |./program
fi
Upvotes: 0
Reputation: 5213
Use system("pause")(in bash it's just "pause") in your program so that it does not exit immediatly.
There are alternatives such as
Upvotes: 0