例如如何在bash getopts的使用 [英] example of how to use getopts in bash

查看:91
本文介绍了例如如何在bash getopts的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打电话给我的bash脚本文件时获得输入参数。

我要打电话的MyScript 文件中是这样的:

  $ ./myscript -s 45 -p any_string

  $ ./myscript -h>>>应显示帮助
$ ./myscript>>>应显示帮助

我的要求是:


  • getopt的在这里得到的输入参数

  • 检查 -s 存在,如果不返回错误

  • 检查 -s 45或90
  • 后的值
  • 检查 -p 存在,并且有一个后输入字符串

  • 如果用户输入 ./的MyScript -h 或只是 ./的MyScript 则显示帮助

我试过到目前为止code:

 #!/斌/庆典
而getopts的H:S:ARG;做
  案例$ ARG在
    H)
      回声用法
      ;;
    S)
      强度= $ OPTARG
      回声$实力
      ;;
  ESAC
DONE

不过,与code我得到的错误。

如何使用bash做到这一点, getopt的


解决方案

 #!/斌/庆典使用(){回声用法:$ 0 -s< 45 | 90 GT;] [-p<串GT]1>&放大器; 2; 1号出口; }而getopts的S:号码:O;做
    案$ {O]在
        S)
            S = $ {} OPTARG
            ((S == 45 ||小号== 90))||用法
            ;;
        p)
            P = $ {} OPTARG
            ;;
        *)
            用法
            ;;
    ESAC
DONE
移位$((OPTIND-1))如果[-z$ {S}] || [-z$ {P}];然后
    用法
科幻回声S = $ {S}
回声P = $ {P}

运行示例:

  $ ./myscript.sh
用法:./myscript.sh [-S< 45 | 90 GT;] [-p<串GT]$ ./myscript.sh -h
用法:./myscript.sh [-S< 45 | 90 GT;] [-p<串GT]$ ./myscript.sh -s-p
用法:./myscript.sh [-S< 45 | 90 GT;] [-p<串GT]$ ./myscript.sh -s 10 -p富
用法:./myscript.sh [-S< 45 | 90 GT;] [-p<串GT]$ ./myscript.sh -s 45 -p富
S = 45
P = foo的$ ./myscript.sh -s 90 -p酒吧
S = 90
P =酒吧

I want to get the input argument when calling my script bash file.

I want to call myscript file in this way:

$ ./myscript -s 45 -p any_string

or

$ ./myscript -h >>> should display help
$ ./myscript    >>> should display help

My requirements are:

  • getopt here to get the input arguments
  • check that -s exists, if not return error
  • check that the value after the -s is 45 or 90
  • check that the -p exists and there is an input string after
  • if the user enters ./myscript -h or just ./myscript then display help

I tried so far this code:

#!/bin/bash
while getopts "h:s:" arg; do
  case $arg in
    h)
      echo "usage" 
      ;;
    s)
      strength=$OPTARG
      echo $strength
      ;;
  esac
done

But with that code I get errors.

How to do it with Bash and getopt?

解决方案

#!/bin/bash

usage() { echo "Usage: $0 [-s <45|90>] [-p <string>]" 1>&2; exit 1; }

while getopts ":s:p:" o; do
    case "${o}" in
        s)
            s=${OPTARG}
            ((s == 45 || s == 90)) || usage
            ;;
        p)
            p=${OPTARG}
            ;;
        *)
            usage
            ;;
    esac
done
shift $((OPTIND-1))

if [ -z "${s}" ] || [ -z "${p}" ]; then
    usage
fi

echo "s = ${s}"
echo "p = ${p}"

Example runs:

$ ./myscript.sh
Usage: ./myscript.sh [-s <45|90>] [-p <string>]

$ ./myscript.sh -h
Usage: ./myscript.sh [-s <45|90>] [-p <string>]

$ ./myscript.sh -s "" -p ""
Usage: ./myscript.sh [-s <45|90>] [-p <string>]

$ ./myscript.sh -s 10 -p foo
Usage: ./myscript.sh [-s <45|90>] [-p <string>]

$ ./myscript.sh -s 45 -p foo
s = 45
p = foo

$ ./myscript.sh -s 90 -p bar
s = 90
p = bar

这篇关于例如如何在bash getopts的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆