[英]How can I pass more than 10 arguments in windows batch file or linux shell script
我需要將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
任何人都可以為此解決問題
您可以使用%*
獲取所有參數並使用for
循環解析它們:
for %%i in (%*) do echo %%i
注意:您的參數可能不包含某些字符(例如“標准分隔符”) ,
或;
除非你引用它們:
mybatch.bat first second "this is the third" "four, not five" five 6 7 8 9 ...
基本上, %10
被解釋為%1
0
。
要解決這個問題,可以在批處理文件或shell腳本中,將第一個參數保存在變量中,然后使用shift
將所有剩余參數遞減1.當您調用shift
, %1
(shell腳本中為$1
)為現在不見了,舊%2
變為%1
,舊%3
變為%2
等。有關更多詳細信息,請參閱此答案 。
在shell腳本中,您還可以使用${10}
引用第十個參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.