如何在一个变量的主机名存储在一个.bat文件? [英] How to store the hostname in a variable in a .bat file?
问题描述
我想这个 / bin / sh的
语法转换成一种广泛兼容的Windows批处理脚本:
I would like to convert this /bin/sh
syntax into a widely compatible Windows batch script:
host=`hostname`
echo ${host}
如何做到这一点,以便它会在任何Windows Vista中,Windows XP和Windows 2000的工作机器?
How to do this so that it'll work on any Windows Vista, Windows XP, and Windows 2000 machine?
要澄清:那我想在节目中去,并使用主机名存储在变量主机
。换句话说,该方案的更大的目标不是简单地echo主机名
To clarify: I would then like to go on in the program and use the hostname as stored in the variable host
. In other words, the larger goal of the program is not to simply echo the hostname.
推荐答案
我平时阅读中使用,因为它省去了创建临时文件的的
命令变量命令的输出。例如:
I usually read command output in to variables using the FOR
command as it saves having to create temporary files. For example:
FOR /F "usebackq" %i IN (`hostname`) DO SET MYVAR=%i
请注意,上面的语句将在一个批处理文件运行在命令行上而不是。在批处理文件中使用它逃脱%
通过把他们两次的
语句:
Note, the above statement will work on the command line but not in a batch file. To use it in batch file escape the %
in the FOR
statement by putting them twice:
FOR /F "usebackq" %%i IN (`hostname`) DO SET MYVAR=%%i
ECHO %MYVAR%
还有很多与的
你可以做更多。欲了解更多详细信息,只是在命令提示符下键入的帮助
。
There's a lot more you can do with FOR
. For more details just type HELP FOR
at command prompt.
这篇关于如何在一个变量的主机名存储在一个.bat文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!