DOS:变量嵌套和命令嵌套
我们先看一下下面dos代码:
@echo off
set num=Redtek 2006 bbs.cn-dos.net
set a=123245
set b=87754321
echo.
echo a=%a% b=%b% num=%num%
set result=%%%%%%%%num:~%%%%b:~%a:~3,1%,1%%%%,4%%%%%%%%
echo %result%
call set result=%%%%%%%%num:~%%%%b:~%a:~3,1%,1%%%%,4%%%%%%%%
echo %result%
call call set result=%%%%%%%%num:~%%%%b:~%a:~3,1%,1%%%%,4%%%%%%%%
echo %result%
call call call set result=%%%%%%%%num:~%%%%b:~%a:~3,1%,1%%%%,4%%%%%%%%
echo %result%
echo.
pause
set num=Redtek 2006 bbs.cn-dos.net
set a=123245
set b=87754321
echo.
echo a=%a% b=%b% num=%num%
set result=%%%%%%%%num:~%%%%b:~%a:~3,1%,1%%%%,4%%%%%%%%
echo %result%
call set result=%%%%%%%%num:~%%%%b:~%a:~3,1%,1%%%%,4%%%%%%%%
echo %result%
call call set result=%%%%%%%%num:~%%%%b:~%a:~3,1%,1%%%%,4%%%%%%%%
echo %result%
call call call set result=%%%%%%%%num:~%%%%b:~%a:~3,1%,1%%%%,4%%%%%%%%
echo %result%
echo.
pause
运行结果如下:
下面来一句一句给大家讲解一下。
(1)
set result=%%%%%%%%num:~%%%%b:~%a:~3,1%,1%%%%,4%%%%%%%%
请大家记住,直接运行命令,只解析第一层变量,其余的当作字符串
%a:~3,1%
得到的是2,所以result最后得到的是 %%%%num:~%%b:~2,1%%,4%%%%
每个下滑线为一层变量
%% %% %% %% num:~%% %%b:~%a:~3,1%,1%% %%,4%% %% %% %%
(2)
call set result=%%%%%%%%num:~%%%%b:~%a:~3,1%,1%%%%,4%%%%%%%%
一层命令嵌套,可解析两层变量,其余当作字符串
先解析第一层变量,得到%%%%num:~%%b:~2,1%%,4%%%%
然后解析第二层,规则一样,两个%变成一个%,于是得到
%%num:~%b:~2,1%,4%%
(3) call call set result=%%%%%%%%num:~%%%%b:~%a:~3,1%,1%%%%,4%%%%%%%%
以此类推,可以解析三层变量
由%%num:~%b:~2,1%,4%% 得到%num:~7,4%
(4)最后得到num:~7,4 ,num变量中,从第1+7=8个开始算起,一共4个字符组成的2006