检查传递给 Bash 脚本的标志和过程值

Check for flag passed to a Bash script and process value

我有一个 bash 脚本,我想在其中检查是否向其传递了带有值的标志,然后在变量中使用该值。像这样(伪代码):

file.sh -c 1.0.0

里面 file.sh :

#!/bin/bash

get flag:
if flag 'c' then 
curl c
else 
curl 'something else'

执行上述操作的最佳方法是什么?

尝试以下方法

#!/bin/bash


while getopts ":c" opt; do
 case $opt in
 c)
    echo "-c was triggered!" >&2
    ;;
 \?)
    echo "Invalid option: -$OPTARG" >&2
    ;;
 esac
done