前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >linux 处理选项

linux 处理选项

作者头像
葫芦
发布2019-04-17 14:29:30
9230
发布2019-04-17 14:29:30
举报
文章被收录于专栏:葫芦葫芦葫芦
[root@localhost ~]# cat -n /test51
     1  #!/bin/bash
     2  while [ -n "$1" ]
     3  do
     4  case "$1" in
     5  -a) echo "-a";;
     6  -b) echo "-b";;
     7  *) echo "nothing";;
     8  esac
     9  shift
    10  done
[root@localhost ~]# sh /test51 -a d -b s
-a
nothing
-b
nothing
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
当出现双波折线-- shell就会停止处理选项,并将剩下的参数都当作命令行参数。
[root@localhost ~]# cat /test52
#!/bin/bash
while [ -n "$1" ]
do
case "$1" in
-a) echo "-a";;
-b) echo "-b";;
--) shift
break;;
*)echo "$1 is not an option";;
esac
shift
done
count=1
for param in $@
do
echo "parameter #$count: $param"
count=$[ $count +1 ]
done
[root@localhost ~]# sh /test52 da dfas"d" ss "s" -a df -b --test -- test3 tes
da is not an option
dfasd is not an option
ss is not an option
s is not an option
-a
df is not an option
-b
--test is not an option
parameter #1: test3
parameter #2: tes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[root@localhost ~]# cat /1
#!/bin/bash
while [ -n "$1" ]
do
case "$1" in
-a) echo " Found the -a option ";;
-b) param="$2"
echo " Found the -b option,with parameter value $param"
shift;;
-c) echo "Found the -c option ";;
--)shift
break;;
*) echo "$1 is not an option";;
esac
shift
done

count=1
for param in "$@"
do
echo "Parameter #$count: $param"
count=$[ $count + 1 ]
done
[root@localhost ~]# sh /1 -a b a -b test1 -d    dsdfsd  ds -- 32s
Found the -a option
b is not an option
a is not an option
Found the -b option,with parameter value test1
-d is not an option
dsdfsd is not an option
ds is not an option
Parameter #1: 32s
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013/10/10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档