iT邦幫忙

0

DOS 指令有一行無法理解

v60i 2012-08-23 13:15:403784 瀏覽

@ECHO off
:start
ECHO 1. Print Hello
ECHO 2. Print Bye
set choice=
set /p choice=Type the number to print text.
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto hello
if '%choice%'=='2' goto bye
ECHO "%choice%" is not valid, try again
ECHO.
goto start
:hello
ECHO HELLO
goto end
:bye
ECHO BYE
goto end
:test
:end

請問為什麼它要寫
set choice=
這一段??
還有set choice=%choice:~0,1%
這是什麼意思

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

4
aesop
iT邦研究生 4 級 ‧ 2012-08-23 15:44:48
最佳解答

v60i提到:
@ECHO off
:start
ECHO 1. Print Hello
ECHO 2. Print Bye
set choice=
set /p choice=Type the number to print text.
if not '%choice%'=='' ...(恕刪)

查了一下HELP
應該是說
從最左邊起擷取1個字元
~0 是指從第0個字元算起
,1是指1個字元

所以當你把整段執行起來時
1 12 102 1010都會是hello 因為第一個都是1
2 22 212 2010都會是bye 因為第一個都是2

v60i iT邦新手 4 級 ‧ 2012-08-23 18:42:36 檢舉

1 12 102 1010
2 22 212 2010

這是怎麼看出來的?

aesop iT邦研究生 4 級 ‧ 2012-08-23 20:40:26 檢舉

~0,1
從最左邊起擷取1個字元

1 12 102 1010
2 22 212 2010
只是舉例

2
bayse
iT邦新手 5 級 ‧ 2012-08-23 14:26:58

set choice=
初始化設定choice為空白

set choice=%choice:~0,1%
沒記錯的話~ 好像是not

我要發表回答

立即登入回答