搜尋此網誌

2011年10月22日 星期六

EFI Shell commands -- FOR(在一個項目集合中,去行一個或多個指令對於每個項目)

Executes one or more commands for each item in a set of items.

FOR %indexvar IN set
    command [arguments]
    [command [arguments]]
     ...
ENDFOR


FOR %indexvar RUN (start end[ step])
    command [arguments]
    [command [arguments]]
    ...
ENDFOR


    %indexvar           - Variable name used to index a set
    set                  - Set to be searched
    command [arguments]  - Command to be executed with optional arguments

Note:
    1. The FOR command is only available in batch script files.
    2. FOR shall be matched with ENDFOR.
    3. start and end can be any integer.  Up to 6 digits allowed.
    4. step can be any integer but zero.  Up to 6 digits allowed.
    5. step is optional, if step is not specified, step will be automatically
       determined as below:
         if start <= end, then step = 1
         if start > end,  then step = -1

Examples:
    #
    #  Sample for loop type contents of all *.txt files
    #
    for %a in *.txt
       type %a
       echo ===== %a done =====
    endfor

    #
    #  To repeat operations, supporting multiple loop:
    #
        for %a in 1 2 3 4 5 6 7 8 9
          for %b in a b c d e f g h i j k l m n o p q r s t u v w x y z
            alias %a a%a
            alias %b %b%a
          endfor
        endfor

        for %a run (1 3)
          echo %a
        endfor
Output:
1
2
3

        for %a run (3 1)
          echo %a
        endfor
Output:
3
2
1

12 則留言:

  1. 您好!
    如果我需要將外部的一數字引入批次檔,然後執行加一,要加4次!
    加1的部份用16進製來表示要怎麼寫?

    回覆刪除
  2. 存成test1.nsh檔案:
    echo -off
    if exist %myval% then
    set myval 0
    endif
    for %b in 0 1 2 3
    for %a run (0 19999)
    if %myval% == %a then
    echo "[ %a ]"
    set mylock 1
    else
    if %mylock% == 1 then
    set mylock 0
    set myval %a
    goto Leave_For
    endif
    endif
    endfor
    :Leave_For
    echo "%myval%"
    set -d mylock
    endfor
    echo -on

    回覆刪除
    回覆
    1. 存成test1.nsh檔案, 從外部輸入一個數字。\>test1.nsh 50

      echo -off
      if not exist %1% then
      echo [[%1]]
      set myval %1%
      endif
      if exist %myval% then
      set myval 0
      endif
      for %b in 0 1 2 3
      for %a run (0 19999)
      if %myval% == %a then
      echo "[ %a ]"
      set mylock 1
      else
      if %mylock% == 1 then
      set mylock 0
      set myval %a
      goto Leave_For
      endif
      endif
      endfor
      :Leave_For
      echo "%myval%"
      set -d mylock
      endfor
      echo -on


      example:
      \>test1.nsh 50 #設定一個數字
      [[50]]
      [50]
      51
      [51]
      52
      [52]
      53
      [53]
      54
      \>test1.nsh #再執行一次
      [54]
      55
      [55]
      56
      [56]
      57
      [57]
      58


      刪除
    2. 謝謝您的解答,另外請問您:
      1‧請問若這組數字是在文字檔裡頭要如何引入批次檔呢?
      2‧能用16進制的方式加1嗎?
      3. 我想將加完之後的最後一組數字存在文字檔裡,要如何做呢?

      刪除
    3. 將此程式存成t3.nsh
      ======t3.nsh==================================
      echo -off

      if not exist %2% then
      echo [[%2]]
      set myval_xx %2%
      endif

      if exist %myval_xx% then
      set myval_xx 0
      endif

      if not exist %1% then
      echo [[%1]]
      set myval_x %1%
      endif

      if exist %myval_x% then
      set myval_x 0
      endif

      for %c in 1 2 3 4
      set not_skip 1
      if %myval_x% == F then
      if %myval_xx% == F then
      set myval_x 0
      set myval_xx 0
      set mylock_x 0
      set mylock_xx 0
      set not_skip 0
      endif
      endif

      if %not_skip% == 1 then
      set mylock_x 0
      for %b in 0 1 2 3 4 5 6 7 8 9 A B C D E F
      if %myval_x% == %b then
      if %myval_xx% == F then
      set myval_xx 0
      set mylock_x 1
      set mylock_xx 0
      else
      for %a in 0 1 2 3 4 5 6 7 8 9 A B C D E F
      if %myval_xx% == %a then
      set mylock_xx 1
      else
      if %mylock_xx% == 1 then
      set mylock_xx 0
      set myval_xx %a
      set myval_x %b
      endif
      endif
      endfor
      endif
      else
      if %mylock_x% == 1 then
      set mylock_x 0
      set myval_x %b
      endif
      endif
      endfor
      endif

      echo "0x%myval_x%%myval_xx%"
      if %c == 4 then
      echo "t3.nsh %myval_x% %myval_xx%" > out.nsh
      endif
      endfor

      set -d mylock_x
      set -d mylock_xx
      echo -on
      ========================================
      Examples:
      輸入時英文請用大寫
      fs0:\> t3 F D
      t3> echo -off
      [[D]]
      [[F]]
      0xFE
      0xFF
      0x00
      0x01

      最後一組備存成out.nsh批次檔
      fs0:\> type out.nsh
      type: fs0:\out.nsh, Size 26

      t3.nsh 0 1

      引入out.nsh批次檔
      fs0:\> out.nsh
      out.nsh> t3.nsh 0 1
      +t3.nsh> echo -off
      [[1]]
      [[0]]
      0x02
      0x03
      0x04
      0x05

      fs0:\> out.nsh
      out.nsh> t3.nsh 0 5
      +t3.nsh> echo -off
      [[5]]
      [[0]]
      0x06
      0x07
      0x08
      0x09

      試試看吧!!!!!!!!!!!!!!!!!!!

      刪除
  3. 作者已經移除這則留言。

    回覆刪除
  4. Hi 想請問板主,
    我想要用一個add count 指令
    每次進入efi shell的時候 就去讀count.txt裡面的次數 並且把次數增加一次,
    想請問板主該怎麼執行?

    回覆刪除
    回覆
    1. 我找到解答了,分享一下。
      用dmpstore xxx -s xxx 去存variable
      然後再laod 回來即可
      Save
      dmpstore counter -s xxx

      Load
      dmpstore counter -l xxx

      刪除
  5. 作者已經移除這則留言。

    回覆刪除
  6. 您好,想請教一下
    請問能在.nsh裡面使用input這個指令嗎,又該如何使用呢?
    因為想嘗試讓使用者先輸入一個變數後,
    再用判斷式來決定要執行的指令動作。

    回覆刪除
    回覆
    1. 應該是沒有這樣的shell指令來支援,可能要直接寫程式來等待使用者輸入。

      Shell Example:

      echo %1
      if %1% == 9 then
      echo this is 9.
      endif

      刪除