技术改变生活

博客园 首页 新随笔 联系 订阅 管理
 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者之间的关联关系。

posted on 2020-04-27 23:02  小阿峰  阅读(228)  评论(0编辑  收藏  举报