簡體   English   中英

Shell腳本循環命令行開關和參數

[英]Shell script to loop command line switches and arguments

我正在嘗試編寫一個bash腳本,該腳本將檢查命令行開關並在switchs參數中使用下一個參數。

例:

sh myprogram.sh -s switchArg -p programArg start

在我的腳本中,我試圖以這種方式循環它:

if [ "$#" -gt 2 ]
then
{
    i=1
    for arg in "$@"
    do
      if [ "$arg" == "-s" ] || [ "$arg" == "-S" ]
      then
      {
          i=$((i++))
          myArg=$i  # This then gets used later in my script
          continue
      }
      elif [ "$arg" == "-p" ] || [ "$arg" == "-P" ]
      then
      {
         i=$((i++))
         myArg2=$i  # This then gets used later in my script
         continue
      }
      else
      {
         echo "illegal option: $arg"
      }
   done

   do stuff
}
fi

我如何才能檢測到一個開關,並且不管開關的數量如何,都將下一個arg用於開關的參數?

您可以使用“ shift”將刪除第一個參數。
例如:

arg1=$1; shift
arg2=$1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM