免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 7243 | 回复: 31
打印 上一主题 下一主题

[其他] shell 函数获取动态参数问题,求解答 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-09-23 18:15 |只看该作者 |倒序浏览
在运行shell脚本的时候,后面会跟上参数,但是参数的数量不确定。
例如:bash test.sh  3  10.10.10.31  10.10.10.32  10.10.10.33
第一个参数表示后面有3个ip参数,参数的数目是根据需求动态变化的。
在tesh.sh 内,有一个函数需要利用到所有的输入参数,。
只会传递固定数量参数的那种,这种动态的不会。
求大牛解答。

在线等~~~

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
2 [报告]
发表于 2014-09-23 19:21 |只看该作者
回复 1# The_X

$ man bash


   Special Parameters
       The shell treats several parameters specially.   These  parameters  may
       only be referenced; assignment to them is not allowed.
       *      Expands  to  the positional parameters, starting from one.  When
              the expansion occurs within double quotes, it expands to a  sin‐
              gle word with the value of each parameter separated by the first
              character of the IFS special variable.  That is, "$*" is equiva‐
              lent to "$1c$2c...", where c is the first character of the value
              of the IFS variable.  If IFS is unset, the parameters are  sepa‐
              rated  by  spaces.   If  IFS  is null, the parameters are joined
              without intervening separators.
       @      Expands to the positional parameters, starting from  one.   When
              the  expansion  occurs  within  double  quotes,  each  parameter
              expands to a separate word.  That is, "$@" is equivalent to "$1"
              "$2"  ...   If the double-quoted expansion occurs within a word,
              the expansion of the first parameter is joined with  the  begin‐
              ning  part  of  the original word, and the expansion of the last
              parameter is joined with the last part  of  the  original  word.
              When  there  are no positional parameters, "$@" and $@ expand to
              nothing (i.e., they are removed).

   

论坛徽章:
0
3 [报告]
发表于 2014-09-23 19:48 |只看该作者
回复 2# jason680

好长的英文,一会好好看看。
   

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
4 [报告]
发表于 2014-09-23 20:48 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
5 [报告]
发表于 2014-09-24 11:26 |只看该作者
如果想做的高大上一点,请用 getopts 命令。

论坛徽章:
0
6 [报告]
发表于 2014-09-24 11:50 |只看该作者
回复 5# Shell_HAT


    http://blog.csdn.net/qzwujiaying/article/details/6371246
上面介绍getopts/getopt,但没看明白,,现在已经改用另一种方案了。

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
7 [报告]
发表于 2014-09-24 12:11 |只看该作者
回复 6# The_X


    代码发出来给大家分享一下吧

论坛徽章:
0
8 [报告]
发表于 2014-09-24 14:34 |只看该作者
回复 5# Shell_HAT


    我没能实现获取动态参数,
更改的方案是获取固定数量的参数,大家都会,就不分享了。
如果哪位大牛实现了获取动态参数,请分享一下,供大家学习。

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
9 [报告]
发表于 2014-09-24 14:40 |只看该作者
回复 8# The_X


能否举个例子说明一下什么是取动态参数,是不是这个意思:
[root]# cat test.sh
echo arg number: $#

[root]# ./test.sh 10.10.10.31
arg number: 1
[root]# ./test.sh 10.10.10.31 10.10.10.32
arg number: 2
[root]# ./test.sh 10.10.10.31 10.10.10.32 10.10.10.33
arg number: 3

论坛徽章:
0
10 [报告]
发表于 2014-09-24 14:50 |只看该作者
回复 9# Shell_HAT

我说的动态参数是指,参数的数量不确定。因为shell脚本内部的函数(funTest)不能直接用"$1" "$2" "$3"的形式调用命令行参数。
但这个函数又需要获取输入的所有参数,因此通过 funTest  $1 $2 $3 的形式给函数传递参数不能解决问题(因为参数数量不一定,而函数需要所有的参数)。

不知道我有没有描述清楚
   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP