我怎样才能只打印一次

我怎样才能只打印一次
#!/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”可以删除。

相关内容