我怎樣才能只印一次

我怎樣才能只印一次
#!/bin/bash

usage ()

 {

echo "run with 1st argument

       -mux2 or mux4 or mux8 or default(mux2) or all ( all the 3 mux)" 

echo "2nd argument 

      -struct or ifs or cases or asgn or default(struct) or all(all the 

       conditions)"

echo "3rd argument 

    -on (waveform) or off (no wave) or default(off)'
echo "run 

      - should take mux2 struct off as arguments"
}

if [ "$1" == "mux2" -o "$1" == "mux4" -o "$1" == "mux8" ]; then

if [ "$2" == "struct" -o "$2" == "ifs" -o "$2" == "cases" -o "$2"=="asgn" ]; then

  if [ "$3" == "on" ]; then

  iverilog -o mux "$1".v  "$1"TB.v -D "$2" 

  vvp mux

  gtkwave T.vcd

  elif [ "$3" == "off" -o "$3" == "" ]; then

  iverilog -o mux "$1".v  "$1"TB.v -D "$2" 

  vvp mux

  else

  usage

  fi

 elif [ "$2" == "all" ]; then

 $0 $1 struct $3

 $0 $1 ifs $3

 $0 $1 cases $3

 $0 $1 asgn $3

 elif [ "$2" =="" ]; then

 $0 $2 struct $3

 else

 usage

 fi

elif [ "$1" == "all" ]; then

$0 mux2 $2 $3

$0 mux4 $2 $3

$0 mux8 $2 $3

elif [ "$1" == "" ]; then

$0 mux2 stuct

else

usage

fi

當我使用以下參數運行腳本時,用法會多次顯示:

run all jhjjk
run all all kjkj

如何才能只列印一次?

答案1

要解析腳本中的參數和選項,我將使用獲取選擇。它提供您搜尋的功能。

但無論如何,要使腳本運行,請exit在此行後面添加:

echo "run 

      - should take mux2 struct off as arguments"
exit
}

答案2

之後有一個錯字default(off),你用的'"。除此之外,它對我來說效果很好。我註解掉了大部分指令,放入echos 進行調試。創建一個最小的範例通常很有用。有了這個,用作all第一個參數效果很好。這是您的原始腳本,經過"修復並具有更好的縮進,這使得調試更加容易。

#!/bin/bash

usage ()
 {
echo "run with 1st argument
  -mux2 or mux4 or mux8 or default(mux2) or all ( all the 3 mux)" 
echo "2nd argument 
  -struct or ifs or cases or asgn or default(struct) or all(all the conditions)"
echo "3rd argument
  -on (waveform) or off (no wave) or default(off)"
echo "run 
  - should take mux2 struct off as arguments"
}

if [ "$1" == "mux2" -o "$1" == "mux4" -o "$1" == "mux8" ]; then
  if [ "$2" == "struct" -o "$2" == "ifs" -o "$2" == "cases" -o "$2"=="asgn" ]; then
    if [ "$3" == "on" ]; then
      iverilog -o mux "$1".v  "$1"TB.v -D "$2" 
      vvp mux
      gtkwave T.vcd
    elif [ "$3" == "off" -o "$3" == "" ]; then
      iverilog -o mux "$1".v  "$1"TB.v -D "$2" 
      vvp mux
    else
      usage
    fi
  elif [ "$2" == "all" ]; then
    $0 $1 struct $3
    $0 $1 ifs $3
    $0 $1 cases $3
    $0 $1 asgn $3
  elif [ "$2" =="" ]; then
    $0 $2 struct $3
  else
    usage
  fi
elif [ "$1" == "all" ]; then
  $0 mux2 $2 $3
  $0 mux4 $2 $3
  $0 mux8 $2 $3
elif [ "$1" == "" ]; then
  $0 mux2 stuct
else
  usage
fi

這是我使用過的一個運行良好的版本。

#!/bin/bash

usage ()
 {
echo "run with 1st argument
  -mux2 or mux4 or mux8 or default(mux2) or all ( all the 3 mux)" 
echo "2nd argument 
  -struct or ifs or cases or asgn or default(struct) or all(all the conditions)"
echo "3rd argument
  -on (waveform) or off (no wave) or default(off)"
echo "run 
  - should take mux2 struct off as arguments"
}

if [ "$1" == "mux2" -o "$1" == "mux4" -o "$1" == "mux8" ]; then
  if [ "$2" == "struct" -o "$2" == "ifs" -o "$2" == "cases" -o "$2"=="asgn" ]; then
    if [ "$3" == "on" ]; then
#      iverilog -o mux "$1".v  "$1"TB.v -D "$2" 
#      vvp mux
#      gtkwave T.vcd
echo 1
    elif [ "$3" == "off" -o "$3" == "" ]; then
#      iverilog -o mux "$1".v  "$1"TB.v -D "$2" 
#      vvp mux
echo 2
    else
      usage
    fi
  elif [ "$2" == "all" ]; then
#    $0 $1 struct $3
#    $0 $1 ifs $3
#    $0 $1 cases $3
#    $0 $1 asgn $3
echo 3
  elif [ "$2" =="" ]; then
#    $0 $2 struct $3
echo 4
  else
    usage
  fi
elif [ "$1" == "all" ]; then
#  $0 mux2 $2 $3
#  $0 mux4 $2 $3
#  $0 mux8 $2 $3
echo 5
elif [ "$1" == "" ]; then
#  $0 mux2 stuct
echo 6
else
  usage
fi

run all jhjjk並且都按預期run all all kjkj返回。5


編輯

擺脫遞歸地獄的最簡單方法是

$0 mux2 $2 $3
$0 mux4 $2 $3
$0 mux8 $2 $3

就是把每組命令做成一個子程式。然後,使用分支結構來呼叫這些子程序,如果沒有意義,則列印出用法。 IMO 遞歸對於這個任務來說太混亂了。

答案3

我更喜歡使用函數,但是當你必須使用遞歸解決方案時:

#!/bin/bash  
usage()
{
         echo Usage without recursive check
         if [ "${USAGE_PRINTED}" = "notyet" ]; then
                 echo Usage xxxx
                 export USAGE_PRINTED="Yes the operator already understands it"
         else
                 echo "ok"

         fi
}

if [ -z "${USAGE_PRINTED}" ]; then
         export USAGE_PRINTED="notyet"
fi

if [ $# -gt 1 ]; then
         echo Parameters $*
         usage
         shift
         $0 $*
else
         echo Parameters $*
         usage
fi

使用參數 1 2 3 4 執行程式時的輸出:

Parameters 1 2 3 4
Usage without recursive check
Usage xxxx
Parameters 2 3 4
Usage without recursive check
ok
Parameters 3 4
Usage without recursive check
ok
Parameters 4
Usage without recursive check
ok

帶有“無需遞歸檢查的用法”的行演示了問題,移動參數是為了在幾步後結束遞歸調用。回顯“ok”可以刪除。

相關內容