Operating System - HP-UX
1821059 Members
2512 Online
109631 Solutions
New Discussion юеВ

Automatic response in shell script

 
SOLVED
Go to solution
Melvin Thong
Advisor

Automatic response in shell script

Hi,

I am creating a Unix shell script to execute a command. The script is executed by root ID. However before I can execute a command, I need to change the user ID which is hnmadm. In the script I added a line "su - hnmadm". Unfortunately, it prompted me 2 questions require me to input:
Use Default Diplay <<10.200.64.100>> (y/n)?
Do you wish to start Net Bos Menu (y or n)?

The first question I would like to answer "y" and the second question I would answer "n".

How can I actually pass in the answer to the script so it will not halt waiting for response?

You assistance and advice is very much appreciated. Thank you.


Regards,
Melvin
5 REPLIES 5
Stefan Farrelly
Honored Contributor
Solution

Re: Automatic response in shell script

Normally you can do;

echo y | su - ...

but this will answer y to everything, try this instead;

echo "y\n n\n" | su - ...
Im from Palmerston North, New Zealand, but somehow ended up in London...
Darren Prior
Honored Contributor

Re: Automatic response in shell script

Hi Melvin,

If you don't need that user's .profile to be run you can omit the dash (-) from the su command. Of course, you will then have to manually configure the environment if parts of it are needed.

regards,

Darren.
Calm down. It's only ones and zeros...
John Meissner
Esteemed Contributor

Re: Automatic response in shell script

you could also download and install 'expect' which can generate automated responses to scripts for you. http://expect.nist.gov/#unix
I use it to automatically telnet to other servers, export my display and launch applications for me. I find this very useful.

Once expect is installed you simply type:
autoexpect -f commands
when you are done the is your script. I'm attaching an example of one of my automatically generated scripts for you to look at
All paths lead to destiny
Bill Hassell
Honored Contributor

Re: Automatic response in shell script

This is caused by putting interactive questions into the user's login profile, none of which are needed when running a batch job such as this script. A good sysadmin will make sure that /etc/profile and /etc/csh.login have protected their tty commands so they will not be run without a 'real' terminal (commands such as tabs, ttytype, tput, etc). The same must be done for .profile (and perhaps .kshrc, .cshrc, etc).

By protecting the interactive commands (if interactive then...) jobs like cron and other scripts can change user and run without pausing or getting the ubiquitous error message: not a typewriter.


Bill Hassell, sysadmin
Donny Jekels
Respected Contributor

Re: Automatic response in shell script

if you just want to say yes or no to your program then pipe it through yes or no.

command | yes or command | no
"Vision, is the art of seeing the invisible"