Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange behaviour when reading user input in command file

Status
Not open for further replies.

slicendice

Programmer
Jun 28, 2002
164
GB
Hi
I have a simple batch/command file that I'm using as the mechanism to run some Oracle SQL scripts on Win XP. The script starts off by requesting the user to input the password of an authorised Oracle account. Entering this password is not mandatory - it simply means that a user can run certain admin SQL scripts that they wouldn't otherwise be able to.

What I'm trying to do is present the user with an option if they enter a blank password. So if they just press Enter instead of entering a password when requested, they'll get a message saying "You will not be able to run admin options. Continue anyway? [Y/N]". They can then either enter Y and continue, or enter N and go back and enter a password.

The script I have at the moment is as follows:
Code:
:GetPPWD
set /p ppwd=Enter password: 
if /i '%ppwd%'=='q' goto End
if /i '%ppwd%'=='' (
  echo You will not be able to run admin options
  set /p continue=Continue anyway? [Y/N] 
  if /i '%continue%'=='Y' goto :Next
  goto :GetPPWD
)

:Next
etc...

This almost works perfectly! The issue is that if I enter no password (i.e. just press enter when password is requested) it asks the question as to whether I want to continue or not OK - but if I select Y (i.e. continue without entering password) then it still loops back and re-asks the question.

The strange thing is, it only does this once - i.e. if I enter no password and select Y to continue a second time, it then works OK and continues as it should.

Can anyone shed any light on why it's doing this?

Thanks very much
 
I think the problem is with your nested if's. The second if is inside the parentheses code of the first and either jumps beyond the closing parenthesis or back to before the opening one.

Rather than testing for the initial password being null, check if not null and if so jump to :Next. That way your second if won't be nested within the first.

Jock
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top