Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

variable not getting update inside if condition in batch script

  @echo off

  SET CONFIGS_QUASAR1=Q1_1 Q1_2 Q1_3 Q1_4 Q1_5 Q1_6
  SET CONFIGS_QUASAR2=Q2_1 Q2_2 Q2_3 Q2_4 Q2_5 Q2_6
  SET CONFIGS_QUASAR3=Q3_1 Q3_2 Q3_3 Q3_4 Q3_5 Q3_6
  SET CONFIGS_QUASAR0B=Q0B_1 Q0B_2 Q0B_3 Q0B_4 Q0B_5 Q0B_6

  FOR %%A IN (QUASAR1 QUASAR2 QUASAR3 QUASAR0B) DO (
    IF "%%A" == "QUASAR1" (
      SET CONFIGS=%CONFIGS_QUASAR1%
    ) ELSE IF "%%A" == "QUASAR2" (
      SET CONFIGS=%CONFIGS_QUASAR2%
    ) ELSE IF "%%A" == "QUASAR3" (
      SET CONFIGS=%CONFIGS_QUASAR3%
    ) ELSE IF "%%A" == "QUASAR0B" (
      SET CONFIGS=%CONFIGS_QUASAR0B%
    )

    echo %%A
    echo %CONFIGS%
  )

  pause

I'm very new to batch file programming. I have written a very small program to set a variable inside an 'if' condition, but the variable("CONFIGS") is not getting udated in the above mentioned program.

Please check the program and tell me what should be modified?

Output of the batch file:

QUASAR1
Q0B_1 Q0B_2 Q0B_3 Q0B_4 Q0B_5 Q0B_6
QUASAR2
Q0B_1 Q0B_2 Q0B_3 Q0B_4 Q0B_5 Q0B_6
QUASAR3
Q0B_1 Q0B_2 Q0B_3 Q0B_4 Q0B_5 Q0B_6
QUASAR0B
Q0B_1 Q0B_2 Q0B_3 Q0B_4 Q0B_5 Q0B_6
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Excuse me. Although your problem is directly related to Delayed Expansion as npocmaka indicated in his answer, I would like to attract your attention to the following advice.

When you use Delayed Expansion you may make good use of the fact that the second (delayed) !expansion! may work over text that has been previously modified or %expanded%. This is a powerful Batch file feature that may avoid complex manipulations.

For example, suppose that you have a variable called result that will store the sum of another variable called term plus the values 1, 2, 3 and 11. One way to do that is this:

for %%a in (1 2 3 11) do (
   if %%a == 1 (
      set /A result=term+1
   ) else if %%a == 2 (
      set /A result=term+2
   ) else if %%a == 3 (
      set /A result=term+3
   ) else if %%a == 11 (
      set /A result=term+11
   )
   echo The sum of !term! plus %%a is !result!
)

However, you may conclude that all those IF's are not necessary, because you may directly take the value of the second term this way:

for %%a in (1 2 3 11) do (
   set /A result=term+%%a
   echo The sum of !term! plus %%a is !result!
)

Well, the same conclusion may be used in your code this way:

@echo off
setlocal EnableDelayedExpansion

rem Create CONFIGS_QUASAR 1, 2, 3 and 0B strings
FOR %%A IN (1 2 3 0B) DO (
   rem Initialize this string
   SET "CONFIGS_QUASAR%%A="
   rem Fill this string with their values
   FOR /L %%I IN (1,1,6) DO SET "CONFIGS_QUASAR%%A=!CONFIGS_QUASAR%%A! Q%%A_%%I"
)

FOR %%A IN (1 2 3 0B) DO (
   SET CONFIGS=!CONFIGS_QUASAR%%A!

   echo QUASAR%%A
   echo !CONFIGS!
)

pause

Output:

QUASAR1
 Q1_1 Q1_2 Q1_3 Q1_4 Q1_5 Q1_6
QUASAR2
 Q2_1 Q2_2 Q2_3 Q2_4 Q2_5 Q2_6
QUASAR3
 Q3_1 Q3_2 Q3_3 Q3_4 Q3_5 Q3_6
QUASAR0B
 Q0B_1 Q0B_2 Q0B_3 Q0B_4 Q0B_5 Q0B_6

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...