zoukankan      html  css  js  c++  java
  • 函数(位置传参)

     1 [root@localhost array]# cat function_locate.sh 
     2 #!/bin/bash
     3 if [ $# -ne 3 ];then
     4     echo "usage: `basename $0` part1  part2 part3"
     5     exit
     6 fi
     7 
     8 fun1() {
     9     echo "$(($1 * $2 * $3))"
    10     echo $1 $2 $3
    11 }
    12 
    13 result=`fun1 $3 $2 $1`
    14 echo "result is $result"
    15 [root@localhost array]# sh function_locate.sh
    16 usage: function_locate.sh part1  part2 part3
    17 [root@localhost array]# sh function_locate.sh 2 3 4
    18 result is 24
    19 4 3 2
    20 [root@localhost array]# 

    第二版:

     1 [root@localhost array]# cat function_locate.sh 
     2 #!/bin/bash
     3 if [ $# -ne 3 ];then
     4     echo "usage: `basename $0` part1  part2 part3"
     5     exit
     6 fi
     7 
     8 fun1() {
     9     echo "$(($1 * $2 * $3))"
    10     echo $1 $2 $3
    11 }
    12 
    13 result=`fun1 5 4 3`
    14 echo "result is $result"
    15 [root@localhost array]# sh function_locate.sh
    16 usage: function_locate.sh part1  part2 part3
    17 [root@localhost array]# sh function_locate.sh 1 2 3
    18 result is 60
    19 5 4 3
    20 [root@localhost array]# 

    思想:shell程序的位置参数和函数的位置参数,2者是不同的概念。可以自定义2者之间的关联关系。

  • 相关阅读:
    leetcode 刷题日志 2018-03-26
    WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping
    sublime wrong
    SSM框架使用-wrong
    C++设计实现算法时易犯错误
    CodeBlocks wrong
    leetcode 刷题日志 2018-3-28
    CountDownLatch
    类加载器和双亲委派
    GC的一个面试题
  • 原文地址:https://www.cnblogs.com/xiaofeng666/p/12790903.html
Copyright © 2011-2022 走看看