1829598 Members
1644 Online
109992 Solutions
New Discussion

yes or no

 
SOLVED
Go to solution
P-Dicky
Frequent Advisor

yes or no

Good Morning All

I just need a simple piece of code. Here is what I am trying to do

I just want to prompt a user with "This will erase demo with no backup" Continue y or n.
y - results in a rm command
n - prompts

"Please have a backup run" and kicks them to a prompt

Any help would be great, sorry for the rookie question

Thanks
15 REPLIES 15
Jeff_Traigle
Honored Contributor

Re: yes or no

Assuming POSIX shell:

echo "This will erase demo with no backup. Continue? (y/[n]): \c"
read ANSWER

case ${ANSWER} in
y|Y) rm whatever
;;
*) echo "Please have a backup run"
;;
esac

This assumes a "no" response for anything other than y or Y.
--
Jeff Traigle
Coolmar
Esteemed Contributor

Re: yes or no

Hi,

I use something like the following:

echo "This will erase demo with no backup. Continue y or n"
read ans
if [ $ans == yes ]; then
rm command
else
prompts
fi
James R. Ferguson
Acclaimed Contributor

Re: yes or no

Hi:

You can adapt this to your needs:

#!/usr/bin/sh
typeset -l CHOICE
while true
do
clear
echo "\nThis will erase demo with no backup\n"
echo "\n >>> Enter < y > to remove"
echo "\n >>> Enter < n > to specify"
echo "\n*---> \c"
#
read CHOICE
case ${CHOICE} in
y ) echo "\nNow removing..."
echo "press ENTER to resume"
read REPLY
continue
;;
n ) exit
;;
"") continue
;;
esac
done
exit 0

Regards!

...JRF...
P-Dicky
Frequent Advisor

Re: yes or no

Jeff

It is not wrapping my text (I changed it a little. I am getting this

and:

How can I get it to split to the next line?
P-Dicky
Frequent Advisor

Re: yes or no

Here is what I have

cp /fpc/oracle/8.0.6/network/admin/listener.ora /drp/demofiles
cp /fpc/oracle/8.0.6/network/admin/tnsnames.ora /drp/demofiles
echo "This will erase prod 32 demo with no backup. It will also bring down test5
4 and prod40. Continue? (y/[n]:\c"
read ANSWER
case ${ANSWER} in
y|Y ps -ef | grep oracle | grep -v grep | awk '{print $2}' | xargs kill -9
cd /fpc
rm -rf *
;;
*) echo "Please have a backup run"
;;
esac
Sandman!
Honored Contributor

Re: yes or no

#!/usr/bin/sh

PS3='This will erase demo with no backup (Pick 1-3)? '
select i in "y" "n" "*"
do
case $i in
"y"|"Y") echo Will run the rm command
break
;;
"n"|"N") echo Please have a backup run!!!
exit
;;
"*") echo "Bye Bye..."
sleep 2
exit
;;
esac
done
OldSchool
Honored Contributor

Re: yes or no

"echo" doesn't understand how to wrap text, so you need to break it yourself.
Something like the following:

WAS:
echo "This will erase prod 32 demo with no backup. It will also bring down test5
4 and prod40. Continue? (y/[n]:\c"

TRY:
echo "This will erase prod 32 demo with no backup. It will also bring "
echo "down test54 and prod40. Continue? (y/[n]:\c"
OldSchool
Honored Contributor

Re: yes or no

or stick a \n anywhere you want a new-line

ie.

echo "abc\ndef"

will produce
abc
def
P-Dicky
Frequent Advisor

Re: yes or no

Good Morning,

So this is what I have so far

cp /fpc/oracle/8.0.6/network/admin/listener.ora /drp/demofiles
cp /fpc/oracle/8.0.6/network/admin/tnsnames.ora /drp/demofiles
echo "This will erase prod 32 demo with no backup. It will also bring down\n tes
t54 and prod40. Continue? (y/[n]): \c"
read ANSWER
if [$ANSWER ==y ]; then
ps -ef | grep oracle | grep -v grep | awk '{print $2}' | xargs kill -9
cd /fpc
rm -rf *
else
echo "Please have a backup run"
fi

And this is what it is doing

# ./s1.sh
This will erase prod 32 demo with no backup. It will also bring down
test54 and prod40. Continue? (y/[n]): y
./s1.sh[5]: [y: not found.
Please have a backup run
[root:demoglo:/drp]
#

Any tips would be great... Thanks
James R. Ferguson
Acclaimed Contributor
Solution

Re: yes or no

Hi:

You need whitespace around the brackets:

# if [ $ANSWER = "y" ]; then

You also need to use "=" since this is the shell's test for string equality.

*ALSO* do yourself a favor: NEVER use 'rm -rf' uness you are dead sure that you are removing what you think you are! In this script, at least, do:

# cd /fpc && rm -rf *

...in this way, *only* if the directory '/fpc' exists and you successfully change into it will the 'rm' be executed. The way you coded you script would mean a missing '/fpc' directory would leave your PATH wherever it was when you ran the script. If you were at the root directory as root, well, you just gave yourself a chance to practice a full server rebuild!

Regards!

...JRF...
Yogeeraj_1
Honored Contributor

Re: yes or no

hi,

instead of:
if [$ANSWER ==y ]; then


try:
if [ $ANSWER = "Y" -o $ANSWER = "y" ] ; then


hope this helps!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Yogeeraj_1
Honored Contributor

Re: yes or no

hi again,

as mentioned by JRF above, pay attention to the whitespaces.

you may wish to copy it from my last post and paste it in your script.

CAUTION!
========
above all, when testing your script, disable the actual command inside the scripts! only use echo commands

good luck

kind regards
yogeeraj

No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
P-Dicky
Frequent Advisor

Re: yes or no

Thank you.. I'll be back!

James R. Ferguson
Acclaimed Contributor

Re: yes or no

Hi (again):

Instead of testing for lowercase and uppercase letters, add 'typeset -l' declaration thusyly:

# typeset -l ANSWER
...
# read ANSWER
...

This automatically converts all uppercase letters to lowercase ones when the assignment is made. Hence you can respond in uppercase and test only lowercase letters.

Regards!

...JRF....
Mark Ellzey
Valued Contributor

Re: yes or no

Hello,

One other comment: using a kill -9 to shutdown a database is almost guaranteed to corrupt your database somewhere down the line.

If you read the man page on kill(1), you will see that the -9 switch means unconditional, cannot-be-ignored kill. This means that the process has not time to close files and clean up after itself.

Why not use the init.d script to shut down the DB in an orderly manner?

Just my 2cts,

Mark