1833006 Members
3394 Online
110048 Solutions
New Discussion

Script question

 
SOLVED
Go to solution
Rob Johnson_3
Regular Advisor

Script question


How do I work with a command in a script that prompts for a Yes or No?

This is the command that waits for user input-
johnsonr@nmsccm01 > /opt/CSCOpx/campus/bin/reinitdb.pl
This will erase all data from the database. Are you sure [y/n] ?

I want to answer y in the script below. How can I accomplish this?
****************************************
#!/bin/sh

#set date variables
my_yr=`date +%y`
my_mon=`date +%b`
my_day=`date +%d`
my_hr=`date +%H`
my_min=`date +M`

my_date="$my_yr$my_mon$my_day"

#make backup of the ANIServer.properties file
cp /opt/CSCOpx/campus/etc/cwsi/ANIServer.properties /opt/CSCOpx/campus/etc/cwsi/ANIServer.properties.23May07

#copy new ANIServer.properties file
cp /tmp/ANIServer.new /opt/CSCOpx/campus/etc/cwsi/ANIServer.properties

#Reinitialize the ANI Database
/opt/CSCOpx/campus/bin/reinitdb.pl
***********************************************
3 REPLIES 3
Steven E. Protter
Exalted Contributor
Solution

Re: Script question

Shalom

snippet

echo "Do you want to proceed y/n "
read proceed

if [ "$proceed" == "y" ]
then
echo "delete data
# delete data
else
echo "doing nothing, have a nice day"
fi

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Hemmetter
Esteemed Contributor

Re: Script question

Hi Rob,

two ways:
first:
# echo y | /opt/CSCOpx/campus/bin/reinitdb.pl

second:
# /opt/CSCOpx/campus/bin/reinitdb.pl <y
EOC


Both give "y" to the program so it can continue.

rgds
HGH




Dennis Handly
Acclaimed Contributor

Re: Script question

In regards to both SEP's and HGH's solutions, you may want to check to see if the script is interactive, and then not ask.

You can use tty(1):
$ if tty -s; then echo it is a tty; fi