英文:
How to set a variable in CMD?
问题
我有以下命令来将我的IP地址设置到一个变量中:
for /F %I in ('curl http://ipecho.net/plain') do set ip=%I
当我打开命令提示符并手动输入(或粘贴)这个命令时,它正常工作。变量 %ip% 被设置,并且以下命令
echo %ip%
返回我的IP地址。
然而,如果我将这个命令放入一个cmd文件中并运行该cmd文件,我会收到以下错误消息:
//ipecho.net/plain') was unexpected.
我做错了什么?
英文:
I have the following command to set my own IP in a variable :
for /F %I in ('curl http://ipecho.net/plain') do set ip=%I
When I open cmd and write (or paste) the command manually, it's working properly. The variable %ip% is set and the command
echo %ip%
returns my IP address.
However if I put this command in a cmd file and run the cmd file, I'm getting the error :
//ipecho.net/plain') was unexpected.
What am I doing wrong?
答案1
得分: 4
请将以下部分翻译为中文:
"就像帮助文档中所说(对于 /?)
要在批处理程序中使用 FOR 命令,请使用 %%variable 代替 %variable。变量名称区分大小写,因此 %i 与 %I 不同。
因此,请将每个 %I 替换为 %%I。
虽然有点烦人,但就是这样。"
英文:
Like it says in the help (for /?)
> To use the FOR command in a batch program, specify %%variable instead
> of %variable. Variable names are case sensitive, so %i is different
> from %I.
So, replace each %I by %%I.
Annoying, but there it is.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论