如何在 bash 脚本中添加 getopt 选项

how to add getopt options in a bash script

我写了一个 bash 脚本,它由多个 Unix 命令和 Python 脚本组成。目标是建立一个管道来检测来自特定输入的长链非编码 RNA。最后我想把它变成一个 'app' 并把它放在一些生物信息学网站上。我面临的一个问题是在 bash 中使用 getopt 工具。我找不到我清楚理解的好教程。此外,感谢任何其他与代码相关的评论。

#!/bin/bash
if [ "" == "-h" ]
then
    echo "Usage: sh [=11=] cuffcompare_output reference_genome blast_file"
    exit
else
wget https://github.com/TransDecoder/TransDecoder/archive/2.0.1.tar.gz && tar xvf 2.0.1 && rm -r 2.0.1
makeblastdb -in  -dbtype nucl -out .blast.out
grep '"u"'  | \
gffread -w transcripts_u.fa -g  - && \
python2.7 get_gene_length_filter.py transcripts_u.fa transcripts_u_filter.fa && \
TransDecoder-2.0.1/TransDecoder.LongOrfs -t transcripts_u_filter.fa
sed 's/ .*//' transcripts_u_filter.fa | grep ">" | sed 's/>//' > transcripts_u_filter.fa.genes
cd transcripts_u_filter.fa.transdecoder_dir
sed 's/|.*//' longest_orfs.cds | grep ">" | sed 's/>//' | uniq > longest_orfs.cds.genes
grep -v -f longest_orfs.cds.genes ../transcripts_u_filter.fa.genes > longest_orfs.cds.genes.not.genes
sed 's/^/>/' longest_orfs.cds.genes.not.genes > temp && mv temp longest_orfs.cds.genes.not.genes
python ../extract_sequences.py longest_orfs.cds.genes.not.genes ../transcripts_u_filter.fa longest_orfs.cds.genes.not.genes.fa
blastn -query longest_orfs.cds.genes.not.genes.fa -db ../.blast.out -out longest_orfs.cds.genes.not.genes.fa.blast.out -outfmt 6
python ../filter_sequences.py longest_orfs.cds.genes.not.genes.fa.blast.out longest_orfs.cds.genes.not.genes.fa.blast.out.filtered
grep -v -f longest_orfs.cds.genes.not.genes.fa.blast.out.filtered longest_orfs.cds.genes.not.genes.fa > lincRNA_final.fa
fi

我是这样运行的:

sh test.sh cuffcompare_out_annot_no_annot.combined.gtf /mydata/db/Brapa_sequence_v1.2.fa TE_RNA_transcripts.fa

如果您希望通话为:

test -c cuffcompare_output -r reference_genome -b blast_file

你会得到类似的东西:

#!/bin/bash

while getopts ":b:c:hr:" opt; do
  case $opt in
    b)
      blastfile=$OPTARG
      ;;
    c)
      comparefilefile=$OPTARG
      ;;
    h)
      echo "USAGE : test -c cuffcompare_output -r reference_genome -b blast_file"
      ;;
    r)
      referencegenome=$OPTARG
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

在字符串“:b:c:hr:”中, - 第一个“:”告诉 getopts 我们将处理任何错误, - 后面的字母是允许的标志。如果该字母后跟一个“:”,则 getopts 将期望该标志带有一个参数,并将该参数作为 $OPTARG

提供