是否可以在变量中存储重定向指令?这些方面的东西:
set redirect=>> buildlog.txt 2>&1
nmake -f Makefile %redirect%
原样,这不起作用。
答案 0 :(得分:2)
由于>
是shell metachars,因此您无法直接使用它们为变量分配文本。它们将由shell执行并且它们的RESULT被分配。所以你必须引用/逃避它们:
set redirect=^>^>buildlog.txt 2^>^&1
^-^--etc... cmd uses ^ to escape.
然后按预期工作:
C:\temp\z>set foo=^>^>log.txt
C:\temp\z>dir %foo%
C:\temp\z>dir
Volume in drive C is Windows7_OS
Volume Serial Number is 0E31-0E35
Directory of C:\temp\z
09/23/2016 10:32 AM <DIR> .
09/23/2016 10:32 AM <DIR> ..
09/23/2016 10:32 AM 333 log.txt
1 File(s) 333 bytes
2 Dir(s) 274,917,388,288 bytes free
C:\temp\z>type log.txt
Volume in drive C is Windows7_OS
Volume Serial Number is 0E31-0E35
Directory of C:\temp\z
09/23/2016 10:32 AM <DIR> .
09/23/2016 10:32 AM <DIR> ..
09/23/2016 10:32 AM 0 log.txt
1 File(s) 0 bytes
2 Dir(s) 274,917,388,288 bytes free
答案 1 :(得分:0)
您真正需要做的就是使用延迟扩展。
@echo off
set "redirect=>> buildlog.txt 2>&1"
setlocal enabledelayedexpansion
nmake -f Makefile !redirect!
endlocal