Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to Automate a Bash Script Prompt with Carriage Return

547 views
Skip to first unread message

BeeRich

unread,
Nov 13, 2015, 5:48:12 AM11/13/15
to
Hi folks.

I wrote a bash script that updates gems and reports the status of an application, etc. During the status check, it asks me a question, which always requires a carriage return. How can I modify my script to automatically send a carriage return when that prompt arrives?

I appreciate any input.

Cheers

Ed Morton

unread,
Nov 13, 2015, 6:51:27 AM11/13/15
to
Simply add:

printf "\n"

after line 17.

Ed.

P.S. Sometimes my crystal ball cant QUITE see your code clearly so treat the
above with a grain of salt.

Kaz Kylheku

unread,
Nov 13, 2015, 8:48:23 AM11/13/15
to
On 2015-11-13, BeeRich <bee...@gmail.com> wrote:
> Hi folks.
>
> I wrote a bash script that updates gems and reports the status of an
> application, etc. During the status check, it asks me a question, which
> always requires a carriage return. How can I modify my script to
> automatically send a carriage return when that prompt arrives?

If a program reads something from standard input, you can pipe it in:

echo answer | program_which_asks_question

If a program asks "are you sure, [y/n]? " numerous times, the yes utility
can generate all the y answers:

yes | program_asking_yes_or_no

With some programs, redirecting to their standard input won't work, because
some programs open the controlling TTY device and use it directly.

To automate such programs you need the expect utility, or something similar.
The expect utility creates a fake terminal session around a pseudo TTY
device, and runs the program in that session. Your expect script
carries out a TTY conversation with the program through the master side
of the pseudo TTY.

BeeRich

unread,
Nov 15, 2015, 8:35:20 PM11/15/15
to
On Friday, November 13, 2015 at 6:51:27 AM UTC-5, Ed Morton wrote:
Didn't work:

sudo passenger-config validate-install

printf "\n"

BeeRich

unread,
Nov 15, 2015, 8:37:39 PM11/15/15
to
On Friday, November 13, 2015 at 8:48:23 AM UTC-5, Kaz Kylheku wrote:

> yes | program_asking_yes_or_no

Aha that worked well. Thank you.

Kaz Kylheku

unread,
Nov 15, 2015, 9:47:53 PM11/15/15
to
But careful with that; it says "y" to every question the program will ask, no
matter how many. You have to be sure that your really want to bust through
all the program's questions with a "y" answer.

BeeRich

unread,
Nov 18, 2015, 12:49:04 PM11/18/15
to
On Sunday, November 15, 2015 at 9:47:53 PM UTC-5, Kaz Kylheku wrote:

> But careful with that; it says "y" to every question the program will ask, no
> matter how many. You have to be sure that your really want to bust through
> all the program's questions with a "y" answer.

Yeah I assumed that. I have one question where the default answer is always selected anyway. Is there a way of providing a list of answers to something like this?

Cheers

Barry Margolin

unread,
Nov 18, 2015, 1:27:16 PM11/18/15
to
In article <a64f5ec1-80f7-4648...@googlegroups.com>,
You can use a here-doc:

command <<EOF
y
n
foo
EOF

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Kaz Kylheku

unread,
Nov 18, 2015, 1:56:20 PM11/18/15
to
Solutions:

# answers from file
program < file-of-answers

# equivalent to above
< file-of-answers program

# answers from command line
( echo y ; echo n; echo y; echo y ) | program

# more compact with printf, though less readable:
printf "y\nn\ny\ny\n" | printf

# embedded literal newlines
echo "y
n
y
y" | program

Geoff Clare

unread,
Nov 19, 2015, 9:11:09 AM11/19/15
to
Kaz Kylheku wrote:

> # answers from command line
> ( echo y ; echo n; echo y; echo y ) | program
>
> # more compact with printf, though less readable:
> printf "y\nn\ny\ny\n" | program
>
> # embedded literal newlines
> echo "y
> n
> y
> y" | program

I prefer this to all of those:

printf '%s\n' y n y y | program

--
Geoff Clare <net...@gclare.org.uk>

Kaz Kylheku

unread,
Nov 19, 2015, 9:37:51 AM11/19/15
to
On 2015-11-19, Geoff Clare <ge...@clare.See-My-Signature.invalid> wrote:
> Kaz Kylheku wrote:
>
>> # answers from command line
>> ( echo y ; echo n; echo y; echo y ) | program
>>
>> # more compact with printf, though less readable:
>> printf "y\nn\ny\ny\n" | program
>>
>> # embedded literal newlines
>> echo "y
>> n
>> y
>> y" | program
>
> I prefer this to all of those:
>
> printf '%s\n' y n y y | program

Oh yeah, doh!

BeeRich

unread,
Nov 20, 2015, 3:01:50 PM11/20/15
to
On Wednesday, November 18, 2015 at 1:56:20 PM UTC-5, Kaz Kylheku wrote:

> > Is there a way of providing a list of answers to something
> > like this?
>
> Solutions:
>
> # answers from file
> program < file-of-answers
>
> # equivalent to above
> < file-of-answers program
>
> # answers from command line
> ( echo y ; echo n; echo y; echo y ) | program
>
> # more compact with printf, though less readable:
> printf "y\nn\ny\ny\n" | printf
>
> # embedded literal newlines
> echo "y
> n
> y
> y" | program

Excellent. Thank you.
0 new messages