DOS下如何进行字符串比较条件?
哇,从来没想过我会在 DOS 中写任何东西。现在我知道了,我知道为什么我从来不想这样做。语法是荒谬的!
无论如何,我需要帮助。 我想提示用户输入,如果收到空行,我想使用默认值,如下所示:
set name=abraham.
set /p input=please enter your name, press enter to use %name%:
if not %input%=="" set name=%input%
echo your name is %name%
我收到一条错误消息“此时设置是意外的”。
你能帮忙吗?
Wow, never thought I would ever write anything in DOS. Now that I do, I know why I never wanted to. The syntax is absurd!
Anyways I need help please.
I would like to prompt the user for input, and if a blank line is received, I would like to use the default value, like this:
set name=abraham.
set /p input=please enter your name, press enter to use %name%:
if not %input%=="" set name=%input%
echo your name is %name%
I get an error says "set was unexpected at this time."
Can you help please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请
注意,在 cmd 文件中,如果未输入任何内容,则 var 不会更改。
或者,使用 if:
注意 if 语句中输入周围的引号,并注意我在运行之前清除输入(或者如果用户未输入任何内容,它将保留最后一个值)
Try
Note that in cmd files, if nothing is entered, the var is not changed.
Or, with the if:
Note the quotes around input in the if statement, and notice that I am clearing out input before running (or it will hold the last value if nothing is entered by the user)
空字符串在 shell 编程中实际上是空的,所以尝试
if "%input%"=="" set...
(带引号)或if %input%== set...
(空字符串为空)。Empty strings are actually empty in shell programming, so try
if "%input%"=="" set...
(with quotes) orif %input%== set...
(empty string is empty).我相信你需要在变量周围加上单引号(不确定是双引号还是单引号):
I believe you need to put single quotes (not sure if double or single matter) around the variable: