我对 cmd.exe 有点挣扎,所以让我把它放在代码中:
:: Set an environment variable var to `pwd`
:: (forget about the backslashes)
C:\somedir>set var=C:\\\somedir\\\ & call echo %var%
C:\\\somedir\\\
:: great! works.
:: Is the environment variable really there?
C:\somedir>set v
var=C:\\\somedir\\\
:: yes. ok.
:: Traverse directory recursively, find class files ending with T,
:: delete `pwd` from filenames:
C:\somedir>dir /s /b *T.class | grep T\. | sed 's/%var%//g'
C:\somedir\domain\test\ClassT.class
:: uh oh no! Didn't work.
:: Wait a minute. Is the environment variable really there?
C:\somedir>set v
var=C:\\\somedir\\\
:: hmm...yes, indeed?
:: ok. Take a breath. Start over. Empty/Delete var.
C:\somedir>set var=
:: Manually fill var
C:\somedir>set var=C:\\\somedir\\\
:: Try again...
C:\somedir>dir /s /b *T.class | grep T\. | sed 's/%var%//g'
domain\test\ClassT.class
::YES! uh...oh...no...w**hy does it not work above?
问题是,为什么我不能var
在管道中使用它,尽管它在环境中可用,以及当我设置 var “手动”时发生了什么变化,而不是set var=C:\\\somedir\\\ & call echo %var%
第一部分完全合法,完全手动,如清单后面.