How can an array be accessed from within another function?
Here is an example of some code. editarray can not seem to access values of the array from outside of the writetofile function. Is there any way to change the scope of the array?
#!/bin/sh
writetofile()
{
templine=`cat /dev/stdin`
export templine
echo "templine is: $templine"
var1=$(echo $templine | gawk '{print $1}')
var2=$(echo $templine | gawk '{print $2}')
var3=$(echo $templine | gawk '{print $3}')
var4=$(echo $templine | gawk '{print $4}')
var5=$(echo $templine | gawk '{print $5}')
echo $var1" "$var2" "$var3" "$var4" "$var5" "
export var1 var2 var3 var4 var5
arrayfunction
#some commands to edit standard input
}
arrayfunction()
{
echo "writing to array"
array[0]=$var1
array[1]=$var2
array[2]=$var3
array[3]=$var4
array[4]=$var5
}
editarray()
{
echo "editing array"
echo "==========Array==Output========="
echo ${array[@]}
echo "==========Finished==Output======"
echo "writing to file"
#call array at index and assign value
}
#main
echo "input this to be parsed and placed into an array" | writetofile
editarray