0

I have a shell script plan.sh which expects a user input (a/b) and then asks for another input (y/n).

I want to run this in one line like sh -a -y plan.sh which doesn't work.

I tried this too

./plan.sh << END_OF_INPUT
a
y
END_OF_INPUT

which throws an error: /bin/bash: warning: here-document at line 0 delimited by end-of-file (wanted 'END_OF_INPUT')

Is there a way to do this better?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Red Bottle
  • 2,839
  • 4
  • 22
  • 59
  • 1
    I assume that you have a space or tab after the last `END_OF_INPUT` in your original code. – Cyrus Feb 14 '23 at 18:56
  • `./plan.sh <<<$'a\ny\n'`, if your shell is `bash` and not `sh` – Charles Duffy Feb 14 '23 at 19:02
  • `printf '%s\n' a y | ./plan.sh`, to work on _any_ POSIX-compliant shell. – Charles Duffy Feb 14 '23 at 19:02
  • 1
    But your original code should work fine if the file _really_ contained only 100% precisely the content shown in the question. Seems very likely there's indentation or something else off; we need a [mre] that lets us cause the problem ourselves to be able to provide a certain explanation. – Charles Duffy Feb 14 '23 at 19:03
  • 2
    (And just to emphasize: `sh yourscript` forces your script to be run with `sh`, not bash; even if your operating system is one where `/bin/sh` is a symlink to `/bin/bash`, it still turns off some bash-only features when started under the `sh` name -- and many common operating systems, like Debian and Ubuntu, don't use bash for sh at all; so if you want to ask a question with the bash Stack Overflow tag, you should be running your code with _bash_, not with _sh_) – Charles Duffy Feb 14 '23 at 19:08
  • Another idea is changing `plan.sh` and add parameters for the two questions. – Walter A Feb 14 '23 at 20:23
  • See [Difference between sh and Bash](https://stackoverflow.com/q/5725296/4154375). – pjh Feb 14 '23 at 21:09

0 Answers0