perforce命令到命令行变量的结果 [英] Results of perforce command to command line variable

查看:93
本文介绍了perforce命令到命令行变量的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理批处理文件,并且需要调用Perforce命令来获取一些用户信息.命令是:

I'm working within a batch file and I have a Perforce command to call in order to gain some user information. The command is:

p4 -Ztag -F %clientName% info

现在我需要将该命令的结果保存在变量或文件中(最好是前者).但是对于我的一生,我不知道该怎么做,有人可以帮我吗.

Now what I need is for the result of this command to be held in either a variable or a file (ideally the former). But for the life of me I can't figure out how to do so, can someone please help me out.

我尝试了以下命令:

p4 -Ztag -F %clientName% info > %HOMEPATH%\clientName.txt

但是结果是:

p4 -Ztag -F  info  1>\Users\UserName\clientName.txt

文件"clientName.txt"包含:

With the file "clientName.txt" containing:

info

这是不正确的.

推荐答案

您始终需要在批处理文件中转义百分比-命令行将忽略未定义的环境变量,但是脚本会将它们扩展为空字符串.由于这些百分比用于p4命令的变量扩展,因此您从不都希望外壳程序触摸它们,而不管是否定义了本地环境变量.

You always need to escape percents in a batch file -- the command line will ignore undefined environment variables, but a script will expand them into an empty string. Since these percents are for the p4 command's variable expansion you never want the shell to touch them, regardless of whether there's a local environment variable defined.

旋转一下:

p4 -Ztag -F %%clientName%% info>CLIENTNAME
set /p CLIENTNAME=<CLIENTNAME

功劳归功于:

  • Escape percent in bat file
  • Batch equivalent of Bash backticks

(第一行将值放入名为CLIENTNAME的文件中,第二行从文件中将其读取到同名的env var中.由于某些原因,set /p不适用于仅像您这样的直接管道会认为的;我放弃了调试那个.)

(The first line puts the value into a file called CLIENTNAME, the second line reads it from the file into an env var of the same name. For some reason set /p doesn't work with just a direct pipe like you'd think it would; I gave up on debugging that one.)

作为附带说明,如果要将其存储在环境变量中,并希望将其传递给从其他配方中获得的另一个p4命令,请注意,这可能是多余的,因为大多数在p4上运行的p4命令客户端将默认为p4 info给您的客户端. :)与Windows env vars相比,在p4命令之间传递信息的方法也更简单.

As a side note, if you're storing this in an environment variable with the idea of passing it to another p4 command that you got from some other recipe, note that this may be redundant as most p4 commands that operate on a client will already default to the same client that p4 info gave you. :) There are also easier ways of passing information between p4 commands than Windows env vars.

这篇关于perforce命令到命令行变量的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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