1

I have a bash script bash.sh that looks like this:

#!/usr/bin/env bash
/usr/local/sbin/pythonScript.py firstArgument secondArgument

If I call the pythonScript.py from my terminal with the two command line arguments it asks me the question " Do you really want to accept this arguments ? ( yes / no )" and I have to type yes or no and press enter in the terminal in order to resume the script execution.

Now I want that my bash.sh script is doing the yes answer for me but I have no clue how.

I tried something like this in my script bash.sh

#!/usr/bin/env bash
echo "yes\n" | /usr/local/sbin/pythonScript.py firstArgument secondArgument

Can anyone help me how to provide dynamic command line arguments to a python script and also simulate the ENTER key press.

EDIT:

The python script is reading from On Unix, the prompt is written to the file-like object stream. stream defaults to the controlling terminal (/dev/tty)

  • echo "yes" (no \n) will send the string yes followed by a newline. This will work unless the Python code is reading directly from the TTY (i.e. not from standard input). Does this not work for you? – Kusalananda Dec 11 '17 at 13:23
  • And echo "yes\n" from Bash will output the literal string yes\n (followed by a newline), which may not be what your python program expects. – ilkkachu Dec 11 '17 at 13:34
  • 3
    Maybe it's not exact answer for your question but please take a look at expect. It's a tool for the cases like yours. – pawel7318 Dec 11 '17 at 14:11
  • 1
    You can also try echo -e "yes\n" – pawel7318 Dec 11 '17 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.