如何在Bash包装器脚本中使用工具的所有命令行参数?

问题描述 投票:1回答:1

我有一个为工具编写的包装脚本,我只暴露了该工具的几个命令行参数,但我想知道如何在包装器脚本中获取工具的所有命令行参数?

这是我感兴趣的工具:

hisat2 [options]* -x <hisat2-idx> {-1 <m1> -2 <m2> | -U <r> | --sra-acc <SRA accession number>} [-S <hit>]

Options    
Input options
-q
Reads (specified with <m1>, <m2>, <s>) are FASTQ files. FASTQ files usually have extension .fq or .fastq. FASTQ is the default format. See also: --solexa-quals and --int-quals.    
--qseq
Reads (specified with <m1>, <m2>, <s>) are QSEQ files. QSEQ files usually end in _qseq.txt. See also: --solexa-quals and --int-quals.   
-f
Reads (specified with <m1>, <m2>, <s>) are FASTA files. FASTA files usually have extension .fa, .fasta, .mfa, .fna or similar. FASTA files do not have a way of specifying quality values, so when -f is set, the result is as if --ignore-quals is also set.    
-r
Reads (specified with <m1>, <m2>, <s>) are files with one input sequence per line, without any other information (no read names, no qualities). When -r is set, the result is as if --ignore-quals is also set.    
-c
The read sequences are given on command line. I.e. <m1>, <m2> and <singles> are comma-separated lists of reads rather than lists of read files. There is no way to specify read names or qualities, so -c also implies --ignore-quals.

在我的包装器脚本中,我能够公开hisat2工具的所有必需选项。

#!/bin/bash

usage() {
      echo ""
      echo "Usage : sh $0 -i Input_folder -l lib_type {-1 <left_reads> -2 <right_reads> | -U <single_reads> | -s <sra_id>} -S <output_sam> -p numb_threads"
      echo ""

cat <<'EOF'
  -i </path/to/input folder>

  -l Library type

  -1 </path/to/reads_1>

  -2 </path/to/reads_2>

  -U </path/to/single_reads>

  -S </path/to/sam output>

  -s SRA ID

  -p Number of threads

EOF
    exit 0
}

while getopts ":hi:l:1:2:U:S:s:p:" opt; do
  case $opt in
    i)
    input_folder=$OPTARG # Input folder
     ;;
    l)
     lib_type=$OPTARG # Library type
     ;;
    1)
    left_reads=$OPTARG # Left reads
     ;;
    2)
    right_reads=$OPTARG # Right reads
     ;;
    U)
    single_reads=$OPTARG # single end reads
     ;;
    S)
    sam_out=$OPTARG # Samoutput file
     ;;
    s)
    sra_id=$OPTARG # SRA ID
     ;;
    p)
    num_threads=$OPTARG # Number of threads
     ;;
    h)
    usage
     exit 1
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done


for i in $input_folder/*; do
    cp $i .
   fbname=$(basename "$i" .ht2 | cut -d. -f1)
done

if [ ! -z $left_reads ] && [ ! -z $right_reads ];
then
    hisat2 -x $fbname --rna-strandness $lib_type -1 $left_reads -2 $right_reads -S temp.sam -p $num_threads
    samtools view -bS temp.sam > $sam_out
    rm temp.sam
fi

当我查看可选参数时,有超过20个,是的,我可以在我的包装脚本中手动公开它们,但我想知道是否有办法以编程方式进行。

bash arguments
1个回答
1
投票

由于您编写包装器的命令有一组复杂的参数,我认为最好这样做:

  • 编写包装器,以便将其调用为: wrapper args -- args_for_the_wrapped_command
  • 抓住所有参数直到包装器中的--并将其余参数传递给包装命令,而不必担心这些参数实际意味着什么
  • 适当地检查包装命令的退出代码,以便我们捕获由错误参数引起的任何错误

这样,包装器不需要处理解析包装命令参数的复杂性。

像这样的东西:

#
# grab all arguments meant for the wrapper
#
declare -a args_for_wrapper
for arg in "$@"; do
  if [[ "$arg" == "--" ]]; then
    shift  # get rid off "--" itself
    break
  else
    # the argument consumption logic can be placed here as well
    # or collect the args into an array
    args_for_wrapper+=("$arg")
    shift
  fi
done

# consume wrapper's argument contained in the array

# call the wrapped command
wrapped_command "$@"

说得通?

但是,如果包装器应该为包装命令构造所有参数,则可能无法使用此方法,可选地将其与通过其自己的命令行接收的参数混合。

© www.soinside.com 2019 - 2024. All rights reserved.