Shell处理用户输入参数:getopts - Linux系统
发布时间:2013-12-18 10:37:14 所属栏目:Linux 来源:站长网
导读:特殊变量提醒: $# 记录命令行参数个数 $* 保存所有参数,并当做单个单词保存 $@ 保存所有参数,当做同一个字符串中的多个独立的单词 getopts 命令格式: getop
特殊变量提醒:
$# 记录命令行参数个数 $* 保存所有参数,并当做单个单词保存 $@ 保存所有参数,当做同一个字符串中的多个独立的单词 getopts 命令格式: getopts optstring variable 有效字母都会列在optstring中,当前参数保存在 variable中 示例: #!/bin/bash while getopts :ab:c opt do case "$opt" in a) echo "Found the -a option";; b) echo "Found the -b option,wiht value $OPTARG";; c) echo "FOund the -c option";; *) echo "Unknown option :$opt";; esac done 测试 # sh test.sh -a -b test -c Found the -a option Found the -b option,wiht value test FOund the -c option # sh test.sh -d Unknown option :? 选项字母要求有参数值的时候,在其后加一个冒号; 去掉错误消息的话,在optstring之前加一个冒号; $OPTARG会保存参数值 出处:http://weipengfei.blog.51cto.com/1511707/1118695 (编辑:佛山站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |