shell 之 getopt

本文深入解析了getopt在Shell脚本中的使用方法,包括其三种格式的语法和参数解析技巧。探讨了如何通过getopt处理短选项、长选项、带值选项及可选参数,为编写高效、易读的Shell脚本提供了实用指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

格式一

getopt optstring parameters

# This is correct
getopt "hv:t::" "-v 123 -t123"  
getopt "hv:t::" "-v123 -t123"  # -v and 123 doesn't have whitespace

# -h takes no value.
getopt "hv:t::" "-h -v123"

# This is wrong.
# -t can't have whitespace.
# Only optional params cannot have whitespace between key and value
getopt "hv:t::" "-v 123 -t 123"

# Multiple arguments that takes value.
getopt "h:v:t::g::" "-h abc -v 123 -t21"

# Multiple arguments without value
# All of these are correct
getopt "hvt" "-htv"
getopt "hvt" "-h -t -v"
getopt "hvt" "-tv -h"


Here h,v,t are the options and -h -v -t is how options should be given in command-line.
'h' is a no-value option. 无值的选项
'v:' implies that option -v has value and is a mandatory option. ':' means has a value. 带值的选项
't::' implies that option -t has value but is optional. '::' means optional. 选项带值可选

In optional param, value cannot have whitespace separation with the option. So, in "-t123" example, -t is option 123 is value.

这里要特别注意,选项带值可选时传值的时候没有空格!!!

格式二

getopt [getopt_options] [--] [optstring] [parameters]
说明参数支持的长形式和短形式。

The getopt_options, it describes how to parse the arguments. single dash long options, double dash options.
--, separates out the getopt_options from the options you want to parse and the allowed short options
The short options, is taken immediately after -- is found. Just like the Form first syntax.
The parameters, these are the options that you have passed into the program.
The options you want to parse and get the actual values set on them.

example:
  getopt -l "name:,version::,verbose" -- "n:v::V" "--name=Karthik -version=5.2 -verbose"

格式三

getopt [getopt_options] [-o options] [--] [optstring] [parameters]

getopt -l "name:,version::,verbose" -a -o "n:v::V" -- "-name=Karthik -version=5.2 -verbose"

GETOPT_OPTIONS

getopt_options changes the way command-line params are parsed.
Below are some of the getopt_options.

-l, --longoptions:
参数名的长形式,用逗号隔开
getopt "name:,version" "--name=George"

-a, --alternative
允许长形式参数名只带一个'-'
getopt "name:,version" "-name=George"

以下是一个来自网上的样例

#!/bin/bash

# filename: commandLine.sh
# author: @theBuzzyCoder

showHelp()
{
	# `cat << EOF` This means that cat should stop reading when EOF is detected
	cat << EOF  
	Usage: ./installer -v <espo-version> [-hrV]
	Install Pre-requisites for EspoCRM with docker in Development mode
	-h, -help,          --help                  Display help
	-v, -espo-version,  --espo-version          Set and Download specific version of EspoCRM
	-r, -rebuild,       --rebuild               Rebuild php vendor directory using composer and compiled css using grunt
	-V, -verbose,       --verbose               Run script in verbose mode. Will print out each step of execution.
	EOF
	# EOF is found above and hence cat command stops reading. This is equivalent to echo but much neater when printing out.
}

export version=0
export verbose=0
export rebuilt=0

# $@ is all command line parameters passed to the script.
# -o is for short options like -v
# -l is for long options with double dash like --version
# the comma separates different long options
# -a is for long options with single dash like -version
options=$(getopt -l "help,version:,verbose,rebuild,dryrun" -o "hv:Vrd" -a -- "$@")

# set --:
# If no arguments follow this option, then the positional parameters are unset.
# Otherwise, the positional parameters are set to the arguments, even if some of them begin with a ‘-’.
eval set -- "$options"

while true
do
case $1 in
-h|--help) 
    showHelp
    exit 0
    ;;
-v|--version) 
    shift
    export version=$1
    ;;
-V|--verbose)
    export verbose=1
    set -xv  # Set xtrace and verbose mode.
    ;;
-r|--rebuild)
    export rebuild=1
    ;;
--)
    shift
    break;;
esac
shift
done
# With short options grouped together and long option
# With double dash '--version'
bash commandLine.sh --version=1.0 -rV

# With short options grouped together and long option
# With single dash '-version'
bash commandLine.sh -version=1.0 -rV

# OR with short option that takes value, value separated by whitespace by key
bash commandLine.sh -v 1.0 -rV

# OR with short option that takes value, value without whitespace separation from key.
bash commandLine.sh -v1.0 -rV

# OR Separating individual short options
bash commandLine.sh -v1.0 -r -V
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值