南强小屋 Design By 杰米
#!/bin/bash
#extracting command text_text_text_line options as parameters
help_info(){
echo "NAME"
echo "\t$0"
echo "SYNOPSIS"
echo "\t$0 is a shell test about process options"
echo "DESCRIPTION"
echo "\toption like -a -b param1 -c param2 -d"
}
if [ $# -lt 0 ]
then
help_info
fi
nomal_opts_act()
{
echo -e "\n### nomal_opts_act ###\n"
while [ -n "$1" ]
do
case "$1" in
-a)
echo "Found the -a option"
;;
-b)
echo "Found the -b option"
echo "The parameter follow -b is $2"
shift
;;
-c)
echo "Found the -c option"
echo "The parameter follow -c is $2"
shift
;;
-d)
echo "Found the -d option"
;;
*)
echo "$1 is not an option"
;;
esac
shift
done
}
#用shell命令自建的选项解析,可以按照自己的想法实现
#优点:自己定制,没有做不到,只有想不到
#缺点:麻烦
getopt_act()
{
echo -e "\n### getopt_act ###\n"
GETOPTOUT=`getopt ab:c:d "$@"`
set -- $GETOPTOUT
while [ -n "$1" ]
do
case $1 in
-a)
echo "Found the -a option"
;;
-b)
echo "Found the -b option"
echo "The parameter follow -b is "$2""
shift
;;
-c)
echo "Found the -c option"
echo "The parameter follow -c is "$2""
shift
;;
-d)
echo "Found the -d option"
;;
--)
shift
break
;;
*)
echo "Unknow option: "$1""
;;
esac
shift
done
param_index=1
for param in "$@"
do
echo "Parameter $param_index:$param"
param_index=$[ $param_index + 1 ]
done
}
#用getopt命令解析选项和参数
#优点:相对与getopts来说是个半自动解析,自动组织选项和参数,用 -- 符号将选项与参数隔开
#缺点:相对于getopts的缺点
#1.需要与set -- 命令配合,不是必须,需要手动shift
#2.选项参数中不支持空格如 -a -b dog -c "earth moon" -d -f param1 param2 就会解析错误
getopts_act()
{
echo -e "\n### getopts_act ###\n"
while getopts :ab:c:d ARGS
do
case $ARGS in
a)
echo "Found the -a option"
;;
b)
echo "Found the -b option"
echo "The parameter follow -b is $OPTARG"
;;
c)
echo "Found the -c option"
echo "The parameter follow -c is $OPTARG"
;;
d)
echo "Found the -d option"
;;
*)
echo "Unknow option: $ARGS"
;;
esac
done
shift $[ $OPTIND -1 ]
param_index=1
for param in "$@"
do
echo "Parameter $param_index:$param"
param_index=$[ $param_index + 1 ]
done
}
#getopts 命令解析选项和参数
#优点:可在参数中包含空格如:-c "earth moon"
# 选项字母和参数值之间可以没有空格如:-bdog
# 可将未定义的选项绑定到"earth moon" -d -f param1 param2
getopt_act -a -b dog -c earth -d -f param1 param2
标签:
Shell脚本,传递参数,方法
南强小屋 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
南强小屋 Design By 杰米
暂无Shell脚本传递参数的3种方法比较的评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。