David Lee
David Lee

Reputation: 41

Is it possible to start a program from the command line with input from a file without terminating the program?

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

Answers (4)

Christopher Creutzig
Christopher Creutzig

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

Chirag Bhatia - chirag64
Chirag Bhatia - chirag64

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

Travis G
Travis G

Reputation: 1602

Why not try something like this

BODY=`./startScript.sh`
if [ -n "$BODY" ]
    then cat "$BODY" |./program
fi

Upvotes: 0

Dmytro
Dmytro

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

  • dummy read
  • infinite loop
  • sigsuspend
  • many more

Upvotes: 0

Related Questions