为什么SHIFT不更改值?

时间:2018-11-09 18:40:45

标签: cmd

似乎SHIFT无效或我不理解。这是我要制作的脚本。

C:>type shiftit.bat
@echo off
echo all is %*
echo 0 is %0
echo 5 is %5
shift /5
echo shifted by 5
echo 0 is %0
echo 1 is %1
echo 2 is %2

但是,我期望移位后%1将包含“ 5”或“ 6”。它不是。看来SHIFT /5命令无效。我想念什么?

12:57:25.78  C:\src\t
C:>cmd /E:ON
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

12:57:29.57  C:\src\t
C:>shiftit.bat 1 2 3 4 5 6 7 8 9
all is 1 2 3 4 5 6 7 8 9
0 is shiftit.bat
5 is 5
shifted by 5
0 is shiftit.bat
1 is 1
2 is 2

1 个答案:

答案 0 :(得分:1)

您不正确, shift / 5将删除参数%5,因此%6变为%5,%7->%6等。

要删除%1 ..%5,您必须移动5次。
allargs%*不受移位的影响。

查看更改的批次

:: shiftit.bat 1 2 3 4 5 6 7 8 9
@echo off
echo all is %*
Echo %%1 IS %1
Echo %%2 IS %2
Echo %%3 IS %3
Echo %%4 IS %4
Echo %%5 IS %5
Echo %%6 IS %6
Echo %%7 IS %7
Echo %%8 IS %8
Echo %%9 IS %9
shift /5
echo shifted by 5

echo all is %*
Echo %%1 IS %1
Echo %%2 IS %2
Echo %%3 IS %3
Echo %%4 IS %4
Echo %%5 IS %5
Echo %%6 IS %6
Echo %%7 IS %7
Echo %%8 IS %8
Echo %%9 IS %9

和示例输出:

> shiftit.bat 1 2 3 4 5 6 7 8 9
all is 1 2 3 4 5 6 7 8 9
%1 IS 1
%2 IS 2
%3 IS 3
%4 IS 4
%5 IS 5
%6 IS 6
%7 IS 7
%8 IS 8
%9 IS 9
shifted by 5
all is 1 2 3 4 5 6 7 8 9
%1 IS 1
%2 IS 2
%3 IS 3
%4 IS 4
%5 IS 6
%6 IS 7
%7 IS 8
%8 IS 9
%9 IS