1

I'm executing uninstall.sh in within bash script which access two std inputs, "yes" and it executes some steps, and then another "y" for to execute another steps as confirmation.

Tried using here doc option as below:

./uninstall.sh <<"EOF"
yes
y
EOF

or

./uninstall.sh <<EOF1 <<EOF2
yes
EOF1
y
EOF2

but it only accepts yes and first stdin and for seconds, it does not take "y" as input, giving below error:

java.util.NoSuchElementException: No line found

What I'm missing here ?

chepner
  • 497,756
  • 71
  • 530
  • 681
  • 1
    You should write `yes` and `y` on their own line, input is often consumed line by line (such as with the `read` builtin). A single heredoc is enough to do so, and [`<<<`](https://unix.stackexchange.com/questions/80362/what-does-mean) + a multiline string would work as well – Aaron Jul 26 '20 at 11:06
  • Well how bash usually reads input is not relevant since your input's consumer seems to be java code, but given the error's `No line found` I think you're having the same problem where the code uses `Scanner.readLine` or equivalent to read the input and consumes the two input you want to send when they're on the same line – Aaron Jul 26 '20 at 11:11
  • try `yes | ./uninstall.sh` – Gereon Jul 26 '20 at 11:34
  • tried adding yes and y in separate lines, but it only takes yes as stdin and failed for y – tanmay shirnalkar Jul 26 '20 at 11:48
  • Try `./uninstall.sh <<< $'yes\ny'` – M. Nejat Aydin Jul 26 '20 at 12:03
  • @M.NejatAydin That's essentially identical to the first, two-line here document. – chepner Jul 26 '20 at 12:53
  • I would expect the first one to work. If it doesn't, you need to provide more detail about how `uninstall.sh` actually reads its standard input. – chepner Jul 26 '20 at 12:54
  • @chepner It wasn't the case when I wrote that comment. – M. Nejat Aydin Jul 26 '20 at 13:00
  • 1
    Perhaps the script reads both from stdin and from the tty. Or perhaps it invokes a utility (such as `rm`) that is reading from the tty. Either way, you need to either dig in to the script and see the details, or strace it to see what it is doing. And file a bug with the maintainer of the script. – William Pursell Jul 26 '20 at 13:25
  • 1
    Please [edit] your question to include a complete but minimal script that demonstrates the problem. – Ed Morton Jul 26 '20 at 19:57

1 Answers1

0

You have mentioned that you are using java. If you are using scanner.nextLine() to read both the inputs try following command

printf "yes\ny" | ./uninstall.sh

If you are using scanner.next() to read both the inputs try

printf "yes y" | ./uninstall.sh

Make sure you are not closing the input stream using scanner.close() after reading the first input. Check if you are having same issue as https://stackoverflow.com/a/13042296/10879454

Vishal
  • 145
  • 7