【Shell脚本学习23】Shell函数参数
来源:互联网 发布:淘宝申请维修流程 编辑:程序博客网 时间:2024/04/27 22:38
在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数...
带参数的函数示例:特殊变量 说明 $#传递给函数的参数个数。$*显示所有传递给函数的参数。$@与$*相同,但是略有区别,请查看Shell特殊变量。$?函数的返回值。
带参数的函数示例:
- #!/bin/bash
- funWithParam(){
- echo "The value of the first parameter is $1 !"
- echo "The value of the second parameter is $2 !"
- echo "The value of the tenth parameter is $10 !"
- echo "The value of the tenth parameter is ${10} !"
- echo "The value of the eleventh parameter is ${11} !"
- echo "The amount of the parameters is $# !" # 参数个数
- echo "The string of the parameters is $* !" # 传递给函数的所有参数
- }
- funWithParam 1 2 3 4 5 6 7 8 9 34 73
运行脚本:
The value of the first parameter is 1 !The value of the second parameter is 2 !The value of the tenth parameter is 10 !The value of the tenth parameter is 34 !The value of the eleventh parameter is 73 !The amount of the parameters is 12 !The string of the parameters is 1 2 3 4 5 6 7 8 9 34 73 !"
注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。
另外,还有几个特殊变量用来处理参数,前面已经提到:
0 0
- 【Shell脚本学习23】Shell函数参数
- shell脚本 函数传递参数
- Shell函数和脚本参数
- shell脚本学习总结,参数
- shell 脚本 输入参数 学习
- linux学习之shell脚本 ------- shell函数
- 脚本学习-----常用shell脚本if参数
- Shell 学习22 - Shell 函数参数
- shell脚本学习--函数篇
- Shell脚本函数定义和函数参数
- shell脚本程序函数参数传递
- Shell 编程学习笔记:脚本输入参数
- Shell脚本学习-命令行参数处理
- Shell脚本学习-命令行参数处理
- shell 脚本 if 参数
- shell 脚本 if 参数
- shell脚本 if参数
- shell脚本文件参数
- 【Shell脚本学习20】Shell until循环
- VB.Net视频——宏观总结
- 【Shell脚本学习21】Shell break和continue命令
- 计算方法实验报告PC版 —王世儒、王金金
- 【Shell脚本学习22】Shell 函数:Shell函数返回值、删除函数、在终端调用函数
- 【Shell脚本学习23】Shell函数参数
- 【Shell脚本学习24】Shell输入输出重定向:Shell Here Document,/dev/null文件
- 【Shell脚本学习25】Shell文件包含
- 【Android车载系统 News | Tech 5】车载设计开发
- 【Java/Android性能优 4】PreloadDataCache支持预取的数据缓存,使用简单,支持多种缓存算法,支持不同网络类型,扩展性强
- 【Java/Android性能优5】 Android ImageCache图片缓存,使用简单,支持预取,支持多种缓存算法,支持不同网络类型,扩展性强
- 字符串的MD5加密
- 【Java/Android性能优 6】Android 图片SD卡缓存 使用简单 支持预取 支持多种缓存算法 支持不同网络类型 支持序列化
- 【Java/Android性能优 7】Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公共类