Ubuntu – How to pass answers to CLI script via arguments

command line

I'm writing a CLI script that asks a series of questions before doing a few things. How can I pass them as arguments so that I don't have to keep entering them every time I want to test my script?

Basically, it should pass 4 items to STDIN, like "text1[ENTER]text2[ENTER]text3[ENTER]text4[ENTER]" automatically.

Yes, I could modify my script to actually read the shell arguments, but I don't want to do it that way, since it's supposed to run more like a wizard.

Looking for something like

SOMEPROGRAM myscript arg1 arg2 arg3 arg4

or

SOMEPROGRAM arg1 arg2 arg3 arg4 | myscript

Or something like that. Is there such a program?

Best Answer

I understand you do not want to modify myscript.

Regarding the second solution you ask for, you can use printf:

printf '%s\n' text1 text2 text3 text4 | myscript

so that, defining an alias for SOMEPROGRAM as:

alias SOMEPROGRAM="printf '%s\n'" 

you could effectively call

SOMEPROGRAM text1 text2 text3 text4 | myscript

The first form is ambiguous (from the point of view of SOMEPROGRAM), because it don't know where myscript options end and text parameters start, unless myscript is effectively invoked without any options. In this case you could use a function:

SOMEPROGRAM() {
  myscript="$1"
  shift
  printf '%s\n' "$@" | "$myscript"
}

so that you could effectively call

SOMEPROGRAM myscript text1 text2 text3 text4