如何在 BASH 中使用 GETOPTS 获取命令后的参数

发布于 2024-12-09 03:12:47 字数 425 浏览 0 评论 0原文

脚本使用示例

./myscript --p 1984 --n someName

#!/bin/bash

while getopts :npr opt 
do
   case $opt in
     n ) echo name= ???                ;;
     p ) echo port=  ???               ;;
     r ) echo robot= "Something"       ;;
     ? ) echo  "Useage: -p [#]"        ;;
  esac
done

如何访问命令选项后面的参数?

此外,如果我输入: ./myscript --p 1985 我想知道如何回显 1985 并处理该参数。

Example of script usage

./myscript --p 1984 --n someName

#!/bin/bash

while getopts :npr opt 
do
   case $opt in
     n ) echo name= ???                ;;
     p ) echo port=  ???               ;;
     r ) echo robot= "Something"       ;;
     ? ) echo  "Useage: -p [#]"        ;;
  esac
done

How to I access the argument following the command option?

Moreover, if i type: ./myscript --p 1985 I would like to know how ot echo 1985 back and work with that argument.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趁微风不噪 2024-12-16 03:12:47

在 bash 中,请参阅 help getopts:“当选项需要参数时,getopts 将该参数放入 shell 变量 OPTARG 中。”

usage() { echo "Usage: $(basename $0) -n name -p port -r"; exit; }

while getopts :n:p:r opt   # don't forget the colons for opts that take an arg
do
   case $opt in
     n ) name="$OPTARG" ;;
     p ) port="$OPTARG" ;;
     r ) robot=chicken  ;;
     ? ) usage ;;
  esac
done
shift $(( OPTIND - 1 ))

echo "the name is $name"
echo "the port is $port"

我确信您可以通过谷歌搜索来找到解析 bash 中选项的解决方案。这是几分钟的努力:

#!/bin/bash

usage() { echo foo; exit; }

while [[ $1 == -* ]]; do
  case "$1" in 
    --) shift 1; break ;;
    -p|--p|--port) port="$2"; shift 2;;
    -n|--n|--name) name="$2"; shift 2;;
    *) echo "unknown option: $1"; usage;;
  esac
done

echo "the name is $name"
echo "the port is $port"
echo "the rest of the args are:"; ( IFS=,; echo "$*" )

还有一个测试,

$ bash longopts.sh --port 1234 --bar a b c
unknown option: --bar
foo
$ bash longopts.sh --port 1234 a b c
the name is
the port is 1234
the rest of the args are:
a,b,c

In bash, see help getopts: "When an option requires an argument, getopts places that argument into the shell variable OPTARG."

usage() { echo "Usage: $(basename $0) -n name -p port -r"; exit; }

while getopts :n:p:r opt   # don't forget the colons for opts that take an arg
do
   case $opt in
     n ) name="$OPTARG" ;;
     p ) port="$OPTARG" ;;
     r ) robot=chicken  ;;
     ? ) usage ;;
  esac
done
shift $(( OPTIND - 1 ))

echo "the name is $name"
echo "the port is $port"

I'm sure you could google around for a solution to parse options in bash. Here's a couple minutes' effort:

#!/bin/bash

usage() { echo foo; exit; }

while [[ $1 == -* ]]; do
  case "$1" in 
    --) shift 1; break ;;
    -p|--p|--port) port="$2"; shift 2;;
    -n|--n|--name) name="$2"; shift 2;;
    *) echo "unknown option: $1"; usage;;
  esac
done

echo "the name is $name"
echo "the port is $port"
echo "the rest of the args are:"; ( IFS=,; echo "$*" )

And a test,

$ bash longopts.sh --port 1234 --bar a b c
unknown option: --bar
foo
$ bash longopts.sh --port 1234 a b c
the name is
the port is 1234
the rest of the args are:
a,b,c
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文