0

I want to run command in console and insert all user data needed.

#!/bin/bash
program < data &

My code works, but after less than second program disappears (only blinks). How can I run program, pass data from file and stay in that program(I have no need to continue bash script after app launching.)

formateu
  • 157
  • 2
  • 17
  • 1
    Helpful information: [What are the uses of the exec command in shell scripts?](http://stackoverflow.com/questions/18351198/what-are-the-uses-of-the-exec-command-in-shell-scripts) – Jonny Henly Jun 27 '16 at 19:30
  • I'm uncertain what it means that the "second program disappears (only blinks)." If you redirect input from a file, however, then the program will see end-of-file on its standard input when the end of the file is reached. It is not uncommon for programs to shut down in that circumstance. – John Bollinger Jun 27 '16 at 19:55
  • John Bollinger: I understand this, but is it possible to switch to a new program and pass input for stdin using bash script? I read Jonny Henly link and using exec I can switch to a new program, but after that operation bash script no longer runs, so I really cannot do this. – formateu Jun 27 '16 at 19:59

1 Answers1

1

Inasmuch as the program you are launching reads data from its standard input, it is reasonable to suppose that when you say that you want to "stay in that program" you mean that you want to be able to give it further input interactively. Moreover, I suppose that the program disappears / blinks either because it is disconnected from the terminal (by operation of the & operator) or because it terminates when it detects end-of-file on its standard input.

If the objective is simply to prepend some canned input before the interactive input, then you should be able to achieve that by piping input from cat:

cat data - | program

The - argument to cat designates the standard input. cat first reads file data and writes it to standard out, then it forwards data from its standard input to its standard output. All of that output is fed to program's standard input. There is no need to exec, and do not put either command into the background, as that disconnects it from the terminal (from which cat is obtaining input and to which program is, presumably, writing output).

John Bollinger
  • 160,171
  • 8
  • 81
  • 157