如何在一个变量的主机名存储在一个.bat文件? [英] How to store the hostname in a variable in a .bat file?

查看:215
本文介绍了如何在一个变量的主机名存储在一个.bat文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这个 / 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆