zoukankan      html  css  js  c++  java
  • Shell函数参数

    在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数...

    带参数的函数示例:

    #!/bin/bash
    
    funWithParam(){
        echo "The value of the first paramter is $1!"
        echo "The value of the second paramter is $2!"
        echo "The value of the tenth paramter is $10!"
        echo "The value of the tenth paramter is ${10}!"
        echo "The value of the eleventh paramter is ${11}!"
        echo "The amount of the parameters is $#!" #参数个数
        echo "The string of the parameter is $*" #传递给函数的所有>参数
    
    }
    funWithParam 1 2 3 4 5 6 7 8 9 34 73

    运行脚本:

    The value of the first paramter is 1!
    The value of the second paramter is 2!
    The value of the tenth paramter is 10!
    The value of the tenth paramter is 34!
    The value of the eleventh paramter is 73!
    The amount of the parameters is 11!
    The string of the parameter is 1 2 3 4 5 6 7 8 9 34 73

    注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。

    另外,还有几个特殊变量用来处理参数,前面已经提到:

    特殊变量 说明
    $# 传递给函数的参数个数。
    $* 显示所有传递给函数的参数。
    $@ 与$*相同,但是略有区别。
    $? 函数的返回值。
  • 相关阅读:
    ruby
    快捷键_命令行
    Babylon
    遇见的一些关于js冲突的问题
    js中出生年月日的使用,以及性别的判断
    js中iframe的使用
    js中ul与li的使用
    jQuery,ajax,for循环使用典型案例
    Easy | LeetCode 118. 杨辉三角
    Easy | LeetCode 461. 汉明距离 | 位运算
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4023574.html
Copyright © 2011-2022 走看看