首页 > 其他 > 详细

shell 中利用getopts

时间:2014-09-16 19:09:31      阅读:265      评论:0      收藏:0      [点我收藏+]

[root@jboss shell]# cat getopts.sh 

#!/bin/bash

#date=2014-09-16

#is to practice  the getopts

while getopts "a:bc" arg(:前面的变量是一定要跟参数的)

do 

case $arg in

 a)

echo "a‘s arg:$OPTARG"

;;

 b)

echo "b"

;;

 c)

echo "c"

;;

 ?)

echo "unkown argument"

     exit1

;;

esac

done


[root@jboss shell]# ./getopts.sh -b

b

[root@jboss shell]# ./getopts.sh -c

c

[root@jboss shell]# ./getopts.sh c

[root@jboss shell]# ./getopts.sh -a /root/

a‘s arg:/root/

如果不加参数的话会报错的。

[root@jboss shell]# ./getopts.sh -a

./getopts.sh: option requires an argument -- a

unkown argument

./getopts.sh: line 18: exit1: command not found


本文出自 “路由汗水交换成功” 博客,请务必保留此出处http://ghnhl.blog.51cto.com/1630025/1553353

shell 中利用getopts

原文:http://ghnhl.blog.51cto.com/1630025/1553353

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!