Linux命令行与shell脚本(14)--在函数中使用数组

数组变量和函数

  • 传递数组给函数
function testit(){
        local new_array;
        new_array=(`echo $@`);
        echo "The new array value is: ${new_array[*]}";

}
myarray=(1 2 3 4 5 6);
echo "The original array is ${myarray[*]}";
testit ${myarray[*]}
  • 函数返回一个数组
function arraydblr(){
    local origarray;
    local newarray;
    local elements;
    local i
    origarray=(`echo "$@"`);
    newarray=(`echo "$@"`);
    elements=$[ $# - 1 ];
    for (( i = 0; i <= $elements ; i++  ))
    {
        newarray[$i]=$[ ${origarray[$i]} * 2 ]
    }
    echo ${newarray[*]}
}
myarray=(1 2 3 4 5);
echo "The original array is :"${myarray[*]};
arg1=`echo ${myarray[*]}`;
result=(`arraydblr $arg1`);
echo "The new array is: ${result[*]}"

你可能感兴趣的:(Linux命令行与shell脚本(14)--在函数中使用数组)