最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【记录】尝试让windows中的cmd中一个命令的输出作为另外一个命令的输入且是传递到参数中间的

Windows crifan 6885浏览 0评论

【背景】

折腾:

【记录】尝试给右键打开Cygwin的mintty中加上log文件功能

期间,已知:

cmd中,运行:

E:\dev_install_root\cygwin\bin\date.exe +%Y%m%d_%H%M%S

可以得到:

20130906_224844

希望将此值,传递到:

E:\dev_install_root\cygwin\bin\mintty.exe –log=E:\201309.log

中,把201309替换掉,即类似于这样的含义:

E:\dev_install_root\cygwin\bin\mintty.exe –log=E:\(E:\dev_install_root\cygwin\bin\date.exe +%Y%m%d_%H%M%S).log

【折腾过程】

1.关于cmd中的重定向,只是>,貌似没法简单的实现这类效果。

2.然后参考了一堆的教程:

CMD命令for语句的用法详解

关于cmd命令的重定向输出

Using command redirection operators

CMD命令行高级教程精选合编

试了半天:

C:\Users\Administrator.PC-20130611GART>E:\dev_install_root\cygwin\bin\date.exe +%Y%m%d_%H%M%S
20130906_224844

C:\Users\Administrator.PC-20130611GART>E:\dev_install_root\cygwin\bin\date.exe +%Y%m%d_%H%M%S

C:\Users\Administrator.PC-20130611GART>set CUR_DATETIME=<E:\dev_install_root\cygwin\bin\date.exe +%Y%m%d_%H%M%S

C:\Users\Administrator.PC-20130611GART>echo %CUR_DATETIME%
 +%Y%m%d_%H%M%S

C:\Users\Administrator.PC-20130611GART>set CUR_DATETIME=|E:\dev_install_root\cygwin\bin\date.exe +%Y%m%d_%H%M%S
20130906_230226

C:\Users\Administrator.PC-20130611GART>echo %CUR_DATETIME%
 +%Y%m%d_%H%M%S

C:\Users\Administrator.PC-20130611GART>E:\dev_install_root\cygwin\bin\date.exe +%Y%m%d_%H%M%S | set CUR_DATETIME=
/usr/bin/date: write error

C:\Users\Administrator.PC-20130611GART>E:\dev_install_root\cygwin\bin\date.exe +%Y%m%d_%H%M%S > set CUR_DATETIME=
/usr/bin/date: extra operand `CUR_DATETIME='
Try `/usr/bin/date --help' for more information.

C:\Users\Administrator.PC-20130611GART>E:\dev_install_root\cygwin\bin\date.exe +%Y%m%d_%H%M%S
20130906_230315

C:\Users\Administrator.PC-20130611GART>E:\dev_install_root\cygwin\bin\date.exe +%Y%m%d_%H%M%S > set CUR_DATETIME=

C:\Users\Administrator.PC-20130611GART>set CUR_DATETIME=`E:\dev_install_root\cygwin\bin\date.exe +%Y%m%d_%H%M%S`

C:\Users\Administrator.PC-20130611GART>echo %CUR_DATETIME%
`E:\dev_install_root\cygwin\bin\date.exe +%Y%m%d_%H%M%S`

C:\Users\Administrator.PC-20130611GART>set CUR_DATETIME=<E:\dev_install_root\cygwin\bin\date.exe +%Y%m%d_%H%M%S

C:\Users\Administrator.PC-20130611GART>echo %CUR_DATETIME%
 +%Y%m%d_%H%M%S

C:\Users\Administrator.PC-20130611GART>CUR_DATETIME=2013
'CUR_DATETIME' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

C:\Users\Administrator.PC-20130611GART>set CUR_DATETIME=2013

C:\Users\Administrator.PC-20130611GART>echo %CUR_DATETIME%
2013

C:\Users\Administrator.PC-20130611GART>E:\dev_install_root\cygwin\bin\date.exe +%Y%m%d_%H%M%S
20130906_231133

C:\Users\Administrator.PC-20130611GART>E:\dev_install_root\cygwin\bin\date.exe +%Y%m%d_%H%M%S 1>&1
重定向句柄 1 时,
无法复制此句柄。

C:\Users\Administrator.PC-20130611GART>set CUR_DATETIME=|'E:\dev_install_root\cygwin\bin\date.exe +%Y%m%d_%H%M%S '
文件名、目录名或卷标语法不正确。

C:\Users\Administrator.PC-20130611GART>set CUR_DATETIME=|E:\dev_install_root\cygwin\bin\date.exe +%Y%m%d_%H%M%S
20130906_231328

C:\Users\Administrator.PC-20130611GART>echo %CUR_DATETIME%
2013

C:\Users\Administrator.PC-20130611GART>set CUR_DATETIME=<E:\dev_install_root\cygwin\bin\date.exe +%Y%m%d_%H%M%S

C:\Users\Administrator.PC-20130611GART>echo %CUR_DATETIME%
 +%Y%m%d_%H%M%S

C:\Users\Administrator.PC-20130611GART>set CUR_DATETIME=|E:\dev_install_root\cygwin\bin\date.exe
Fri Sep  6 23:14:31     2013

C:\Users\Administrator.PC-20130611GART>echo %CUR_DATETIME%
 +%Y%m%d_%H%M%S

C:\Users\Administrator.PC-20130611GART>set CUR_DATETIME=<E:\dev_install_root\cygwin\bin\date.exe

C:\Users\Administrator.PC-20130611GART>echo %CUR_DATETIME%
%CUR_DATETIME%

C:\Users\Administrator.PC-20130611GART>echo %CUR_DATETIME%

C:\Users\Administrator.PC-20130611GART>E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\`E:\dev_install_root\cygwin\bin\date.exe +%Y%m%d_%
H%M%S`.log

C:\Users\Administrator.PC-20130611GART>

结果还是没达到目标。

3.参考:

Variables: extract part of a variable (substring)

去试试。

C:\Users\Administrator.PC-20130611GART>SET _test=123456789abcdef0 && SET _result=%_test:~0,5% && ECHO %_result%
%_result%

C:\Users\Administrator.PC-20130611GART>SET _test=123456789abcdef0 & SET _result=%_test:~0,5% & ECHO %_result%
%_test:~0,5%

C:\Users\Administrator.PC-20130611GART>SET _test=123456789abcdef0 | SET _result=%_test:~0,5% | ECHO %_result%
12345

4.再参考:

Howto: Get variable substrings in Bat/Cmd scripts

去试试:

C:\Users\Administrator.PC-20130611GART>date
当前日期: 2013/09/06 周五
输入新日期: (年月日)
C:\Users\Administrator.PC-20130611GART>time
当前时间: 23:31:07.82
输入新时间:
C:\Users\Administrator.PC-20130611GART>date /t
2013/09/06 周五

C:\Users\Administrator.PC-20130611GART>time /t
23:31

C:\Users\Administrator.PC-20130611GART>time /?
显示或设置系统时间。

TIME [/T | time]

显示当前时间设置和输入新时间的提示,请键入
不带参数的 TIME。要保留现有时间,请按 Enter。

如果命令扩展被启用,TIME 命令会支持 /T 命令行开关;该命令行开关告诉
命令只输出当前时间,但不提示输入新时间。

C:\Users\Administrator.PC-20130611GART>date --help
系统无法接受输入的日期。
输入新日期: (年月日)
C:\Users\Administrator.PC-20130611GART>date /?
显示或设置日期。

DATE [/T | date]

显示当前日期设置和输入新日期的提示,请键入
不带参数的 DATE。要保留现有日期,请按 Enter。

如果命令扩展被启用,DATE 命令会支持 /T 开关;
该开关指示命令只输出当前日期,但不提示输入新日期。

C:\Users\Administrator.PC-20130611GART>echo %date%
2013/09/06 周五

C:\Users\Administrator.PC-20130611GART>echo %date:~4,10%
/09/06 周五

C:\Users\Administrator.PC-20130611GART>echo %date:~,4%
2013

C:\Users\Administrator.PC-20130611GART>echo %date:~5,2%
09

C:\Users\Administrator.PC-20130611GART>echo %date:~8,2%
06

C:\Users\Administrator.PC-20130611GART>echo %time%
23:34:25.60

C:\Users\Administrator.PC-20130611GART>echo %time~,2%
%time~,2%

C:\Users\Administrator.PC-20130611GART>echo %time~0,2%
%time~0,2%

C:\Users\Administrator.PC-20130611GART>echo %time%
23:34:57.07

C:\Users\Administrator.PC-20130611GART>echo %time:~,2%
23

C:\Users\Administrator.PC-20130611GART>echo %time:~3,2%
35

C:\Users\Administrator.PC-20130611GART>echo %time:~6,2%
22

C:\Users\Administrator.PC-20130611GART>echo %time:~9,2%
17

C:\Users\Administrator.PC-20130611GART>set DATE=date | echo %DATE%
2013/09/06 周五

C:\Users\Administrator.PC-20130611GART>set CURDATE=date | echo %CURDATE%
%CURDATE%

C:\Users\Administrator.PC-20130611GART>set CURDATE=%date% | echo %CURDATE%
%CURDATE%

C:\Users\Administrator.PC-20130611GART>set CURDATE=%date%

C:\Users\Administrator.PC-20130611GART>echo %CURDATE%
2013/09/06 周五

C:\Users\Administrator.PC-20130611GART>set CURDATE=%date% && echo %CURDATE%
2013/09/06 周五

C:\Users\Administrator.PC-20130611GART>set CURTIME=%date% && echo %CURTIME%
%CURTIME%

C:\Users\Administrator.PC-20130611GART>set CURTIME=%time% && echo %CURTIME%
2013/09/06 周五

C:\Users\Administrator.PC-20130611GART>set CURTIME=%time% && echo %CURTIME%
23:39:42.27

C:\Users\Administrator.PC-20130611GART>set CURDATE=%date% && echo %CURDATE:~,4%%CURDATE:~5,2%%CURDATE:~8,4%
20130906 周

C:\Users\Administrator.PC-20130611GART>set CURDATE=%date% && echo %CURDATE:~,4%%CURDATE:~5,2%%CURDATE:~8,2%
20130906

C:\Users\Administrator.PC-20130611GART>set CURTIME=%date% && echo %CURTIME:~,2%%CURTIME:~3,2%%CURTIME:~6,2%%CURTIME:~9,2%

C:\Users\Administrator.PC-20130611GART>set CURTIME=%date% && echo %CURTIME:~,2%%CURTIME:~3,2%%CURTIME:~6,2%%CURTIME:~9,2%
23395517

C:\Users\Administrator.PC-20130611GART>set CURDATE=%date% && set CURTIME=%date% && echo %CURDATE:~,4%%CURDATE:~5,2%%CURDATE:~8,2%_%CURTIME:~
,2%%CURTIME:~3,2%%CURTIME:~6,2%%CURTIME:~9,2%
20130906_203/9/6

C:\Users\Administrator.PC-20130611GART>set CURTIME=%time% && echo %CURTIME:~,2%%CURTIME:~3,2%%CURTIME:~6,2%%CURTIME:~9,2%
203/9/6

C:\Users\Administrator.PC-20130611GART>set CURTIME=%time% && echo %CURTIME:~,2%%CURTIME:~3,2%%CURTIME:~6,2%%CURTIME:~9,2%
23431515

C:\Users\Administrator.PC-20130611GART>set CURDATE=%date% && set CURTIME=%time% && echo %CURDATE:~,4%%CURDATE:~5,2%%CURDATE:~8,2%_%CURTIME:~
,2%%CURTIME:~3,2%%CURTIME:~6,2%%CURTIME:~9,2%
20130906_23431811

C:\Users\Administrator.PC-20130611GART>set CURDATE=%date% && set CURTIME=%time% && echo %CURDATE:~,4%%CURDATE:~5,2%%CURDATE:~8,2%_%CURTIME:~
,2%%CURTIME:~3,2%%CURTIME:~6,2%%CURTIME:~9,2%.log
20130906_23434965.log

C:\Users\Administrator.PC-20130611GART>set CURDATE=%date% && set CURTIME=%time% && set LOGFILE=%CURDATE:~,4%%CURDATE:~5,2%%CURDATE:~8,2%_%CU
RTIME:~,2%%CURTIME:~3,2%%CURTIME:~6,2%%CURTIME:~9,2%.log && echo %LOGFILE%
%LOGFILE%

C:\Users\Administrator.PC-20130611GART>set CURDATE=%date% && set CURTIME=%time% && set LOGFILE=%CURDATE:~,4%%CURDATE:~5,2%%CURDATE:~8,2%_%CU
RTIME:~,2%%CURTIME:~3,2%%CURTIME:~6,2%%CURTIME:~9,2%.log && echo %LOGFILE%
20130906_23440529.log

C:\Users\Administrator.PC-20130611GART>

5.然后再去最后测试,能否真正用于mintty的log参数:

C:\Users\Administrator.PC-20130611GART>set CURDATE=%date% && set CURTIME=%time% && set CURLOGFILE=%CURDATE:~,4%%CURDATE:~5,2%%CURDATE:~8,2%_
%CURTIME:~,2%%CURTIME:~3,2%%CURTIME:~6,2%%CURTIME:~9,2%.log && E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOGFILE%

虽然打开mintty了,也创建log文件了,可惜只是:

%CURLOGFILE%

而不是预想的日期的值。

但是,试了第二次,结果就可以了:

cmd open mintty also create datetime log file

即,用:

C:\Users\Administrator.PC-20130611GART>set CURDATE=%date% && set CURTIME=%time% && set CURLOGFILE=%CURDATE:~,4%%CURDATE:~5,2%%CURDATE:~8,2%_%CURTIME:~,2%%CURTIME:~3,2%%CURTIME:~6,2%%CURTIME:~9,2%.log && E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOGFILE%

的cmd命令,打开了mintty,

且的确生成了对应的log文件:

E:\20130906_23454766.log

效果还是不错的。

6.不知道,此处是否是有其他问题。搞得第二次才可以。

然后,把此部分的设置,尝试去添加到原先的注册表中。

之前是这样的,只有-e参数:

origin run cygwin is only mintty e para

现在去添加变为:

set DATENOW=%date% && set TIMENOW=%time% && set CURLOG=%DATENOW:~,4%%DATENOW:~5,2%%DATENOW:~8,2%_%TIMENOW:~,2%%TIMENOW:~3,2%%TIMENOW:~6,2%%TIMENOW:~9,2%.log && E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOG% -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

即:

change runcygwin to set value then run mintty

然后,再去测试一下:

for crosstoolng build dir do runcygwin

效果是:

找不到应用程序

can not find app

难道是command的值,太长了?(刚才在regedit中,都无法显示全。。。)

7.重新把值,改短点:

set DN=%date% && set TN=%time% && set CURLOG=%DN:~,4%%DN:~5,2%%DN:~8,2%_%TN:~,2%%TN:~3,2%%TN:~6,2%%TN:~9,2%.log && E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOG% -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

结果还是无法显示完全:

can not show all command content

再去改短:

set DN=%date% && set TN=%time% && set CURLOG=%DN:~,4%%DN:~5,2%%DN:~8,2%_%TN:~,2%.log && E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOG% -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

还不行。

再短:

set DN=%date% && set CURLOG=%DN:~,4%%DN:~5,2%%DN:~8,2%.log && mintty --log=E:\%CURLOG% -e bash -c "/bin/xhere /bin/bash.exe '%L'"

看看不给mintty和bash加路径,是否可以找到:

结果效果依旧,还是上面的错误:找不到应用程序

所以,看来是不识别set?还是不识别此处不加路径的mintty。

8.加上路径,再试试:

set DN=%date% && set CURLOG=%DN:~,4%%DN:~5,2%.log && E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOG% -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

可以显示完整,然后看看能否运行。

结果还是:找不到应用程序

9.去cmd中,试试此处上面的命令:

set DN=%date% && set CURLOG=%DN:~,4%%DN:~5,2%.log && E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOG% -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

结果也是之前类似错误:

还是只能创建%CURLOG%的log文件,而不是真正的值。

看来是此处,对于变量不识别。

10.后续针对多个命令同一行执行,参考了:

How to run two commands in one line in Windows CMD?

去试了试:

C:\Users\Administrator.PC-20130611GART>set DTN=%date% && set CURRENTLOG=%DTN:~,4%%DTN:~5,2%.log && echo %CURRENTLOG%
%CURRENTLOG%

C:\Users\Administrator.PC-20130611GART>set DATEN=%date% & set CURRENTL=%DATEN:~,4%%DATEN:~5,2%.log & echo %CURRENTL%
%CURRENTL%

C:\Users\Administrator.PC-20130611GART>set DATEN1=%date% | set CURRENTL1=%DATEN1:~,4%%DATEN1:~5,2%.log | echo %CURRENTL1%
%CURRENTL1%

C:\Users\Administrator.PC-20130611GART>set DATEN2=%date% & set CURRENTL2=%DATEN2:~,4%%DATEN2:~5,2%.log & echo %CURRENTL2%
%CURRENTL2%

C:\Users\Administrator.PC-20130611GART>set DATEN3=%date% & echo %DATEN3%
%DATEN3%

C:\Users\Administrator.PC-20130611GART>set DATEN4=%date% && echo %DATEN4%
%DATEN4%

C:\Users\Administrator.PC-20130611GART>set DATEN5=%date% , echo %DATEN5%

C:\Users\Administrator.PC-20130611GART>set DATEN6=%date% ; echo %DATEN6%

C:\Users\Administrator.PC-20130611GART>set DATEN7=%date%

C:\Users\Administrator.PC-20130611GART>echo %DATEN7%
2013/09/07 周六

C:\Users\Administrator.PC-20130611GART>

对此,官网:

在单个行上的多个命令可能无法运行,当您使用 & & 命令分隔符

也解释了。

 

11.参考了:

How to execute multiple commands in a single line

再去试试,果然是:

C:\Users\Administrator.PC-20130611GART>set DATEN8=%date% & echo %DATEN8% & echo %DATEN8%
%DATEN8%
%DATEN8%

C:\Users\Administrator.PC-20130611GART>set DATEN8=%date% & echo %DATEN8% & echo %DATEN8%
2013/09/07 周六
2013/09/07 周六

即,第一次不会显示,之后才会显示。

感觉像是bug。。

12.参考:

run two commands in one windows cmd line, one command is SET command

去试试CALL:

的确可以了:

C:\Users\Administrator.PC-20130611GART>set DATEN9=%date% & CALL echo %DATEN9%
2013/09/07 周六

13.所以,再去试试:

C:\Users\Administrator.PC-20130611GART>set DATETIME1=%date% && CALL set CURLOGFILE1=%DATETIME1:~,4%%DATETIME1:~5,2%.log && CALL echo %CURLOG
FILE1%
201309.log

的确是可以了。

14.再去试试:

set DATETIME=%date% && CALL set CURLOGFILE=%DATETIME:~,4%%DATETIME:~5,2%.log && CALL E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOGFILE% -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

结果是:

好像和已有的

201309.log

冲突了。

把新的内容,append到原因的内容中了:

append to origin log file

15.所以,再去试试:

set DATENOW=%date% && CALL set TIMENOW=%time% && set CURLOGFILE=%DATENOW:~,4%%DATENOW:~5,2%%DATENOW:~8,2%_%TIMENOW:~,2%%TIMENOW:~3,2%%TIMENOW:~6,2%%TIMENOW:~9,2%.log CALL && E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOGFILE% -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

结果是:

好像还是没有创建想要的log文件。

汗,原来是漏掉一个call,去改为:

set DATENOW=%date% && CALL set TIMENOW=%time% && CALL set CURLOGFILE=%DATENOW:~,4%%DATENOW:~5,2%%DATENOW:~8,2%_%TIMENOW:~,2%%TIMENOW:~3,2%%TIMENOW:~6,2%%TIMENOW:~9,2%.log && CALL E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOGFILE% -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

结果是:

0401532 log  No such file or directory

16.再去试试:

set DATENOW=%date% && call set TIMENOW=%time% && call set CURLOGFILE=%DATENOW:~,4%%DATENOW:~5,2%%DATENOW:~8,2%_%TIMENOW:~,2%.log && call E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOGFILE% -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

结果是:

0381234.log: No such file or directory

再试试:

set DATENOW=%date% && call set TIMENOW=%time% && call set CURLOGFILE=%DATENOW:~,4%%DATENOW:~5,2%%DATENOW:~8,2%_%TIMENOW:~,2%.log && E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOGFILE% -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

结果是:

0.log: No such file or directory

再试试:

set DATENOW=%date% & call set TIMENOW=%time% & call set CURLOGFILE=%DATENOW:~,4%%DATENOW:~5,2%%DATENOW:~8,2%_%TIMENOW:~,2%.log & call E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOGFILE% -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

结果还是:

0.log: No such file or directory

再试:

set DATENOW=%date% & call set CURLOGFILE=%DATENOW:~,4%%DATENOW:~5,2%%DATENOW:~8,2%.log & call E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOGFILE% -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

结果错误依旧。

17.再试试之前正常的:

set DATETIME=%date% && CALL set CURLOGFILE=%DATETIME:~,4%%DATETIME:~5,2%.log && CALL E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOGFILE% -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

然后是正常的,且可以得到log文件的:

E:\20130907.log

内容是刚才的命令输出。

接着是个加上time的:

set DATETIME=%date% && CALL set TIMENOW=%time% && CALL set CURLOGFILE=%DATETIME:~,4%%DATETIME:~5,2%_%TIMENOW:~,2%%TIMENOW:~3,2%%TIMENOW:~6,2%%TIMENOW:~9,2%.log && CALL E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOGFILE% -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

结果是:

开始可以,多次执行,就找不到log了。

18。再去试试:

set DATETIME=%date% && CALL set TIMENOW=%time% && CALL set CURLOGFILE=%DATETIME:~,4%%DATETIME:~5,2%%TIMENOW:~,2%%TIMENOW:~3,2%%TIMENOW:~6,2%%TIMENOW:~9,2%.log && CALL E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOGFILE% -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

结果为:

找不到文件。

再试试:

set DATETIME=%date% && set TIMENOW=%time% && set CURLOGFILE=%DATETIME:~,4%%DATETIME:~5,2%%TIMENOW:~,2%%TIMENOW:~3,2%%TIMENOW:~6,2%%TIMENOW:~9,2%.log && CALL E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOGFILE% -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

结果,

0533558.log: No such file or directory

貌似不能加time???

再次试试:

set DATETIME=%date% && CALL set CURLOGFILE=%DATETIME:~,4%%DATETIME:~5,2%.log && CALL E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOGFILE% -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

结果不行。

再试:

set DATENOW=%date% && CALL set CURLOGFILE=%DATENOW:~,4%%DATENOW:~5,2%.log && CALL E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOGFILE% -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

结果:可以创建:

201309.log

再试:

set DATENOW=%date% && CALL set CURLOGFILE=%DATENOW:~,4%%DATENOW:~5,2%%DATENOW:~8,2%.log && CALL E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOGFILE% -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

结果:

没创建要的log文件。

在重新执行:

set DATENOW=%date% && CALL set CURLOGFILE=%DATENOW:~,4%%DATENOW:~5,2%%DATENOW:~8,2%.log && CALL E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\%CURLOGFILE% -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

才生成:

20130907.log

19.最终:

暂时貌似没法实现,给log文件加上动态的命令,获得当前日期和时间。

暂时只能固定为,设置注册表值为:

E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\dev_install_root\cygwin\home\log\cygwin_mintty_log.txt -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

然后通过RunCygwin,是可以:

自动创建对应的log文件:

E:\dev_install_root\cygwin\home\log\cygwin_mintty_log.txt

的,且:

每次重新启动一个Cygwin,然后就会TRUC,即清空原先log文件,重新写入当前的log输出内容。

效果如下:

fix log file name can work

20.另外,尝试去使用环境变量试试:

手动新建一个环境变量:

CURRENTDATE=20130907

然后RunCygwin的command的值改为:

E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\dev_install_root\cygwin\home\log\%CURRENTDATE%.txt -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

看看是否正常。

结果只能生成:

E:\dev_install_root\cygwin\home\log\CURRENTDATE.txt

所以不是想要的。

但是cmd中,的确可以得到该变量的值的:

C:\Users\Administrator.PC-20130611GART>echo %CURRENTDATE%
20130907

C:\Users\Administrator.PC-20130611GART>

 

然后,算了,就用最后最简单的吧:

 

【总结】

去注册表中,将:

HKEY_CLASSES_ROOT\Directory\shell\RunCygwin\command

值设置为:

E:\dev_install_root\cygwin\bin\mintty.exe --log=E:\dev_install_root\cygwin\home\log\cygwin_mintty_log.txt -e E:\dev_install_root\cygwin\bin\bash -c "/bin/xhere /bin/bash.exe '%L'"

这样:

每次右键任意一个文件夹,选择RunCygwin,然后就

  • 调用mintty打开cygwin了;
  • 且定位到当前文件夹。
  • 且清空对应的log文件:E:\dev_install_root\cygwin\home\log\cygwin_mintty_log.txt
  • 且接下来的,所有的cygwin窗口的输出的内容,都自动保存到该log文件中;

注意:

如果另外再(通过右击选择RunCygwin或者独立打开Cygwin),都会导致清空该log文件,即,该log文件,只能记录最后一个打开的Cygwin的mintty的输出的内容

 

最后附上导出的注册表内容:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\RunCygwin\command]
@="E:\\dev_install_root\\cygwin\\bin\\mintty.exe --log=E:\\dev_install_root\\cygwin\\home\\log\\cygwin_mintty_log.txt -e E:\\dev_install_root\\cygwin\\bin\\bash -c \"/bin/xhere /bin/bash.exe '%L'\""

需要的自己另存为

runCygwin_WithMinttyLogFile.reg

然后双击运行即可。

转载请注明:在路上 » 【记录】尝试让windows中的cmd中一个命令的输出作为另外一个命令的输入且是传递到参数中间的

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (2)

  1. 哇感谢楼主!我也想过这个问题,也折腾过挺长时间,但是远没有楼主这种毅力。 另外那个第二次才行的不是bug,应该是cmd的一个机制,就是预处理了这些命令,可以试试启动变量延迟来解决。
    snljty6年前 (2017-12-20)回复
85 queries in 0.181 seconds, using 22.16MB memory