shell 函数传递参数的几种方式


1.最近总结了 shell 中 function 的传递变量的几种方式
1.传递单个变量
2.传递数组变量
 
#!/bin/bash
 
#trying to pass an variable.
 
function func()
{
echo "The number of parameters is: ${#}"
for line in "$@"
do
echo "$line"
done
}
 
function func2()
{
  param1=("${!1}")
  param2=("${!2}")
  echo ${param1[*]}
  echo ${param2[*]}
}
echo "****************************************************"
#1.pass simpl variable.
func "hello"
func "hello"  "world"
func 1 2 3
 
#2.pass array variable
echo "*****************************************************"
array=(1 2 3)
strarray=(hello world)
echo "***************************************************"
func ${array[*]} ${strarray[*]}
func ${array[@]} ${strarray[@]}
 
echo "*****************************************************"
func2 array[@] strarray[@]
func2 array[*] strarray[*]
 
输出结果如下:
****************************************************
The number of parameters is: 1
hello
The number of parameters is: 2
hello
world
The number of parameters is: 3
1
2
3
*****************************************************
***************************************************
The number of parameters is: 5
1
2
3
hello
world
The number of parameters is: 5
1
2
3
hello
world
*****************************************************
1 2 3
hello world
1 2 3
hello world


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM