【问题标题】:How can I pass more than 10 arguments in windows batch file or linux shell script如何在 Windows 批处理文件或 linux shell 脚本中传递超过 10 个参数
【发布时间】:2015-05-15 12:07:33
【问题描述】:

我需要将超过 10 个参数传递给单个批处理文件(shell 脚本) 但是在第 9 个参数之后它将不起作用(它将从头开始)

代码示例

 echo Hello! This a sample batch file.
    echo %1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11 %12 %13 %14 %15 %16
    pause



>mybatchdotbat a b c d e f g h i j  k l m n o p

谁能解决这个问题

【问题讨论】:

  • 我怀疑问题在于 %10 被读取为 %1 0。在 Unix shell 中,您可以使用 ${10} 引用第十个参数,或使用 shift 删除第一个参数并将所有后续参数减 1(在循环中很有用)。

标签: windows shell batch-file cmd


【解决方案1】:

基本上,%10 被解释为 %1 0

要解决此问题,在批处理文件或 shell 脚本中,您可以将第一个参数保存在变量中,然后使用 shift 将所有剩余参数减 1。当您调用 shift 时,%1 (shell 脚本中的$1)现在消失了,旧的%2 变成了%1,旧的%3 变成了%2,等等。更多详细信息请参见this answer

在 shell 脚本中,您还可以使用 ${10} 引用第十个参数。

【讨论】:

    【解决方案2】:

    您可以使用%* 获取所有参数并使用for 循环解析它们:

    for %%i in (%*) do echo %%i
    

    注意:您的参数可能不包含某些字符(“标准分隔符”),例如 ,;,除非您引用它们:

    mybatch.bat first second "this is the third" "four, not five" five 6 7 8 9 ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-08
      • 2023-04-09
      • 2018-04-10
      • 2014-02-28
      • 2013-01-05
      • 1970-01-01
      • 2021-06-04
      • 1970-01-01
      相关资源
      最近更新 更多