简体   繁体   English

如何将Windows批处理脚本变量传递给管道命令

[英]How to pass a windows batch script variable to a piped command

I have a batch script that generates a date stamped file name for creating a log file via php. 我有一个批处理脚本,该脚本会生成一个带日期戳的文件名,以便通过php创建日志文件。 That filename is stored in a windows batch file variable called logFile. 该文件名存储在Windows批处理文件变量logFile中。

I would like to reuse the variable in my piped tee command however it appears that the sub command created by the pipe doesn't inherit or have the variables from the parent cmd console in context. 我想在我的管道tee命令中重用该变量,但是似乎该管道创建的子命令不会继承上下文或具有来自父cmd控制台的变量。

Can anyone show how to resolve this issue without having to use temporary files or the creation of another batch script? 任何人都可以展示如何解决此问题而不必使用临时文件或创建另一个批处理脚本吗?

Thanks! 谢谢!

execJobs.bat execJobs.bat

@echo off

SET logFile=| php -r "$d = new DateTime(); $dateString = $d->format('Ymd-His');  
                     echo 'C:\\temp\\logs\\' . $dateString . '-exec.log';"

:: this is Ok
:: outputs c:\temp\logs\20150109-111031-exec.bat
%logFile%

:: This doesn't work
:: %logFile% in subCmd process is empty
:: how to inherit or pass environment/variable context to child processes?
call execJobs.bat | tee %logFile%

:: workaround, this doesn't work either
:: %logFile% is still empty in move command
:: call execJobs.bat | tee tmp
:: move /Y tmp %logFile%
:: del /F /Q tmp

Outputs: 输出:

C:\temp\logs\20150109-114443-exec.log
execJobs.bat | tee

The problem is that this 问题是

SET logFile=| php -r "$d = new DateTime(); $dateString = $d->format('Ymd-His');  
                     echo 'C:\\temp\\logs\\' . $dateString . '-exec.log';"

doesn't actually do what you want it to; 实际上并没有做您想要的; the pipe operator divides it into two commands, one of which deletes the logFile environment variable and the other of which prints the filename. 管道运算符将其分为两个命令,其中一个命令删除logFile环境变量,而另一个则打印文件名。

This 这个

%logFile%

does nothing at all, since logFile isn't defined and hence expands to an empty string. 根本不执行任何操作,因为未定义logFile ,因此扩展为空字符串。

The output you think is coming from the expansion of logFile is actually coming from the previous command. 您认为来自logFile扩展的输出实际上来自先前的命令。

In batch files, the "proper" way to execute a command and retrieve its output into a variable is to use a for /f command 在批处理文件中,执行命令并将其输出检索到变量中的“正确”方法是使用for /f命令

@echo off
    setlocal enableextensions disabledelayedexpansion
    set "logFile="

    for /f "usebackq" %%a in (`
        php -r "$d=new DateTime(); echo $d->format('Ymd-His');"
    `) do set "logFile=C:\temp\logs\%%a-exec.log"

    echo %logFile%

The for command will execute the code in the do clause for each line in the output of the execute comamnd. for命令将对execute comamnd输出中的每一行执行do子句中的代码。 The replaceable parameter ( %%a is used in this case) will hold the contents of the line being processed. 可替换参数(在这种情况下使用%%a )将保存正在处理的行的内容。

Thanks for the comment Harry, MC you beat me to it, posting the solution. 感谢您的评论Harry,MC,您击败了我,并发布了解决方案。 That makes sense. 那讲得通。 Here is the solution if anyone else is interested. 如果其他人有兴趣,这里是解决方案。 Adding the output from a command to a shell variable is not very intuitive :) 将命令的输出添加到shell变量不是很直观:)

:: this command places the output from the php script into a shell variable: logFile
for /f %%i in ('php -r "$d = new DateTime(); $dateString = $d->format('Ymd-His'); 
    echo 'C:\\temp\\logs\\' . $dateString . '-exec.log';"') do set logFile=%%i
echo Logging execJobs.bat output to: %logFile%
echo.
call execJobs.bat | tee %logFile%

I also pieced together some information from this answer as well: Windows batch assign output of a program to a variable 我还从该答案中整理了一些信息: Windows批处理将程序的输出分配给变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM