Chinaunix首页 | 论坛 | 博客
  • 博客访问: 70393
  • 博文数量: 9
  • 博客积分: 214
  • 博客等级: 入伍新兵
  • 技术积分: 95
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-09 13:24
个人简介

hi,fuck u

文章分类
文章存档

2011年(9)

我的朋友

分类: LINUX

2011-09-18 20:16:55

  所谓的位置参数指的也就是shell脚本的命令行参数。在shell函数里面,它们也可以同时是函数的参数。各参数都由整数命名,但超过了9就应该用大括号把数字框起来。

[badboy@localhost ~]$ set -- 1 2 3 4 5 6 7 8 9 10
[badboy@localhost ~]$ echo the first number is $1
the first number is 1
[badboy@localhost ~]$ echo the tenth number is ${10}
the tenth number is 10
 
 下面我主要介绍的是特殊“变量”提供对传递的参数的总数的访问,以及一次对所有参数的访问。

 1. $#
 
  提供传递到shell脚本或函数的参数总数。当你为了处理选项和参数而建立循环时会很有用哈。。
[badboy@localhost ~]$ echo the number of arg is $#
the number of arg is 10

 2. $*,$@
   一次表示所有的命令行参数。
[badboy@localhost ~]$ echo all number is $*
all number is 1 2 3 4 5 6 7 8 9 10
[badboy@localhost ~]$ echo all number is $@
all number is 1 2 3 4 5 6 7 8 9 10

 3."$*"
    将所有的命令行参数视为单个字符串。等同于"$1 $2 $3..."。
           
[badboy@localhost ~]$ for i in "$*"
> do echo i is $i
> done

i is 1 2 3 4 5 6 7 8 9 10

 4. "$@"
     将所有命令行参数视为单个字符串,等同于"$1","$2","$3"....。。
[badboy@localhost ~]$ for i in "$@"
> do echo i is $i
> done

i is 1
i is 2
i is 3
i is 4
i is 5
i is 6
i is 7
i is 8
i is 9
i is 10

阅读(2609) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~