如何安全地回应文字字符串“%I”? [英] How to securely echo literal string "%I"?
问题描述
如何以安全的方式回显字符串%I
,而不管是否回显
命令行放在中为%I
循环?
例如, (code> cmd ):
$ b $ >>> rem //`echo`在`for`作用域之外,所以这个作品:
>>> echo%I
%I
>>> rem //`echo`在`for`范围内,所以`%I`被解析:
>>> (。)中的%I做@echo%I
。
>>> rem //转义不起作用,'%I`仍然解决:
>>> ()中的%I做@(echo ^%I& echo%^ I& echo ^%^ I& echo %% I)
。
。
。
%。
并且在批处理文件中:
@echo off&对/ /这里你需要加倍`%`标志!
和批处理文件)获得
rem //`echo`在`for`作用域之外,所以这个作用:
echo %% I& echo %%%% I
echo /
rem //`echo`在`for`范围内,所以`%% I`被解析为:
for %% )do(echo %% I& echo %%%% I)
echo /
rem //转义不起作用,'%% I`仍然被解析:
for %%我在(。)中执行(回声^ %%我&回声%% ^ I&回声^ %% ^ I&回声^%^%I& echo ^%^%^ I& echo ^ %% ^ b
$结果是:
%I
%% I
。
%。
。
。
。
I
^ I
%。
那我该如何改变上述方法code> cmd
%I
回显?解决方案有两种方法可以解决
对引用的无意扩展
%I $ c
$ b延迟展开
在
cmd
:>>>对/ /确保延迟扩展启用!
>>>设置STR =%I
>>>回声!STR!
%I
>>> (。)中的%I做@echo!STR!
%I
在批处理文件中:
@echo off&对/ /这里你需要加倍`%`标志!
setlocal EnableDelayedExpansion
setSTR = %% I
echo!STR!
为%%我在(。)中做echo!STR!
setSTR = %%%% I
echo!STR!
为%%我在(。)中做echo!STR!
endlocal
...结果:
%I
%I
%% I
%% I
然而,在某些情况下,所以可以通过
setlocal
/endlocal
。
<但是还有另外一种方法:
$ b $ h <$ 3 $> 循环
在
cmd
:>> ;> (%I)中的%J做@echo%J
%I
>>> (%)中的%J做(@)%我在(。)中做@echo%J
%I
在批处理文件中:...
@echo off&对/ /这里你需要加倍`%`标志! (%% I)do(
echo %% J
for %% I in(。)do echo %% J
)
(%% J I)do(
echo %% J
for %% I in(。)do echo %% J
)
...结果:
%I
%I
%% I
%% I
现在当然不能再回显
%% J
了。 >
How can I echo the string
%I
in a secure way, independent on whether or not theecho
command line is placed within afor %I
loop?For example, in command prompt (
cmd
):>>> rem // `echo` is outside of `for` scope, so this works: >>> echo %I %I >>> rem // `echo` is inside of `for` scope, so `%I` is resolved: >>> for %I in (.) do @echo %I . >>> rem // escaping does not help, `%I` is still resolved: >>> for %I in (.) do @(echo ^%I & echo %^I & echo ^%^I & echo %%I) . . . %.
And in a batch file...:
@echo off & rem // Here you need to double the `%` signs! rem // `echo` is outside of `for` scope, so this works: echo %%I & echo %%%%I echo/ rem // `echo` is inside of `for` scope, so `%%I` is resolved: for %%I in (.) do (echo %%I & echo %%%%I) echo/ rem // escaping does not help, `%%I` is still resolved: for %%I in (.) do (echo ^%%I & echo %%^I & echo ^%%^I & echo ^%^%I & echo ^%^%^I & echo ^%%^%%^I)
...the result is:
%I %%I . %. . . . I ^I %.
So how do I have to change the above approaches (both
cmd
and batch file) to get%I
echoed?解决方案There are two ways to work around the unintended expansion of the
for
reference%I
:Delayed Expansion
In
cmd
:>>> rem // Ensure that delayed expansion is enabled! >>> set "STR=%I" >>> echo !STR! %I >>> for %I in (.) do @echo !STR! %I
In a batch file...:
@echo off & rem // Here you need to double the `%` signs! setlocal EnableDelayedExpansion set "STR=%%I" echo !STR! for %%I in (.) do echo !STR! set "STR=%%%%I" echo !STR! for %%I in (.) do echo !STR! endlocal
...with the result:
%I %I %%I %%I
However, delayed expansion could harm under certain circumstances; so could the environment localisation done by
setlocal
/endlocal
.But there is still another way:
Wrap Around Another
for
LoopIn
cmd
:>>> for %J in (%I) do @echo %J %I >>> for %J in (%I) do @for %I in (.) do @echo %J %I
In a batch-file...:
@echo off & rem // Here you need to double the `%` signs! for %%J in (%%I) do ( echo %%J for %%I in (.) do echo %%J ) for %%J in (%%%%I) do ( echo %%J for %%I in (.) do echo %%J )
...with the result:
%I %I %%I %%I
Although you cannot echo
%%J
now any more, of course.这篇关于如何安全地回应文字字符串“%I”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文