Shall高级编程

源代码在线查看: array-function.sh

软件大小: 1353 K
上传用户: stuoju
关键词: Shall 高级编程
下载地址: 免注册下载 普通下载 VIP

相关代码

				#!/bin/bash				# array-function.sh: Passing an array to a function and...				#                   "returning" an array from a function												Pass_Array ()				{				  local passed_array   # Local variable.				  passed_array=( `echo "$1"` )				  echo "${passed_array[@]}"				  #  List all the elements of the new array				  #+ declared and set within the function.				}												original_array=( element1 element2 element3 element4 element5 )								echo				echo "original_array = ${original_array[@]}"				#                      List all elements of original array.												# This is the trick that permits passing an array to a function.				# **********************************				argument=`echo ${original_array[@]}`				# **********************************				#  Pack a variable				#+ with all the space-separated elements of the original array.				#				# Note that attempting to just pass the array itself will not work.												# This is the trick that allows grabbing an array as a "return value".				# *****************************************				returned_array=( `Pass_Array "$argument"` )				# *****************************************				# Assign 'echoed' output of function to array variable.								echo "returned_array = ${returned_array[@]}"								echo "============================================================="								#  Now, try it again,				#+ attempting to access (list) the array from outside the function.				Pass_Array "$argument"								# The function itself lists the array, but...				#+ accessing the array from outside the function is forbidden.				echo "Passed array (within function) = ${passed_array[@]}"				# NULL VALUE since this is a variable local to the function.								echo								exit 0							

相关资源