Operating System - HP-UX
1752770 Members
5068 Online
108789 Solutions
New Discussion юеВ

Korn Help: need to enter two "y"s onto a command in a script

 
Hanry Zhou
Super Advisor

Korn Help: need to enter two "y"s onto a command in a script

I have a script and it will be basically repeating to run a command on a lot of different parameters, then this command requires to enter two "y"s, and then enter after each "y".

What can I do to allow the script to enter a "y", and then RETRUN, then one more "y" again, then RETURN?

Tried differnet ways, but could not figure that out. Appreciate your help!

none
8 REPLIES 8
Steven Schweda
Honored Contributor

Re: Korn Help: need to enter two "y"s onto a command in a script

> I have a script and it will be basically [...]

   With my weak psychic powers, I can't see your script, and what it
"basically" does is probably much less important than what it _actually_
does.

> [...] Appreciate your help!

   Perhaps, but you might waste less of everyone's time if you didn't
make your readers guess what you're doing.  As usual, showing actual
commands with their actual output can be more helpful than vague
descriptions or interpretations.  Examples of useless descriptions:

      run a command on a lot of different parameters
      this command
      to enter a "y"
      Tried [different] ways


   For a good time, try a Web search for:
      how to ask a technical question
Then, do some reading.  Then, try asking a question which a non-psychic
might have some chance of being able to answer.

Bill Hassell
Honored Contributor

Re: Korn Help: need to enter two "y"s onto a command in a script

There is a program in HP-UX called yes. (see: man yes)

Since there are no details about how you are asking the question in the script, you'll have to figure out how to connect the yes program to the question. If your script only expects the "y" then you can feed the answers like this:

yes | script_name <commandLine_stuff>
or
yes | head -2 | script_name <commandLine_stuff>

BTW: to see it working:

$ yes | head -2
y
y
$ yes no | head -3
no
no
no

 



Bill Hassell, sysadmin
Dennis Handly
Acclaimed Contributor

Re: KSH Help: need to enter two "y"s onto a command in a script

You could also use a here-doc where you supply the input:

script <<EOF

inputs ...

EOF

 

Unless you quote the EOF word, $ expansions are done in your inputs.

Hanry Zhou
Super Advisor

Re: KSH Help: need to enter two "y"s onto a command in a script

cat sani.sh
for i in `cat list`
do
ssh user_id@ip command op1 opt2 -c 6 $i
done

to beigining with, I just did one interactive execution, and it doesn't work.

$ yes | head -2 | ssh user_id@ip command op1 opt2 -c 6 0a.16

Would you like to continue (y/n)? Please answer yes or no.
Would you like to continue (y/n)?

It did not accept what being fed.

Hope it is clear this time. Thanks!

 

none
Steven Schweda
Honored Contributor

Re: KSH Help: need to enter two "y"s onto a command in a script

> to beigining with, I just did one interactive execution, [...]

   That's a good way to start.

> $ yes | head -2 | ssh user_id@ip command op1 opt2 -c 6 0a.16

   What happens without the "yes | head -2 | " part?

> Would you like to continue (y/n)? Please answer yes or no.
> Would you like to continue (y/n)?
>
> It did not accept what being fed.

   From here, it's not clear who "It" is.  Are the "Would you like to
continue" questions coming from "ssh" or from "command" (whatever that
might be)?  If you "ssh" (as "user_id") to "ip", can you make "command
[...]" work interactively?

> Hope it is clear this time.  [...]

   Better, but not good.

   It might be helpful if you said what the operating system at "ip" is,
and what "command" is. If the program which is asking the questions is
not reading from stdin, then you may not be able to pipe data into it.

   There may be other ways to make this kind of thing work, but with no
real information on what "this kind of thing" actually is, you're asking
for more guesswork (again).

Patrick Wallek
Honored Contributor

Re: KSH Help: need to enter two "y"s onto a command in a script

If the remote end is HP-UX, then you might try something like this instead:

 

ssh user_id@ip yes | head -2 | command op1 opt2 -c 6 0a.16

 

This way the "yes" is run on the destination rather than the source.

 

But that's just a shot in the dark since we're not sure about the destination.

Steven Schweda
Honored Contributor

Re: KSH Help: need to enter two "y"s onto a command in a script

> ssh user_id@ip yes | head -2 | command op1 opt2 -c 6 0a.16
>
> This way the "yes" is run on the destination rather than the source.

   Unfortunately, that way, "yes" is the _only_ thing which is run on
the remote system.  For example (on a Mac or two):

mba$ uname -n
mba.local

mba$ ssh pro3 pwd | uname -n
mba.local

   With some quotation, more of the pipeline can be run on the remote
system:

mba$ ssh pro3 'pwd | uname -n'
pro3.antinode.info

> But that's just a shot in the dark since we're not sure about the
> destination.

   Among many other things about which we have no useful information.

Dennis Handly
Acclaimed Contributor

Re: KSH Help: need to enter two "y"s onto a command in a script

Try using ssh -n to prevent stdin from being eaten up:

for host in $(< list); do
   ssh -n user_id@ip command op1 opt2 -c 6 $host
done

 

If you need that yes(1) solution, you would need to put it inside the ssh command pipeline.