while getopts ":pq:" optname do case "$optname" in "p") echo "Option $optname is specified" ;; "q") echo "Option $optname has value $OPTARG" ;; "?") echo "Unknown option $OPTARG" ;; ":") echo "No argument value
echo "Option $optname has value $OPTARG" ;; "?") echo "Unknown option $OPTARG" ;; ":") echo "No argument value for option $OPTARG" ;; *) # Should not occur echo "Unknown error while processing options" ;; esac echo "OPTIND is now $OPTIND" done getopts命令使用了两个预先确定的变量。
getopts命令(注意是复数)内建于bash shell,非常适用于解析命令行的所有参数 # Processing options & parameters with getoptsLENGTH=1500OUTPREFIX="output"whilegetoptshi:g:l:o: optdocase"$opt"in h)echo"Found the -h option"echo"Usage: bash$name-g genome_fasta -i input_gff -l 1500 -o out_prefix...
printf “$SCRIPT: %s\n”“Processing files for $COMPANY...” This script is shorterthan the positional 这个脚本比定位参数的脚本更短,如果getopts出错,switch语句会不运行。 作为一个特定的情况,如果提供getopts命令作为一个额外的参数,getopts能够处理这些变量而不是脚本参数,这样可以使用特定的参数来测试开关。
getopts 可以处理变量的格式 #!/bin/bash # echo while getopts :ab:c opt #选项前的:去掉错误信息,选项后的:用来表示有参数 do case "$opt" in a) echo "Found the -a option";; #注意,这里不需要 - 了 b) echo "Found the -b option,with value $OPTARG" ;; # $OPTARG 存储了那个参数变量 ...
printf “$SCRIPT: %s\n”“Processing files for $COMPANY...” This script is shorterthan the positional 这个脚本比定位参数的脚本更短,如果getopts出错,switch语句会不运行。 作为一个特定的情况,如果提供getopts命令作为一个额外的参数,getopts能够处理这些变量而不是脚本参数,这样可以使用特定的参数来测试开关。
echo "No argument value for option $OPTARG" ;; *) # Should not occur echo "Unknown error while processing options" ;; esac echo "OPTIND is now $OPTIND" done getopts命令使用了两个预先确定的变量。OPTIND 变量开始被设为 1。之后它包含待处理的下一个参数的索引。如果找到一个选项,则getopts命令返...
Finally, getopts can handle your command line options in any order. The only rule is that the file or files you are processing have to come after all of the option switches. options -d bar -c chu -b man -a foo example1.txt example2.txt ...
whilegetopts":h"opt;docase${opt}inh)echo"Usage:"echo" pip -h Display this help message."echo" pip install Install a Python package."exit0;;\?)echo"Invalid Option: -$OPTARG"1>&2exit1;;esacdoneshift$((OPTIND-1)) Now let’s add the sub-commandinstallto our script.installtakes as ...
while getopts ":hn:" option; do case $option in h) # display Help Help exit;; n) # Enter a name Name=$OPTARG;; \?) # Invalid option echo "Error: Invalid option" exit;; esac done $OPTARGis always the variable name used for each new option argument, no matter how many there are...