0

I am just finding it hard to achieve below using for-loop. Your suggestions/help will be really appreciated. Thanks

  1. If user responds in upper or lower case then it should accept and convert in uppercase.
  2. When I try to provide another if-loop for "N" response it does not touch that loop instead it gives the error from 1st loop (ELSE condition) itself.

echo -n "IS THIS A PRODUCTION DATABASE. PLEASE RESPOND Y|N :- "
read RESPONSE
RSPN=$RESPONSE
if [ "${RSPN}" = "Y" ]
then
print "\n"
echo -n "PLEASE ENSURE A 7 YEARS RETENTION RMAN_FULL BACKUP TAKEN for $DBTD"
print "\n"
else
echo "ERROR: Cannot proceed as 7 Years backup not yet TAKEN for PRODUCTION database. Please take a 7 Years Retention backup and re-run the script again."
exit 1
print "\n"
fi
###############################################################
if [ "${RSPN}" = "N" ]
then
print "\n"
echo -n "PLEASE CHECK IF BACKUP IS REQUIRED BEFORE DECOMMISSIONING $DBTD
print "\n"
fi
###############################################################

output --->

IS THIS A PRODUCTION DATABASE. PLEASE RESPOND Y|N :- N
ERROR: Cannot proceed as 7 Years backup not yet TAKEN for PRODUCTION database. Please take a 7 Years Retention backup and re-run the script again.
  • 1
    You talk about loops, but there are no loops in your code. An if-statement is no loop. What shell is your code written for, ksh? – Kusalananda May 21 '18 at 12:16
  • 1
    Also tell us in what way you expect the script to work. – Kusalananda May 21 '18 at 12:22
  • My bad, My head must be heavy today. its not FORLOOP. Its all IF ELSE statements . I have mentioned my requirement, would be really grateful if you could help on it @Kusalananda .and YES its for KSH – Mohammeddba May 21 '18 at 12:36
  • 1
    You exit in the "else" branch if the response is not "Y", that is why the last echo does not produce any output. Is that the main issue or do you also need to handle upper and lowercase responses? – Kusalananda May 21 '18 at 12:41
  • That's the main issue, how can I make it to read the second section when issued RSPN=N and yeah the other as well (upper and lowercase response) @Kusalananda – Mohammeddba May 21 '18 at 12:59
2

The following /bin/sh script insists on getting a valid response from the user (it will ask again if the user does not provide a y or a n or a word that starts with one of those two letters, uppercase or lowercase).

while true; do
    printf 'IS THIS A PRODUCTION DATABASE? [y/n]: '
    read response

    case "$response" in
        [Yy]*) 
            printf 'PLEASE ENSURE A 7 YEARS RETENTION RMAN_FULL BACKUP TAKEN for %s\n' "$DBTD"
            break
            ;;
        [Nn]*)
            printf 'ERROR: Cannot proceed as 7 Years backup not yet TAKEN for PRODUCTION database. Please take a 7 Years Retention backup and re-run the script again.\n'
            printf 'PLEASE CHECK IF BACKUP IS REQUIRED BEFORE DECOMMISSIONING %s\n' "$DBTD"
            break
            ;;
        *) printf 'Invalid response "%s"\n' "$response"
    esac
done

If the user enters N in your code, it seems that you want both the error message and the "PLEASE CHECK" message to be printed. This is why I output both of these messages when the user enters a word starting with n.

If you want the "PLEASE CHECK" message to always be outputted, then just move that printf line down to the end of the script.

  • Thank you @Kusalananda . Its helpful but, Let me write down the flow chart which I want the complete script to do. – Mohammeddba May 21 '18 at 15:46
  • @Mohammeddba Yes, that would be helpful. – Kusalananda May 21 '18 at 15:49
  • Flow chart --> 1) 'IS THIS A PRODUCTION DATABASE? [y/n]: ' (if responded with Y then follow below question) if Y -- Then it should ask another question (HAVE YOU TAKEN A 7 YEARS RETENTION RMAN_FULL BACKUP for DPROXY11 - Y|N ) if N -- GOTO STEP 2 HAVE YOU TAKEN A 7 YEARS RETENTION RMAN_FULL BACKUP for DPROXY11 - Y|N if Y -- Then it should give message " Proceeding with DECOMMISSIONING. PLEASE WAIT.." and it should execute the commands. command 1 (gather database details) command 2 (drop database) command 3 (send email notifn) then terminate here. – Mohammeddba May 21 '18 at 16:14
  • if N -- Then it should terminate with a message "'ERROR: Cannot proceed as 7 Years backup not yet TAKEN for PRODUCTION database." 2) 'IS THIS A PRODUCTION DATABASE? [y/n]: '(if responded with N then follow below questions) if N -- Then it should give message "'PLEASE CHECK WITH USER IF BACKUP IS REQUIRED BEFORE DECOMMISSIONING FOR NON_PROD" then it should execute command 1 (gather database details) command 2 (drop database) command 3 (send email notification) – Mohammeddba May 21 '18 at 16:14
  • @Mohammeddba This is hard to read and to follow. You seem to ask whether it's a production database twice. Ideally, edit your question to include this information so that it's easier to read. – Kusalananda May 22 '18 at 10:41
0

You can do it the compatible way

if [ "${RSPN}" = "Y" ] || [ "${RSPN}" = "y" ]

or if you are using a recent bash anyway:

if [ "${RSPN^}" = "Y" ]
  • Thank you @Hauke Laging . The upper/lower case query is answered now. But, I am still looking for RSPN=N where it should go to the other section and execute the commands respective to RSPN=N. – Mohammeddba May 21 '18 at 14:32
  • @Mohammeddba I don't understand the problem. – Hauke Laging May 21 '18 at 14:43
  • @Hauke_Laging Thank you, I have got the answer which I was looking for. Thanks for your efforts and help on this one. Really appreciate it. – Mohammeddba May 21 '18 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.