Chinaunix首页 | 论坛 | 博客
  • 博客访问: 97979
  • 博文数量: 27
  • 博客积分: 550
  • 博客等级: 下士
  • 技术积分: 240
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-20 10:47
文章分类
文章存档

2014年(1)

2011年(26)

分类: LINUX

2011-07-18 09:15:34

一个完善的shell脚本应该能处理行如 -option 的命令行选项,bash 有一个内置的命令 getopts 可用来处理命令行选项
 
示例,用法 getopts.h -x -y YARG -z ZARG *
 
bbkf:/yzyfb/report/cdq/bash$cat getopts.sh
#!/bin/bash
# getopts.sh - Using getopts
##############################
while getopts xy:z: opt;
do
        case $opt in
                x) xopt='-x set';;
                y) yopt="-y set and called with $OPTARG" ;;
                z) zopt="-z set and called with $OPTARG" ;;
                \?) echo 'USAGE: getopts.sh [-x] [-y arg] [-z arg] file ...'
                   exit 1
        esac
done
shift $(($OPTIND - 1 ))
echo ${xopt:-'did not use -x'}
echo ${yopt:-'did not use -y'}
echo ${zopt:-'did not use -z'}
echo "Remaining command-line arguments are:"
for f in "$@"
do
        echo "\t$f"
done
阅读(1570) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~