我正在尝试使用bash代码将一些python代码放在一起,并希望能够提供多个选项,这些选项接受您可以通过命令行输入的值。为此,我试图使用getopts,但我遇到了尝试设置默认值的问题。
我已经在网上查看了这个问题并且之前已经提出了类似的问题,我试图在主要的getopts代码之后使用冒号选项等答案并在之前放置一个默认值,这导致了下面的代码:
!/bin/bash
usage() { echo "Usage: $0 [-f <string] [-r <string>] [-t <string>] [-o <string>] [-s <5|10> [-e <1|2|3>] [-b <string>] [-q <string>]]" 1>&2; exit 1; }
q = "30"
b = "15"
while getopts ":f:r:t:o:s:e:b:q:" p; do
case "${p}" in
f)
f=${OPTARG}
;;
r)
r=${OPTARG}
;;
t)
t=${OPTARG}
;;
o)
o=${OPTARG}
;;
s)
s=${OPTARG}
((s == 5 || s == 10)) || usage
;;
e)
e=${OPTARG}
((e == 1 || e == 2 || e == 3)) || usage
;;
b)
b=${OPTARG}
;;
q)
q=${OPTARG}
;;
*)
usage
;;
esac
done
: ${q = 30}
: ${b = 15}
shift $((OPTIND-1))
if [ -z "${f}" ] || [ -z "${r}" ] || [ -z "${t}" ] || [ -z "${o}" ] || [ -z "${s}" ] || [ -z "${e}" ] || [-z "${b}"] || [-z "${q}"]; then
usage
fi
b2 = ${b} + 2
echo "The following are your options."
echo "reverse read file = ${r}"
echo "forward read file = ${f}"
echo "type of file = ${t}"
echo "output identifier = ${o}"
echo "number of splits wanted (s) = ${s}"
echo "You have chosen to complete ${e} rounds of joining"
echo "Your have chosen that a consensus region of ${b} bases is needed for a match to be made."
if ["${e}" == 3]; then
echo " For quality trimming you have select threshold cut off at ${q}"
fi
如上所述,我以前从未使用过getopts,如果有人能解释我如何纠正我的代码以设置b和q选项的默认值,我将不胜感激。
答案 0 :(得分:1)
在shell分配中=
周围没有空格!
q = "30"
q="30"
错误的代码执行带有参数q
和=
的命令30
。这不是预期的目的。
与:相似:
: ${q = 30}
应该是:
: ${q=30}
和相关但有些不同:
b2 = ${b} + 2
可以是:
b2=$((${b} + 2))
b2="${b} + 2"
(或许多次要变体),但请注意,这些结果会有很大差异。