Windows批处理忽略if else

时间:2017-05-04 19:19:58

标签: windows batch-file

我的批处理脚本似乎跳过我的if else部分......或者它可能只是死了......我不知道......

为什么会这样?

@echo on
echo Welche URL soll in Chrome geoeffnet werden? (Enter = lamp.dev)
echo Die URL bitte ohne http/https
echo.
set /p url=URL: 
pause
IF %url="" (
    "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --ignore-certificate-errors --unsafely-treat-insecure-origin-as-secure=https://lamp.dev --new-window "https://lamp.dev"
)ELSE(
    "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --ignore-certificate-errors --unsafely-treat-insecure-origin-as-secure=https://%url% --new-window "https://%url%"
)
pause

3 个答案:

答案 0 :(得分:2)

我会这样简化:

@echo off
echo Welche URL soll in Chrome geoeffnet werden? (Enter = lamp.dev)
echo Die URL bitte ohne http/https
echo.
set /p url=URL: 
pause
IF NOT DEFINED url SET "url=lamp.dev"
"%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe" --ignore-certificate-errors --unsafely-treat-insecure-origin-as-secure=https://%url% --new-window "https://%url%"
pause

答案 1 :(得分:0)

通过将变量括在%中来引用变量。而不是%url=""使用"%url%"==""

此外,您应该用引号括起set /p命令以显示末尾的空格,并使用if not defined url检查是否定义了变量。

@echo off
echo Welche URL soll in Chrome geoeffnet werden? (Enter = lamp.dev)
echo Die URL bitte ohne http/https
echo.
set /p "url=URL: "
if not defined url (
    "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --ignore-certificate-errors --unsafely-treat-insecure-origin-as-secure=https://lamp.dev --new-window "https://lamp.dev"
) else (
    "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --ignore-certificate-errors --unsafely-treat-insecure-origin-as-secure=https://%url% --new-window "https://%url%"
)

答案 2 :(得分:0)

对于if语句,因为它是一个字符串变量,所以需要将if括在括号中。您还忘了添加结束%

set /p url=URL: 
pause
IF "%url%"="" (
    "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --ignore-certificate-errors --unsafely-treat-insecure-origin-as-secure=https://lamp.dev --new-window "https://lamp.dev"
)ELSE(
    "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --ignore-certificate-errors --unsafely-treat-insecure-origin-as-secure=https://%url% --new-window "https://%url%"
)
pause

希望这有帮助