help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] Passing elements to function


From: Jerry
Subject: [Help-bash] Passing elements to function
Date: Sat, 14 Oct 2017 17:13:52 -0400

I am new to programing in bash. I am trying to copy data from an array to a
file. I got this script snippet while Googling.

for j in "address@hidden"
do
index=0
element_count="${j}"
while [ "${index}" -lt "${element_count}" ]
do    # List all the elements in the array.
echo "${array[${index}]}" >> ${FILE}
((index++))
done
done

Now, I want to make this into a function, so I wrote it as this:

array2file () {
for j in "$1"
do
index=0
element_count="${j}"
while [ "${index}" -lt "${element_count}" ]
do    # List all the elements in the array.
echo "${$3[${index}]}" >> $2
((index++))
done
done
}

The problem comes when I try to elements to it.

array2file "address@hidden" "data_file.txt"  "MyArray"

The problem is in the substitution. "j" = $1 and  $2 = "data_file.txt". The
problem is I don't know how to get $3 = "MyArray" in this example. I keep
receiving an error message about a bad substitution. Is there a way to do this?

Thanks!

--
Jerry



reply via email to

[Prev in Thread] Current Thread [Next in Thread]