2016-01-24 30 views
-3

我發現,而我試圖添加一個函數,如果命令,我可能不明白這些命令的實際功能。我批處理if for句子

了我所要實現的:我試着告訴腳本,「如果」(變量)等於(本),然後它會「SET」(這個變量),以0

我如何試圖acomplish這: 我試圖通過寫acomplish這樣的:IF %Q%==/decrypt_Delhi-Inter-Cafe-Guest set decrypt_Delhi-Inter-Cafe-Guest=0

,正如你可以看到... ...我aparently不明白我在這裏做了哈哈挺什麼

爲什麼我會想要做這樣的事?

好吧,我主要是玩弄幾件事情,爲了我做任何事情。我需要寫一些可以做到這一點的東西(類似於waY)

繼承人腳本本身(我刪除了任何不必要的代碼行,這些代碼之間的菜單和我刪除的很多其他東西,這是唯一的部分需要固定) (對不起我的無能,我這裏學習):PI試着google了一下,讀了很多,但我沒有運氣黯然

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% 

編輯

我注意到theres仍然是語法錯誤。所以我決定檢查一下,如果我的代碼中的某些句子出現語法錯誤,可能是原因。所以我讓他們都轉到:error1標籤。免除我想使用的那個。

和輸出如果我寫入:/decrypt_Delhi-Inter-Cafe-Guest 是:錯誤,這意味着一些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 

回答

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 
) 
+0

謝謝!該網站是真的寫得很好,很容易理解:然而,正如你所說,如果/我「你好」equ「你好」(回聲嘿那裏。)其他(轉到測試)這將是使用它的核心方式? –

+0

首先,在註釋中,您可以使用backQuote(「shift」+「〜」)圍繞某個命令來突出顯示「像這樣」。我會在對ya的編輯中添加一個響應。 – Bloodied

+0

我仍然無法獲得這部分工作:'如果公式「/ decrypt_Delhi-間咖啡館,遊客的」設置「decrypt_Delhi-間咖啡廳,客戶= 1」」'它提供了一個語法錯誤....我試着做這個轉變和按鈕你說,但沒有解決:P它只讓我這個符號*^* –

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 

在第一行中, 「引用字符串」 確保了第一串是非空(如果您只是回覆輸入,並且如果回覆字符串包括分隔符如空間然後「引用字符串」將單獨的子字符串形成單個字符串。

/i版本使得匹配不區分大小寫。

在第二行中,已省略了 S和因此if命令字符串encryption_Delhi-Inter_Cafe-Guest針對0,而不是可變encryption_Delhi-Inter_Cafe-Guest進行比較。此外,您已使用=,其中==是必需的。

再次,我已經安裝了「引號」,因爲encryption_Delhi-Inter_Cafe-Guest可能不能設置,在這情況下你原來的命令(如果你使用的 S)將成爲

if ==0 goto ... 

隨着行情的命令變爲

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

(這將計算爲 b如果以前的版本會產生語法錯誤)

同時,請考慮減少變量和所需輸入的長度。在這麼長的名字一個簡單的換位是很難被發現 - 特別是如果你熟悉所涉及的名字 - 例如(從您的文章 - 一點點強調

echo you successfully decrypted __dh__eli guest network


在修改代碼:

你堅持用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

+0

嘿。感謝您的幫助,我想我發現爲什麼語法錯誤是順便說一句。請檢查編輯。但是我不知道爲什麼這些句子會干擾我指定使用的句子。 –

+0

錯誤輸出來自:錯誤部分。檢查代碼中發生了什麼,我只是簡單地將每一行都轉到該字段:錯誤,以便查看其他一些行是否會干擾當前要使用的一行。它正在做。這不是一個物理錯誤。但它跳到錯誤的標籤(這是標記爲:錯誤) –

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 /?
+0

反正。感謝您的幫助。 :) –