在 shell 脚本中获取存储在 OPTARG 变量中的值

get the values stored in OPTARG variable inside a shell script

我有一个脚本,它接受 3 个参数作为输入,然后继续 script.I 我正在使用 getopts 检查传递的参数,但我无法在我的脚本中获取传递参数的值。 任何人都可以检查这段代码并建议如何获取在我的脚本中传递的参数值(函数内部和外部函数)

while getopts ":s:a:c:" params
do
   case $params in
      s) name="$OPTARG" ;;
      a) value="OPTARG" ;;
      c) file="OPTARG" ;;
      ?) print_usage;;
   esac
done

当我尝试访问 $name、$value 和 $file 时,我的脚本总是打印我在脚本中的帮助信息,即 print_usage 内容

在此先感谢您的帮助

除了打字错误(OPTARG 中缺少 $ 符号),它对我来说工作正常:

print_usage() {
  echo "usage"
  exit
}

while getopts ":s:a:c:" params
do
   case $params in
      s) name="$OPTARG" ;;
      a) value="$OPTARG" ;;
      c) file="$OPTARG" ;;
      ?) print_usage;;
   esac
done

echo "name=$name, value=$value, file=$file"

然后

$ bash test.sh -s foo -a bar -c baz
name=foo, value=bar, file=baz

$ bash test.sh -z 
usage