当前位置:   article > 正文

cmd 分支选择示例 、函数定义和用法_cmd 函数

cmd 函数

 

 本文主要讲述如下几个问题:

   1.什么是函数,怎么创建函数?

   2.怎么调用一个函数?

   3.函数是怎么工作的?

   4.怎么向函数传递参数?

   5.函数怎么返回值和返回一个局部变量的值。

一、创建函数(什么是函数)

    在batch script 中的函数以一个标签开始,并以goto:eof结束,如下:

 script

 
  1. :myDosFunc - 函数的开始,用一个标签标识

  2. echo. 函数体,可以执行很多命令

  3. echo.

  4. GOTO:EOF

二、调用函数

Script: 01.
 call:myDosFunc

 

三、函数怎么工作

  调用函数的脚本将其分成两部分。

    1.main script: 从第一行开始并且以 GOTO:EOF命令结束

    2.函数部分:由多个函数组成,由main script调用。

   

   SCRIPT:

 
  1. @echo off

  2. echo.开始调用函数

  3. call:myDosFunc

  4. echo.从函数返回myDosFunc

  5. echo.&pause&goto:eof

  6. ::--------------------------------------------------------

  7. ::-- 函数部分开始

  8. ::--------------------------------------------------------

  9. :myDosFunc - here starts my function identified by it`s label

  10. echo. here the myDosFunc function is executing a group of commands

  11. echo. it could do a lot of things

  12. goto:eof


 

三、怎么传递参数,并且在函数中获取参数的值

   1.用空格或者逗号将参数分开

   2.用双引号将带有空格的字符串参数括起来

 
  1. call:myDosFunc 100 YeePEE

  2. call:myDosFunc 100 "for me"

  3. call:myDosFunc 100,"for me"

 

   获取参数,采用%1~%9来获取每个参数的值。%0,表示批处理文件本身

 
  1. :myDosFunc - here starts myDosFunc identified by it`s label

  2. echo.

  3. echo. here the myDosFunc function is executing a group of commands

  4. echo. it could do %~1 of things %~2.

  5. goto:eof

 

 带参数的脚本

 
  1. @echo off

  2. echo.going to execute myDosFunc with different arguments

  3. call:myDosFunc 100 YeePEE

  4. call:myDosFunc 100 "for me"

  5. call:myDosFunc 100,"for me"

  6. call:myDosFunc 100,for me

  7. echo.&pause&goto:eof

  8. ::--------------------------------------------------------

  9. ::-- Function section starts below here

  10. ::--------------------------------------------------------

  11. :myDosFunc - here starts my function identified by it's label

  12. echo.

  13. echo. here the myDosFunc function is executing a group of commands

  14. echo. it could do %~1 of things %~2.

  15. goto:eof


四、函数返回值

    
   1、调用命令不像其他语言那样能有返回值,最常用的做法是在函数中将该值保存在全局变量中,调用结束后

直接用该全局变量。如下:

  Usage:

 
  1. set "var1=some hopefully not important string"

  2. echo.var1 before: %var1%

  3. call:myGetFunc

  4. echo.var1 after : %var1%

Script:

 
  1. :myGetFunc - get a value

  2. set "var1=DosTips"

  3. goto:eof

 

脚本输出如下:

var1 before: some hopefully not important string
var1 after : DosTips

  2、通过引用返回值,调用者通过传递一个变量给函数来存储返回值

Usage:

 
  1. call:myGetFunc var1

  2. echo.var1 after : %var1%

 

Script:

 
  1. :myGetFunc - passing a variable by reference

  2. set "%~1=DosTips"

  3. goto:eof

脚本输出如下:

var1 after : DosTips
 

完整脚本:

 
  1. @echo off

  2. set "var1=CmdTips"

  3. echo.var1 before: %var1%

  4. call:myGetFunc var1

  5. echo.var1 after : %var1%

  6. echo.&pause&goto:eof

  7.  
  8. ::--------------------------------------------------------

  9. ::-- Function section starts below here

  10. ::--------------------------------------------------------

  11. :myGetFunc - passing a variable by reference

  12. set "%~1=DosTips"

  13. goto:eof


五、函数的局部变量

   怎么保证局部变量和全局变量不冲突,SETLOCAL命令能让处理器当做是局部变量,用ENDLOCAL解除局部变量。

ENDLOCAL 会被自动调用,当批处理执行到文件末尾的时候,即GOTO:EOF。SETLOCAL可以很好的保护函数内与外面的变量不会冲突。

 

 
  1. @echo off

  2. set "aStr=Expect no changed, even if used in function"

  3. set "var1=No change for this one. Now what?"

  4. echo.aStr before: %aStr%

  5. echo.var1 before: %var1%

  6. call:myGetFunc var1

  7. echo.aStr after : %aStr%

  8. echo.var1 after : %var1%

  9. echo.&pause&goto:eof

  10. ::--------------------------------------------------------

  11. ::-- Function section starts below here

  12. ::--------------------------------------------------------

  13. :myGetFunc - passing a variable by reference

  14. SETLOCAL

  15. set "aStr=DosTips"

  16. set "%~1=%aStr%"

  17. ENDLOCAL

  18. goto:eof

 

脚本输出:

aStr before: Expect no changed, even if used in function
var1 before: No change for this one.  Now what?
aStr after : Expect no changed, even if used in function
var1 after : No change for this one.  Now what?

   返回局部变量

      ----怎么跳过ENDLOCAL的屏障,返回局部变量值?

   采用”变量扩充“,在SETLOCAL与ENDLOCAL之间的全局变量的值会备份,当退出ENDLOCAL,该值将恢复。让命令处理器来执行ENDLOCAL 和SET命令。

 

 
  1. @echo off

  2. set "aStr=Expect no changed, even if used in function"

  3. set "var1=Expect changed"

  4. echo.aStr before: %aStr%

  5. echo.var1 before: %var1%

  6. call:myGetFunc var1

  7. echo.aStr after : %aStr%

  8. echo.var1 after : %var1%

  9. echo.&pause&goto:eof

  10. ::--------------------------------------------------------

  11. ::-- Function section starts below here

  12. ::--------------------------------------------------------

  13. :myGetFunc - passing a variable by reference

  14. SETLOCAL

  15. set "aStr=DosTips"

  16. ( ENDLOCAL

  17. set "%~1=%aStr%"

  18. )

  19. goto:eof

  20. :myGetFunc2 - passing a variable by reference

  21. SETLOCAL

  22. set "aStr=DosTips"

  23. ENDLOCAL&set "%~1=%aStr%" &rem THIS ALSO WORKS FINE

  24. goto:eof



脚本输出:

aStr before: Expect no changed, even if used in function
var1 before: Expect changed
aStr after : Expect no changed, even if used in function
var1 after : DosTips

六、编写递归函数

     让函数局部变量的变换对调用者是可见的,循环调用函数,让变量可重用。下面编写一个函数计算Fibonacci数列。

 
  1. @echo off

  2. set "fst=0"

  3. set "fib=1"

  4. set "limit=1000000000"

  5. call:myFibo fib,%fst%,%limit%

  6. echo.The next Fibonacci number greater or equal %limit% is %fib%.

  7. echo.&pause&goto:eof

  8.  
  9. ::--------------------------------------------------------

  10. ::-- Function section starts below here

  11. ::--------------------------------------------------------

  12. :myFibo -- calculate recursively the next Fibonacci number greater or equal to a limit

  13. :: -- %~1: return variable reference and current Fibonacci number

  14. :: -- %~2: previous value

  15. :: -- %~3: limit

  16. SETLOCAL

  17. set /a "Number1=%~1"

  18. set /a "Number2=%~2"

  19. set /a "Limit=%~3"

  20. set /a "NumberN=Number1 + Number2"

  21. if /i %NumberN% LSS %Limit% call:myFibo NumberN,%Number1%,%Limit%

  22. (ENDLOCAL

  23. IF "%~1" NEQ "" SET "%~1=%NumberN%"

  24. )

  25. goto:eof

 

七、总结,定义一个标准的dos batch script function

 

 
  1. :myFunctionName -- function description here

  2. :: -- %~1: argument description here

  3. SETLOCAL

  4. REM.--function body here

  5. set LocalVar1=...

  6. set LocalVar2=...

  7. (ENDLOCAL & REM -- RETURN VALUES

  8. IF "%~1" NEQ "" SET %~1=%LocalVar1%

  9. IF "%~2" NEQ "" SET %~2=%LocalVar2%

  10. )

  11. GOTO:EOF


原文链接:https://blog.csdn.net/xiaoding133/article/details/39252357

  1. @echo off
  2.  
  3. set serverDir=\\10.80.3.252\测试专用(勿动)\packtool_v4.0\config
  4. REM set serverDir=C:\Users\wangzhongyuan\Desktop\tmp2\config
  5. set localDir=D:\sci\ADT\单机工具
  6.  
  7.  
  8. echo -----------------------------------------------
  9. echo 单机工具配置文件更新
  10.  
  11. :Step1
  12. echo -----------------------------------------------
  13. echo 请选择要操作的文件
  14. set /p choice2=1、ApkUtility  2、BillingGrab  3、KeyWord_Search  4、PackingTool  5、退出 :
  15.  
  16.  
  17. :Step1_Next
  18. if %choice2%==1 goto ApkUtility
  19. if %choice2%==2 goto BillingGrab
  20. if %choice2%==3 goto ApkUtility
  21. if %choice2%==4 goto BillingGrab
  22. if %choice2%==5 goto end
  23.  
  24.  
  25. REM 设置文件路径参数
  26. :ApkUtility 
  27. set FileName=ApkUtility_config.txt
  28. set sourceFile=%serverDir%\%FileName%
  29. set targetFile1=%localDir%\ApkUtility\src\%FileName%
  30. set targetFile2=%localDir%\ApkUtility\ApkUtility_tools\%FileName%
  31. goto Step2
  32.  
  33. :BillingGrab
  34. set FileName=BillingGrab_config.txt
  35. set sourceFile=%serverDir%\%FileName%
  36. set targetFile1=%localDir%\BillingGrab\src\%FileName%
  37. set targetFile2=%localDir%\BillingGrab\BillingGrab_tools\%FileName%
  38. goto Step2
  39.  
  40. :KeyWord_Search
  41. set FileName=KWSearch_config.txt
  42. set sourceFile=%serverDir%\%FileName%
  43. set targetFile1=%localDir%\KeyWord_Search\src\%FileName%
  44. set targetFile2=%localDir%\KeyWord_Search\KWSearch_tools\%FileName%
  45. goto Step2
  46.  
  47. :PackingTool
  48. set FileName=PackingTool_config.txt
  49. set sourceFile=%serverDir%\%FileName%
  50. set targetFile1=%localDir%\PackingTool\src\%FileName%
  51. set targetFile2=%localDir%\PackingTool\tools\%FileName%
  52. goto Step2
  53.  
  54.  
  55. :Step2
  56. if i==2 goto Step1_Next
  57. echo -----------------------------------------------
  58. echo 请选择要执行的操作
  59. set /p choice1=1、下载配置文件到本地 2、上传本地配置文件 :
  60. echo -----------------------------------------------
  61.  
  62. :Step2_Next
  63. if %choice1%==1 goto download
  64. if %choice1%==2 goto upload
  65.  
  66. :download
  67. echo 下载配置文件到本地...
  68. if exist %sourceFile% echo 复制文件 %sourceFile% 到 %targetFile1%
  69. if exist %sourceFile% copy /Y %sourceFile% %targetFile1%
  70. if exist %sourceFile% echo 复制文件 %sourceFile% 到 %targetFile2%
  71. if exist %sourceFile% copy /Y %sourceFile% %targetFile2%
  72. goto Step1
  73.  
  74. :upload
  75. echo 上传本地配置文件...
  76. if exist %targetFile1% echo 复制文件 %targetFile1% 到 %sourceFile%
  77. if exist %targetFile1% copy /Y %targetFile1% %sourceFile%
  78. if exist %targetFile1% echo 复制文件 %targetFile1% 到 %targetFile2%
  79. if exist %targetFile1% copy /Y %targetFile1% %targetFile2%
  80. goto Step1
  81.  
  82.  
  83. REM 退出
  84. :end
  85. exit
  86.  
  87.  
  88. 原文:https://blog.csdn.net/scimence/article/details/52808855

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/450328
推荐阅读
相关标签
  

闽ICP备14008679号