这是本人第一次写博客,之前从一些开源技术网站上看到不少大牛和前辈们的文章,从中学习受益。本着开源界的奉献和学习精神,觉得有必要将自己的学习成果拿出来与大家一起交流分享,既当作是一种自我学习的总结也可能帮助到有需要的人,因此发表了该篇博客。本文主要讲述在shell中getopts命令的使用以及本人写的一个脚本小案例。由于本人技术水平和能力有限,文章中的命令说明部分主要来自于help getopts的翻译,同时会带有一些本人的主观意识,如有任何描述不对的地方,请大家批评指正。

在日常工作中写shell脚本,往往会涉及到一些命令行选项和参数的解析,这个解析过程如果完全由自己写代码去控制实现将会有一定困难,这里给大家介绍一个命令解析工具getopts。getopts是bash shell的内建命令,作用是在shell脚本中解析由命令行传递的选项和参数(也可能传递给函数、另一个被调用的shell脚本的位置参数,关于选项或参数,后面会讲解)。getopts只支持短选项,若要解析长选项请参考getopt。

getopts命令语法:

getopts optstring name [arg]

相关的术语:

选项(option):GNU风格的命令选项,如:-x,-y等减号加上单个字母的为短选项;--help为长选项;

选项的参数:某些选项之后必须尾随参数,如:-f xxx.conf,xxx.conf称为选项的参数,但-f和xxx.conf之间的空格不是必须的;

参数:位于最后一个选项,空格之后的字符串;

optionstring:用于匹配option的字符串,optionstring中每个字母对应的是去除减号后的option(这里指短选项);

[arg]:用来替代位置参数被getopts解析的,类似于将$@赋值给arg;

OPTIND: getopts的一个内置变量,表示下一个要处理的参数索引,每次shell或shell脚本执行的时候初始化为1;

name: getopts每解析到一个选项,都会将该选项对应的字符复制给name变量.

getopts命令的简单解释:

1.optstring中如果字母后面跟随着冒号,表示该字母所表示的option后面是带参数的,参数值将被赋到OPTARG变量中。如test.sh中包含:getopts "i:" name; 那么在命令行执行test.sh -i 1.1.1.1时,$name='i',$OPTARG=1.1.1.1

getopts包含两种错误处理的模式

安静模式下:

2.optstring的第一个字符为冒号时,getopts将使用安静模式来报告错误(但不会有错误提示输出),生产环境中一般使用该模式;

3.如果输入一个无效的字符作为选项时(optstring中没包含的字符),将‘?’赋值给name变量,该无效选项的字符赋值给OPTARG变量;

4.如果选项要求跟随的参数没找到,getopts会将冒号":"赋值给name变量,设置OPTARG为发现的选项字符;

非安静模式下:

5.如果getopts不处于安静模式,一个无效的选项被发现,'?'将被设置到name变量中且unset OPTARG;

6.如果选项要求的参数没找到,'?'将被设置到name,OPTARG将被unset,错误提示信息将被打印.

退出状态:

7.如果shell中$OPTERR环境变量值为0,getopts将关闭错误信息的打印,即使optstring中第一个字符不是冒号。默认的$OPTERR值为1;

8.getopts一般情况下解析位置参数($0-$9),但位置参数超过这个范围时仍然能被正确解析;

9.如果选项被发现(匹配optionstring)返回true,如果遇到了选项的结尾或者有错误则返回false.

脚本示例:

1. 不带可选参数[arg]

#!/bin/bash

echo "OPTIND starts at $OPTIND"

while getopts ":q:p" optname

do

case "$optname" in

"p")

echo "Option $optname is specified"

;;

"q")

echo "Option $optname has value $OPTARG"

#shift 1

shift $((OPTIND-2))

;;

"?")

echo "Unknown option $OPTARG"

;;

":")

echo "No argument value for option $OPTARG"

;;

*)

# Should not occur

echo "Unknown error while processing options"

;;

esac

echo "OPTIND is now $OPTIND"

done

执行结果:

[root@dns1 ~]# bash test_getopts.sh -q aa -p -h

OPTIND starts at 1

Option q has value aa

OPTIND is now 3

Unknown option h

OPTIND is now 4

2. 带可选参数[arg]

#!/bin/bash

echo "OPTIND starts at $OPTIND"

while getopts ":q:p" optname "-q qarg" "-p" "-h"

do

case "$optname" in

"p")

echo "Option $optname is specified"

;;

"q")

echo "Option $optname has value $OPTARG"

;;

"?")

echo "Unknown option $OPTARG"

;;

":")

echo "No argument value for option $OPTARG"

;;

*)

# Should not occur

echo "Unknown error while processing options"

;;

esac

echo "OPTIND is now $OPTIND"

done

执行结果:

[root@dns1 ~]# bash test_getopts.sh

OPTIND starts at 1

Option q has value qarg

OPTIND is now 2

Option p is specified

OPTIND is now 3

Unknown option h

OPTIND is now 4

3. 实现一个添加yum源的脚本

#!/bin/bash

#

# Descrition: this script will download the yum repo file from mirrors.163.com and epel for CENTOS/REDHAT DISTIBUTE

# Author: Gateray Jo

# mail: hikaru211@126.com

set -e

prog=`basename $0`

arch=`uname -m`

release=`lsb_release -r | sed 's/.*:[[:space:]]*//'`

function usage() {

cat<

Usage: ./${prog} [options] [arg]

-h: print the command help.

-u: specified the baseurl for yum repository, must use with -ufn combination.

-f: specified the name of local repofile(no ".repo" ext name) which place in /etc/yum.repos.d/, must use with -ufn

combination.

-n: specified the repo's name in xxx.repo file, must use with -ufn combination.

-d: specified the url of repofile which will be download to /etc/yum.repos.d/.

By default, nothing options are specified which will download epel and 163 yum repofile.

eof

}

function default() {

major_rel=${release%%.*}

# download 163 repofile

printf "Download 163 repofile from mirrors.163.com:\n"

wget -P /etc/yum.repos.d http://mirrors.163.com/.help/CentOS${major_rel}-Base-163.repo

printf "Download epel repofile from http://dl.fedoraproject.org.:\n"

if [[ $arch = "x86_64" ]]; then

[ ${major_rel} -eq 5 ] && rpm -ivh http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm

[ ${major_rel} -eq 6 ] && rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

else

[ ${major_rel} -eq 5 ] && rpm -ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm

[ ${major_rel} -eq 6 ] && rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

fi

return $?

}

if [[ $# -eq 0 ]]; then

default

exit $?

fi

while getopts ":hu:f:n:d:" optname; do

case $optname in

"h")

usage && exit 0

;;

"u")

u_opt=1

u_arg=$OPTARG

;;

"f")

f_opt=1

u_arg=$OPTARG

;;

"n")

n_opt=1

n_arg=$OPTARG

;;

"d")

wget -P /etc/yum.repos.d $OPTARG

exit $?

;;

"?")

echo "Unknown option $OPTARG"

usage && exit 1

;;

":")

echo "It is need a option value for $OPTARG"

usage && exit 1

;;

*)

echo 'Unknown error!' && exit 1

;;

esac

done

if [[ $u_opt -eq 1 ]] && [[ $f_opt -eq 1 ]] && [[ $n_opt -eq 1 ]]; then

if [[ ${n_arg} =~ '-' ]]; then

echo "Invalid value for -n option, \"${n_arg}\" is include '-' character."

exit 1

fi

cat >> /etc/yum.repos.d/${f_arg}.repo <

[${n_arg}]

name=${n_arg}

baseurl=${u_arg}

enabled=1

gpgcheck=0

eof

else

usage && exit 1

fi

脚本的执行:

1)打印命令帮助

[root@dns1 ~]# ./load_yum_repo.sh -h

Usage: ./load_yum_repo.sh [options] [arg]

-h: print the command help.

-u: specified the baseurl for yum repository, must use with -ufn combination.

-f: specified the name of local repofile(no ".repo" ext name) which place in /etc/yum.repos.d/, must use with -ufn

combination.

-n: specified the repo's name in xxx.repo file, must use with -ufn combination.

-d: specified the url of repofile which will be download to /etc/yum.repos.d/.

By default, nothing options are specified which will download epel and 163 yum repofile.

2)不带任何选项参数时,自动下载163和epel的yum源repofile

[root@dns1 ~]# ./load_yum_repo.sh

Download 163 repofile from mirrors.163.com:

--2015-04-04 22:23:05-- http://mirrors.163.com/.help/CentOS6-Base-163.repo

Resolving mirrors.163.com... 123.58.173.106

Connecting to mirrors.163.com|123.58.173.106|:80... connected.

HTTP request sent, awaiting response... 200 OK

Length: 2006 (2.0K) [application/octet-stream]

Saving to: “/etc/yum.repos.d/CentOS6-Base-163.repo”

100%[==========================================================================================>] 2,006 --.-K/s in 0.05s

2015-04-04 22:23:05 (39.2 KB/s) - “/etc/yum.repos.d/CentOS6-Base-163.repo” saved [2006/2006]

Download epel repofile from http://dl.fedoraproject.org.:

Retrieving http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

warning: /var/tmp/rpm-tmp.wVrCDS: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY

Preparing... ########################################### [100%]

1:epel-release ########################################### [100%]

3)从指定的url中下载repofile

[root@dns1 ~]# ./load_yum_repo.sh -d http://mirrors.163.com/.help/CentOS6-Base-163.repo

--2015-04-04 22:13:47-- http://mirrors.163.com/.help/CentOS6-Base-163.repo

Resolving mirrors.163.com... 123.58.173.106

Connecting to mirrors.163.com|123.58.173.106|:80... connected.

HTTP request sent, awaiting response... 200 OK

Length: 2006 (2.0K) [application/octet-stream]

Saving to: “/etc/yum.repos.d/CentOS6-Base-163.repo”

100%[==========================================================================================>] 2,006 --.-K/s in 0.05s

2015-04-04 22:13:47 (40.9 KB/s) - “/etc/yum.repos.d/CentOS6-Base-163.repo” saved [2006/2006]

4)创建自定义的repofile

[root@dns1 ~]# ./load_yum_repo.sh -f test -n test -u file:///mnt

[root@dns1 ~]# cat /etc/yum.repos.d/test.repo

[test]

name=test

baseurl=file:///mnt

enabled=1

gpgcheck=0

repo命令添加bin_Linux shell中getopts命令学习--实现一个添加yum源的脚本相关推荐

  1. python getopts_linux bash shell 中getopts 命令 和 python 中 getopt 函数的比较总结

    在 python 中有个获取命令行参数的函数叫 getopt(args, shortopts, longopts=[]) 通常我们使用的时候是如下的形式: import sys import geto ...

  2. shell中sed命令的用法

    sed (stream editor)流编辑器也是linux中的一条命令,在shell中经常需要用到的非交互式修改文件内容的命令.sed处理文本是按行处理,也就是读一行处理一行. sed的命令基本格式 ...

  3. linux shell中的命令自动补全(compgen complete)与 命令行参数解析

    linux shell中的命令自动补全(compgen complete)与 命令行参数解析 标签: shell脚本 2013-12-31 21:56 6661人阅读 评论(6) 收藏 举报 分类: ...

  4. shell中declare命令

    shell中declare命令 declare命令有如下选项: -a 声明一个数组 -i 声明一个整型 -f 打印所有函数定义 -F 仅打印函数名字 -r 声明一个readonly变量,该变量的值无法 ...

  5. shell中source命令与sh命令的区别

    一.source命令 1.1 source命令的使用方法 source filename.sh 文件没有可执行权限时,也可以使用source命令执行. source命令是在当前shell中执行的,并未 ...

  6. linux 进入shell命令,linux或者shell进入vi命令

    vi的基本操作 a) 进入vi     在系统提示符号输入vi及文件名称后,就进入vi全屏幕编辑画面: $ vi file 不过有一点要特别注意,就是您进入vi之后,是处于「命令行模式(command ...

  7. Linux中你必须学习的软件安装工具yum(以及必须得安装的软件)

    目录 1. Linux下的软件安装方式 2.什么是yum 3.如何使用yum安装 3.1 查看yum中可下载软件 3.2* yum安装相应软件 3.3 yum卸载相应软件 4 配置yum源 4.1 为 ...

  8. linux脚本添加source,shell中的source命令的巧妙用法

    首先,通常用于重新执行刚修改的初始化文件,使之立即生效,而不必注销并重新登录.例如,当我们修改了/etc/profile文件,并想让它立刻生效,而不用重新登录,就可以使用source命令,如sourc ...

  9. shell中test命令方法详解

    1)判断表达式 if test (表达式为真) if test !表达式为假 test 表达式1 –a 表达式2 两个表达式都为真 test 表达式1 –o 表达式2 两个表达式有一个为真 2)判断字 ...

最新文章

  1. 这次不忽悠:3个成功案例告诉你,开一家AI公司其实不难
  2. matlab共轭梯度法_优化算法之牛顿法
  3. 配置phoenix连接hbase_Phoenix4.14不读取hbase配置文件-问答-阿里云开发者社区-阿里云...
  4. [案例分享]科大云炬工作室开发的安卓APP项目
  5. 【渝粤教育】国家开放大学2018年春季 0043-22T计算机文化 参考试题
  6. 支付宝上市,让我损失了2000万(盘点这些年错过的机会)
  7. Referenced file contains errors (http://JAVA.sun.com/xml/ns/j2ee/web-app_2_5.xsd).
  8. 转:【微信小程序】实现锚点定位楼层跳跃的实例
  9. KVO.非常简单的键值监听模式
  10. Java菜鸟教程 标识符
  11. 新手建站十大免费空间推荐-稳定,可用的免费空间及其使用体验
  12. 实名认证失败_身份证在国政通进行实名认证失败怎么办?
  13. 公司企业邮箱怎么注册开通?
  14. ERP的灵魂是管理思想
  15. 亚马逊、沃尔玛、速卖通、Temu、OZON自养号测评和真人测评两者如何选择?
  16. RMAN Recipes 中对Flash Recovery Area的总结
  17. 10 个Web3 设计灵感网站
  18. 100 个网络基础知识,全部掌握顶半个网络高手
  19. 改进Python文字小游戏(4)
  20. Flink系列文档-(YY02)-Flink编程基础-入门示例

热门文章

  1. 计算机CCT考试模拟操作题,基础计算机cct模拟测试模拟题.doc
  2. Essential Linux Device Drivers 中文版第2章
  3. python生成随机数和随机矩阵
  4. 国网GIM设备三维模型要求细则 - 框架式电容器
  5. 人工智能、机器学习、深度学习学习资料整理(开发必备)
  6. c语言,%d %.2d %2d %02d的区别
  7. 苹果屏蔽更新_知友答疑——无需越狱,屏蔽 iOS 13更新的描述文件来了
  8. abaqus如何并行计算_如何修改abaqus并行计算的默认设置
  9. leetcode 1014
  10. 0306-二维地图开发-地图可视化:简单符号渲染