当前位置: 动力学知识库 > 问答 > 编程问答 >

if statement - I batch if, for, do sentences

问题描述:

I discovered while I tried to add a function with if command that I probably don't understand the real function of theese commands.

what I tried to achieve: I tried to tell the script that "if" (variable) equals to (this) then it will "SET" (this variable) to 0

how I tried to acomplish this:

i tried to acomplish this by writing: IF %Q%==/decrypt_Delhi-Inter-Cafe-Guest set decrypt_Delhi-Inter-Cafe-Guest=0

and as you can see... I aparently don't understand quite what I'm doing here haha

why would i want to do something like this?

well I'm mostly playing around with some few things and in order for whatever I do to work. I need to write something that can do this (sort of same waY)

heres the script itself (i removed any unneccecary lines. between those codes theres an menu and a lot of other stuff I removed. this is the only part that needs fixing.)

(sorry for my incompetence. I'm here to learn) :P I tried to google a bit and read a lot but I had no luck sadly

set encryption_Delhi-Inter_Cafe-Guest=1

set /P Q=Console:

IF %Q%==/decrypt_Delhi-Inter-Cafe-Guest set decrypt_Delhi-Inter-Cafe-Guest=0

if encryption_Delhi-Inter_Cafe-Guest=0 goto decryptedsuccess

:decryptedsuccess

echo you successfully decrypted dheli guest network

echo encryption value: %encryption_Delhi-Inter_Cafe-Guest%

edit

I noticed theres still syntax error. so I decided to check if some of the if sentences under the code I get syntax error with could be the cause. so I made them all goto an :error1 label. exept the one I want to use.

and the output if I write in : /decrypt_Delhi-Inter-Cafe-Guest

is: ERROR which means some of the IF sentences are interfering. and making it goto wrong label. but I cannot see why,

:decrpt

set encryption_Arris-DE02=1

set encryption_The-G-Strings=1

set encryption_Delhi-Inter_Cafe-Guest=1

set encryption_Delhi-Inter_Cafe-Priv=1

set /P Q=Console:

IF /i "%Q%"=="/decrypt_Delhi-Inter-Cafe-Guest" set decrypt_Delhi-Inter-Cafe-Guest=0

if "%encryption_Delhi-Inter_Cafe-Guest%"=="0" goto decryptedsuccess

IF %Q%==/cmd.help call :ERROR1

IF %Q%==/scan.networks goto networkscan

IF %Q%==/ipconfig goto :ERROR1

IF %Q%==cls goto cls

IF %Q%==/decrypt goto :ERROR1

IF %Q%==/decrypt_Arris-DE02 goto :ERROR1 "REM do (set encryption=0) do (goto decryption complete)"

IF %Q%==/decrypt_The-G-Strings goto :ERROR1

IF %Q%==/decrypt_Delhi-Inter_cafe-Priv goto :ERROR

网友答案:

Before I even type any sort of answer, try SS64. A very useful site with direct explanations and a few examples.

You can write if commands in many ways. For your case, the command on success is short and doesn't need to be split across lines.

However it's wise to quote both sides of the if check, and even the set command. Your == is valid, but I always prefer using equ over it, as it isn't constrained to string comparisons. Here's an example of what I mean:

if "%Q%" equ "/decrypt_Delhi-Inter-Cafe-Guest" set "decrypt_Delhi-Inter-Cafe-Guest=0"

If you want to make it easier on yourself, you can make it so the if check is case insensitive with /i, like this:

if /i "HELLO" equ "hello" echo Hey there.

This will only match if the /i tag is given of course.


EDIT: On asker prompt:

if /i "HELLO" equ "hello" (echo Hey there. ) else (goto test)

will function. (although you should use goto :test instead of goto test) You can split this command to make it easier to read/edit, example:

if /i "HELLO" equ "hello" (
    echo Hey there.
    goto :test_1
) else (
    echo Whoah this should never appear with the /i tag . . .
    pause
    exit /b 0
)
网友答案:

You haven't told us what input you provided, so we have to assume and guess.

Your code :

IF %Q%==/decrypt_Delhi-Inter-Cafe-Guest set decrypt_Delhi-Inter-Cafe-Guest=0
if encryption_Delhi-Inter_Cafe-Guest=0 goto decryptedsuccess

should be

IF "%Q%"=="/decrypt_Delhi-Inter-Cafe-Guest" set decrypt_Delhi-Inter-Cafe-Guest=0
if "%encryption_Delhi-Inter_Cafe-Guest%"=="0" goto decryptedsuccess

or preferably

IF /i "%Q%"=="/decrypt_Delhi-Inter-Cafe-Guest" set decrypt_Delhi-Inter-Cafe-Guest=0
if "%encryption_Delhi-Inter_Cafe-Guest%"=="0" goto decryptedsuccess

In the first line, "quoting the string" ensures that the first string is non-empty (if you simply reply Enter and that if the reply-string includes separators like Space then "quoting the string" forms the separate substrings into a single string.

The /i version makes the match case-insensitive.

In the second line, you have omitted the %s and hence the if command compares the string encryption_Delhi-Inter_Cafe-Guest against 0, not the variable encryption_Delhi-Inter_Cafe-Guest. Also, you've use = where == is required.

Again, I've installed "quotes" since encryption_Delhi-Inter_Cafe-Guest may not be set, under which circumstance your original command (if you'd used the %s) would become

if ==0 goto ...

With the quotes, the command becomes

if ""=="0" goto ...

(this will evaluate to false but the prior version will yield a syntax error)

Meanwhile, please consider reducing the length of your variables and of your required input. A simple transposition in such long names can be hard to spot - especially if you are familiar with the names involved - for example (from your post - with a little emphasis

echo you successfully decrypted __dh__eli guest network


On amended code:

You persist in using the IF %Q%==/ipconfig ... form, despite what I have said about the use of wuoted strings. In this case, it's unlikely to matter.

Running this code with the input /decrypt_Delhi-Inter-Cafe-Guest produced no response for me, not the error you report (and we'd need the entire error report, not just one word.)

Perhaps the problem is that you are accepting the input into a variable named Q then testing Q for the string and setting a variable named decrypt_Delhi-Inter-Cafe-Guest to zero if the input string matches.

Your next statement tests whether encryption_Delhi-Inter_Cafe-Guest is zero. Perhaps you should be testing decrypt_Delhi-Inter-Cafe-Guest

网友答案:

I suggest using this code with remarks (comments):

@echo off
setlocal EnableDelayedExpansion

:decrypt
cls

rem Define environment variables with default values enclosed in
rem double quotes to ignore trailing whitespaces in batch file.

set "encryption_Arris-DE02=1"
set "encryption_The-G-Strings=1"
set "encryption_Delhi-Inter_Cafe-Guest=1"
set "encryption_Delhi-Inter_Cafe-Priv=1"

rem Define variable Q with a default value as when batch user just
rem hits key RETURN or ENTER without entering anything variable Q
rem still does not exist or if existing already at this time keeps
rem its current value. Then prompt batch user for the input string.

set "Q=no input"
set /P "Q=Console: "

rem For the case-insensitive string comparisons delayed expansion is
rem used to avoid an exit in batch processing because of a syntax error
rem if the batch user inputs for example an odd number of double quotes.

if /I "!Q!"=="/decrypt_Delhi-Inter-Cafe-Guest" set "encryption_Delhi-Inter_Cafe-Guest=0"
if "%encryption_Delhi-Inter_Cafe-Guest%"=="0" goto decryptedsuccess
if /I "!Q!"=="/cmd.help" goto :ERROR1
if /I "!Q!"=="/scan.networks" goto networkscan
if /I "!Q!"=="/ipconfig" goto :ERROR1
if /I "!Q!"=="cls" goto decrypt
if /I "!Q!"=="/decrypt" goto :ERROR1
if /I "!Q!"=="/decrypt_Arris-DE02" goto :ERROR1
if /I "!Q!"=="/decrypt_The-G-Strings" goto :ERROR1
if /I "!Q!"=="/decrypt_Delhi-Inter_cafe-Priv"  goto :ERROR1

rem The batch user entered none of the above strings. Inform the user
rem about the unknown command, halt batch execution until user presses
rem a key and then let the user enter the command once again.

echo Unknown command: !Q!
echo.
pause
goto decrypt

:decryptedsuccess
echo Decryption was successful.
echo.
pause
goto :EOF

:networkscan
echo Executing network scan.
rem Simulate activity by waiting 3 seconds.
%SystemRoot%\System32\ping.exe 127.0.0.1 -n 4 >nul
goto decrypt

:ERROR1
echo Variable Q=!Q!
echo.
pause

I'm not sure why you assign with

IF %Q%==/decrypt_Delhi-Inter-Cafe-Guest set decrypt_Delhi-Inter-Cafe-Guest=0

the value 0 to variable

decrypt_Delhi-Inter-Cafe-Guest

and test on next line the different variable

encryption_Delhi-Inter_Cafe-Guest

on value 0. This does not make much sense in my point of view.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • cls /?
  • echo /?
  • goto /?
  • if /?
  • pause /?
  • ping /?
  • rem /?
  • set /?
  • setlocal /?
分享给朋友:
您可能感兴趣的文章:
随机阅读: