如何在getopt bash中使参数可选

时间:2015-03-23 23:44:15

标签: bash getopt

我想在getopt bash中将参数作为可选参数,这样如果用户没有指定它,那么它仍然可以在不杀死程序的情况下运行。我怎样才能做到这一点。这是我之前的代码

while getopts ":l:q:s:e:hg:" opt; do
  case $opt in
    l)
      lincRNAfasta=$OPTARG
    ;;
    q)
      query_species=$OPTARG
      ;;
    s)
      subject_species=$OPTARG
      ;;
    e)
      subject_gff=$OPTARG
      ;;
    h)
      echo "USAGE : open script in text editor"
      exit 1
      ;;
    g)
      subject_genome=$OPTARG
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

1 个答案:

答案 0 :(得分:1)

抱歉,我不得不更新我的回答,我误解了这个问题。

我正在查看支持getopts.c

::文档

但是,无论我是指定1,2或3个参数,还是以任何顺序,您所使用的代码都是有效的。

如果出现错误,一个原因可能是需要定义变量:

#! /bin/bash
lincRNAfasta=
query_species=
subject_species=
subject_gff=
subject_genome=

help='''
  USAGE : open script in text editor
    -l lincRNAfasta
    -q query_species
    -s subject_species
    -e subject_gff
    -g subject_genome
'''

while getopts ":l:q:s:e:hg:" opt; do
  case $opt in
    l)
      lincRNAfasta=$OPTARG
    ;;
    q)
      query_species=$OPTARG
      ;;
    s)
      subject_species=$OPTARG
      ;;
    e)
      subject_gff=$OPTARG
      ;;
    h)
      printf "$help"
      exit 1
      ;;
    g)
      subject_genome=$OPTARG
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

echo "doing something else"

if [ -z "$lincRNAfasta" ];then
    echo "its empty"
    echo "lincRNAfasta: $lincRNAfasta"
    echo
else
    echo "not empty"
    echo "lincRNAfasta: $lincRNAfasta"
    echo
fi

if [ -z "$query_species" ];then
    echo "its empty"
    echo "query_species: $query_species"
    echo
else
    echo "not empty"
    echo "query_species: $query_species"
    echo
fi

if [ -z "$subject_species" ];then
    echo "its empty"
    echo "subject_species: $subject_species"
    echo
else
    echo "not empty"
    echo "subject_species: $subject_species"
    echo
fi

if [ -z "$subject_gff" ];then
    echo "its empty"
    echo "subject_gff: $subject_gff"
    echo
else
    echo "not empty"
    echo "subject_gff: $subject_gff"
    echo
fi

if [ -z "$subject_genome" ];then
    echo "its empty"
    echo "subject_genome: $subject_genome"
    echo
else
    echo "not empty"
    echo "subject_genome: $subject_genome"
    echo
fi


# do something once variables have been set
# any variable not set will be empty

echo "doing something else"

输出:

bob@squids:~/Desktop$ ./1.sh -h

  USAGE : open script in text editor
    -l lincRNAfasta
    -q query_species
    -s subject_species
    -e subject_gff
    -g subject_genome
bob@squids:~/Desktop$ ./1.sh -s a -g h -e e -q q
doing something else
its empty
lincRNAfasta: 

not empty
query_species: q

not empty
subject_species: a

not empty
subject_gff: e

not empty
subject_genome: h

doing something else
bob@squids:~/Desktop$ ./1.sh -s a -g h -e e
doing something else
its empty
lincRNAfasta: 

its empty
query_species: 

not empty
subject_species: a

not empty
subject_gff: e

not empty
subject_genome: h

doing something else
bob@squids:~/Desktop$ ./1.sh -s a -e e -g 123
doing something else
its empty
lincRNAfasta: 

its empty
query_species: 

not empty
subject_species: a

not empty
subject_gff: e

not empty
subject_genome: 123

doing something else

或者不是将变量初始化为空,而是可以将它们初始化为类似" NOTSPECIFIEDONSTART"的字符串。在启动脚本时,您可以传递一个空字符串,如-g ''