它实际上并不在getopts中,但是如果变量是空的或者不是空的,您可以让shell以不同的方式展开变量。
i) SOURCE_SNAPSHOT_ID="${OPTARG:-yourdefaultvalue}"
或者,您可以只检查OPTARG是否为空并继续,或者在整个循环之后设置默认值,例如,其中任何一个都将设置SOURCE_SNAPSHOT_ID,如果且仅当它之前为空
: ${SOURCE_SNAPSHOT_ID:=yourdefaultvalue}
SOURCE_SNAPSHOT_ID=${SOURCE_SNAPSHOT_ID:-yourdefaultvalue}
有关这种变量用法的更多信息,请参见bash手册的“参数扩展”(仅引用我使用的两个变量):
${parameter:-word}
Use Default Values. If parameter is unset or null, the expansion of word is substituted.
Otherwise, the value of parameter is substituted.
${parameter:=word}
Assign Default Values. If parameter is unset or null, the expansion of word is assigned to
parameter. The value of parameter is then substituted. Positional parameters and special
parameters may not be assigned to in this way.