我批评if,for,做句子

时间:2016-01-24 00:45:43

标签: batch-file if-statement

我发现当我尝试使用if命令添加一个函数时,我可能不了解theese命令的实际功能。

我试图实现的目标:我试图告诉脚本" if" (变量)等于(this)然后它将" SET" (此变量)为0

我是如何尝试这样做的: 我试着写一下:IF %Q%==/decrypt_Delhi-Inter-Cafe-Guest set decrypt_Delhi-Inter-Cafe-Guest=0

正如你所看到的......我显然不明白我在这里做什么哈哈

为什么我要做这样的事情?

好吧,我大部分时间都在玩一些东西,为了我的工作而努力。我需要写一些可以做到这一点的东西(有点相同)

继承脚本本身(我删除了任何不必要的行。在这些代码之间有一个菜单和我删除的很多其他东西。这是唯一需要修复的部分。) (对不起我的无能。我在这里学习):P我试着去谷歌并阅读了很多,但我没有运气可悲

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%

修改

我注意到仍有语法错误。所以我决定检查代码中的一些if语句是否得到语法错误可能是原因。所以我让他们都转到:error1标签。解雇了我想要使用的那个。

和我输入的输出:/decrypt_Delhi-Inter-Cafe-Guest 是:ERROR意味着一些IF句子正在干扰。并使它转到错误的标签。但我不明白为什么,

: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

3 个答案:

答案 0 :(得分:0)

在我输入任何答案之前,请尝试SS64。一个非常有用的网站,有直接的解释和一些例子。

您可以通过多种方式编写if命令。对于您的情况,成功的命令很短,并且不需要跨行分割。

然而,引用if检查的两面,甚至是set命令都是明智之举。您的==有效,但我总是更喜欢使用equ,因为它不受限于字符串比较。这是我的意思的一个例子:

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

如果您想让自己更轻松,可以使if检查对/i不区分大小写,如下所示:

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

这只会在当然给出/i标记的情况下匹配。

编辑: 提问者提示:

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

将起作用。 (尽管您应该使用goto :test代替goto test) 您可以拆分此命令以便于阅读/编辑,例如:

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
)

答案 1 :(得分:0)

您尚未告诉我们您提供了哪些输入,因此我们必须假设并猜测。

您的代码:

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

应该是

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

或者最好

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

在第一行中,“引用字符串”确保第一个字符串非空(如果您只是回复 Enter ,并且如果回复字符串包含 Space <之类的分隔符/ kbd>然后“引用字符串”将单独的子字符串形成一个字符串。

/i版本使匹配不区分大小写。

在第二行中,您省略了,因此if命令将字符串encryption_Delhi-Inter_Cafe-Guest0进行比较,而不是变量 encryption_Delhi-Inter_Cafe-Guest。此外,您还需要=,其中==是必需的。

同样,我已经安装了“引号”,因为encryption_Delhi-Inter_Cafe-Guest 可能未设置,在这种情况下您的原始命令(如果您使用了 s)将成为

if ==0 goto ...

使用引号,命令变为

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

(这将评估为 false ,但先前版本将产生语法错误)

同时,请考虑减少变量的长度和所需的输入。如此长的名字的简单转换可能很难发现 - 特别是如果你熟悉所涉及的名字 - 例如(从你的帖子 - 稍加强调)

  

回复你成功解密__dh__eli访客网络

修改后的代码:

你坚持使用IF %Q%==/ipconfig ...形式,尽管我已经说过使用wuoted字符串。在这种情况下,它不太重要。

使用输入/decrypt_Delhi-Inter-Cafe-Guest运行此代码对我没有任何回复,而不是您报告的error(我们需要整个错误报告,而不只是一个字。)

也许问题是你接受了一个名为Q的变量的输入,然后测试Q的字符串,并在输入字符串匹配时将名为decrypt_Delhi-Inter-Cafe-Guest的变量设置为零。

您的下一个语句会测试encryption_Delhi-Inter_Cafe-Guest是否为零。也许您应该测试decrypt_Delhi-Inter-Cafe-Guest

答案 2 :(得分:0)

我建议将此代码与备注(评论)一起使用:

@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

我不确定你为什么要分配

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

0变量

decrypt_Delhi-Inter-Cafe-Guest

并在下一行测试不同的变量

encryption_Delhi-Inter_Cafe-Guest

0。在我看来,这没有多大意义。

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • cls /?
  • echo /?
  • goto /?
  • if /?
  • pause /?
  • ping /?
  • rem /?
  • set /?
  • setlocal /?