[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
这里直接上操作来说明function 的传参
新建一个脚本
vi 1.sh
#!/bin/bash function check_a { echo $2 echo $3 echo "this is check_a" }
function check_b { echo $2 echo "this is check_b" } $1 #$1作为选择执行哪个function的参数
执行以下这个脚本传入参数看看会有什么结果
sh 1.sh check_a kafka rabbitmq
sh 1.sh check_b kafka
可以看到执行两个function函数的脚本都有输出固定的参数,说明$1是传参成功的,但是脚本里定义的变量参数都输出为空,说明没有参数传入,但是在执行语句的时候明明有写入参数
更改一下脚本
#!/bin/bash function check_a { echo $2 echo $3 echo "this is check_a" }
function check_b { echo $2 echo "this is check_b" } $1 $2 $3
执行以下这个脚本传入参数看看会有什么结果
sh 1.sh check_a kafka rabbitmq
sh 1.sh check_b kafka
可以看到check_a脚本输出了$2变量参数,但是$2输出的参数是有问题的,$2输出了rabbitmq,这个参数应该是$3变量输出才对,然而$3变量参数输出为空,kafka参数依旧没有输出,check_b脚本输出的$2参数也依旧是空
再次更改脚本
#!/bin/bash function check_a { echo $1 echo $2 echo "this is check_a" }
function check_b { echo $1 echo "this is check_b" } $1 $2 $3
执行以下这个脚本传入参数看看会有什么结果
sh 1.sh check_a kafka rabbitmq
sh 1.sh check_b kafka
可以看到这次check_a脚本正确输出了所有的变量参数,check_b也正确的输出了所有变量参数,其实是因为在外部传入脚本的参数不能直接传到function函数定义的脚本里面,只能先传到脚本里,在通过定义每个function函数的参数队列去重新传入参数到function函数的脚本中