Operating System - Linux
1748245 Members
3656 Online
108760 Solutions
New Discussion юеВ

Re: How to by pass a prompt in a script.

 
SOLVED
Go to solution
Reynaldo Torres
Advisor

How to by pass a prompt in a script.

I am putting part of the .profile where is gets prompted to enter the instance you want to work with, and is as follow:

echo "Enter an environment TST,DEV,PRD,DMO, or PERF:"
read ENV
#
case $ENV in
TST|1)
echo "You entered (TST) :\n";
export MYBAN=TEST
export PSADM=/u04/psoft/CRM89TST/pt845/crm89/appserv
export PS_HOME=/u04/psoft/CRM89TST/pt845/crm89
. /u04/psoft/CRM89TST/pt845/crm89/psconfig.sh
;;
DEV|2)
echo "You entered (DEV) :\n";
export MYBAN=DEVELOP
export PSADM=/u04/psoft/CRM89DEV/pt845/crm89/appserv
export PS_HOME=/u04/psoft/CRM89DEV/pt845/crm89
. /u04/psoft/CRM89DEV/pt845/crm89/psconfig.sh
;;
Since we want to shutdown PeopleSoft using a cron job, we do not want it to be prompted and continue with the shutdown as normal. Since we have 5 or 6 different environment the user needs to put what environment will be working with and it gets re-directed to his choice. If you could help on what do I need to put in order to by pass the prompt, I would really appreciated.

Thank you so much!

** removed email address to comply with forum guidelines **
Reynaldo Torres
4 REPLIES 4
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How to by pass a prompt in a script.

Perhaps the easiest method is to pass a value on the command line; e.g. let $1 = ENV
Essentially we set ENV = "" and then assign it to ${1}. If the length of ${ENV} is zero then you need to prompt the user otherwise enter the case statement.


Something like this:

#!/usr/bin/sh

typeset ENV=""
if [[ ${#} -gt 0 ]]
then
ENV=${1}
shift
fi
if [[ -z "${ENV}" ]]
then
echo "Enter an environment TST,DEV,PRD,DMO, or PERF: \c"
read ENV
fi
case ${ENV} in
.... the rest is the same as your existing script
If it ain't broke, I can fix that.
Matti_Kurkela
Honored Contributor

Re: How to by pass a prompt in a script.

It would be best to test whether a tty is available before trying to ask things from user in .profile:

if tty -s
then
# we have a TTY and can ask things
echo "Enter an environment:"
read ENV
#...
else
# we do not have a TTY.
# Don't ask anything, because the prompt
# might break things like rcp/scp/rdist/
# rsync or cron jobs.
# Set the environment variables to some
# sensible default values.
fi
MK
Dennis Handly
Acclaimed Contributor

Re: How to bypass a prompt in a script.

You can also use the following to check to see if you are an interactive shell:
if [ "${-%%*i*}" != "$-" ]; then

But perhaps Matti's solution would be better in scripts, where mine better for .profile.

 

Also, you should NOT be using ENV for a variable because ENV has a special meaning for a real shell!

Arturo Galbiati
Esteemed Contributor

Re: How to by pass a prompt in a script.

Hi,
just another possbile test:
if [[ $(tty) != "not a tty" ]]; then
# BEGIN interactive section
# END intercative sectin

put here all yoy command which requires
terminal (i.e.: terminal setup) or
interaction with user
else
# BEGIN batch section
# END batch sectin
fi
HTH,
Art