“.bat”파일에서 명령 줄 매개 변수를 확인하는 방법은 무엇입니까?
내 OS는 Windows Vista입니다. 사용자가 명령 줄 매개 변수를 입력했는지 확인해야하는 ".bat"파일이 필요합니다. 그렇다면 매개 변수가 다음과 같으면 -b
다른 작업을 수행합니다. "잘못된 입력"플래그를 지정합니다. 사용자가 명령 줄 매개 변수를 입력하지 않으면 내가 뭔가를 할 것입니다. 다음 .bat 파일을 만들었습니다. 케이스 -b
와 동일하지 않지만 작동 -b
하지만 사용자가 명령 줄 매개 변수를 전달하지 않으면 실패합니다.
항상 오류가 발생합니다.
GOTO was unexpected at this time.
아무도 내가 여기서 뭘 잘못하고 있는지 말해 줄 수 있습니까?
ECHO OFF
CLS
ECHO.
IF [%1]==[/?] GOTO BLANK
IF %1=="-b" GOTO SPECIFIC
IF NOT %1=="-b" GOTO UNKNOWN
:SPECIFIC
ECHO SPECIFIC
GOTO DONE
:BLANK
ECHO No Parameter
GOTO DONE
:UNKNOWN
ECHO Unknown Option
GOTO DONE
:DONE
ECHO Done!
매개 변수가 비어 있는지 확인해야합니다. if "%~1"=="" goto blank
이 작업을 마치면 -b에서 if / else 스위치를 수행합니다. if "%~1"=="-b" (goto specific) else goto unknown
매개 변수를 따옴표로 묶으면 공백 / 빈 / 누락 된 매개 변수와 같은 항목을 더 쉽게 확인할 수 있습니다. "~"는 큰 따옴표가 명령 줄 인수에있는 경우 제거되도록합니다.
답변 은 http://ss64.com/nt/if.html 을 참조하십시오 . 명령이 IF [%1]==[] GOTO NO_ARGUMENT
유사합니다.
짧은 대답-대괄호 사용 :
if [%1]==[] goto :blank
또는 (따옴표 붙은 인수를 처리해야하는 경우 아래 편집을 참조하십시오) :
if [%~1]==[] goto :blank
왜? 당신은 물어볼 수 있습니다. Jeremiah Willcock이 언급했듯이 http://ss64.com/nt/if.html- 그들은 그것을 사용합니다! 좋아요,하지만 따옴표에 무슨 문제가 있습니까?
다시 한 번, 짧은 대답은 "마 법적"입니다. 때로는 큰 따옴표 (큰 따옴표)가 작은 따옴표 (큰 따옴표)로 변환되기도합니다. 그리고 그들은 시작을 위해 일치해야합니다.
이 작은 스크립트를 고려하십시오.
@rem argq.bat
@echo off
:loop
if "%1"=="" goto :done
echo %1
shift
goto :loop
:done
echo Done.
테스트 해 보겠습니다.
C:\> argq bla bla
bla
bla
Done.
Seems to work. But now, lets switch to second gear:
C:\> argq "bla bla"
bla""=="" was unexpected at this time.
Boom This didn't evaluate to true, neither did it evaluate to false. The script DIED. If you were supposed to turn off the reactor somewhere down the line, well - tough luck. You now will die like Harry Daghlian.
You may think - OK, the arguments can't contain quotes. If they do, this happens. Wrong Here's some consolation:
C:\> argq ""bla bla""
""bla
bla""
Done.
Oh yeah. Don't worry - sometimes this will work.
Let's try another script:
@rem args.bat
@echo off
:loop
if [%1]==[] goto :done
echo %1
shift
goto :loop
:done
echo Done.
You can test yourself, that it works OK for the above cases. This is logical - quotes have nothing to do with brackets, so there's no magic here. But what about spicing the args up with brackets?
D:\>args ]bla bla[
]bla
bla[
Done.
D:\>args [bla bla]
[bla
bla]
Done.
No luck there. The brackets just can't choke cmd.exe
's parser.
Let's go back to the evil quotes for a moment. The problem was there, when the argument ended with a quote:
D:\>argq "bla1 bla2"
bla2""=="" was unexpected at this time.
What if I pass just:
D:\>argq bla2"
The syntax of the command is incorrect.
The script won't run at all. Same for args.bat
:
D:\>args bla2"
The syntax of the command is incorrect.
But what do I get, when the number of "
-characters "matches" (i.e. - is even), in such a case:
D:\>args bla2" "bla3
bla2" "bla3
Done.
NICE - I hope you learned something about how .bat
files split their command line arguments (HINT: *It's not exactly like in bash). The above argument contains a space. But the quotes are not stripped automatically.
And argq? How does it react to that? Predictably:
D:\>argq bla2" "bla3
"bla3"=="" was unexpected at this time.
So - think before you say: "Know what? Just use quotes. [Because, to me, this looks nicer]".
Edit
Recently, there were comments about this answer - well, sqare brackets "can't handle" passing quoted arguments and treating them just as if they weren't quoted.
The syntax:
if "%~1"=="" (...)
Is not some newly found virtue of the double quotes, but a display of a neat feature of stripping quotes from the argument variable, if the first and last character is a double quote.
This "technology" works just as well with square brackets:
if [%~1]==[] (...)
It was a useful thing to point this out, so I also upvote the new answer.
Finally, double quote fans, does an argument of the form ""
exist in your book, or is it blank? Just askin' ;)
In addition to the other answers, which I subscribe, you may consider using the /I
switch of the IF
command.
... the /I switch, if specified, says to do case insensitive string compares.
it may be of help if you want to give case insensitive flexibility to your users to specify the parameters.
IF /I "%1"=="-b" GOTO SPECIFIC
You are comparing strings. If an arguments are omitted, %1
expands to a blank so the commands become IF =="-b" GOTO SPECIFIC
for example (which is a syntax error). Wrap your strings in quotes (or square brackets).
REM this is ok
IF [%1]==[/?] GOTO BLANK
REM I'd recommend using quotes exclusively
IF "%1"=="-b" GOTO SPECIFIC
IF NOT "%1"=="-b" GOTO UNKNOWN
Actually, all the other answers have flaws. The most reliable way is:
IF "%~1"=="-b" (GOTO SPECIFIC) ELSE (GOTO UNKNOWN)
Detailed Explanation:
Using "%1"=="-b"
will flat out crash if passing argument with spaces and quotes. This is the least reliable method.
IF "%1"=="-b" (GOTO SPECIFIC) ELSE (GOTO UNKNOWN)
C:\> run.bat "a b"
b""=="-b" was unexpected at this time.
Using [%1]==[-b]
is better because it will not crash with spaces and quotes, but it will not match if the argument is surrounded by quotes.
IF [%1]==[-b] (GOTO SPECIFIC) ELSE (GOTO UNKNOWN)
C:\> run.bat "-b"
(does not match, and jumps to UNKNOWN instead of SPECIFIC)
Using "%~1"=="-b"
is the most reliable. %~1
will strip off surrounding quotes if they exist. So it works with and without quotes, and also with no args.
IF "%~1"=="-b" (GOTO SPECIFIC) ELSE (GOTO UNKNOWN)
C:\> run.bat
C:\> run.bat -b
C:\> run.bat "-b"
C:\> run.bat "a b"
(all of the above tests work correctly)
참고URL : https://stackoverflow.com/questions/4953170/how-to-check-command-line-parameter-in-bat-file
'program story' 카테고리의 다른 글
과거에 두 개의 임의 커밋 사이에 커밋을 삽입하는 방법은 무엇입니까? (0) | 2020.08.30 |
---|---|
마우스 이벤트없이 jQuery에서 마우스 위치를 얻는 방법은 무엇입니까? (0) | 2020.08.30 |
문서 Main.storyboard에는 Xcode 8.0 이상이 필요합니다. (0) | 2020.08.29 |
.NET은 바이트를 KB, MB, GB 등으로 쉽게 변환하는 방법을 제공합니까? (0) | 2020.08.29 |
MySQL을 사용하여 임의의 고유 한 8 자 문자열 생성 (0) | 2020.08.29 |