Discussion:
[Help-bash] Passing elements to function
Jerry
2017-10-14 21:13:52 UTC
Permalink
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 "${#array[@]}"
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 "${#MyArray[@]}" "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
Tim Visher
2017-10-15 00:21:30 UTC
Permalink
There's honestly a great many misunderstandings present in your code. You
would do well to read this <http://mywiki.wooledge.org/BashGuide> and then
revisit the problem.

You'll probably get a snippet of an answer here but if you don't read up on
things your in for a lot of hurt as you continue.
Post by Jerry
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.
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
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.
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
Pierre Gaston
2017-10-15 05:30:11 UTC
Permalink
Post by Jerry
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.
do
expands to the number of elements, so it will be only one number, this loop
is useless here

a much better way wood be to directly loop on the elements of the array
for elem in "${array[@]}";do
echo "${elem}" >> "${file}"
done

or slightly more efficient
for elem in "${array[@]}";do
echo "${elem}"
done >> "${file}"

since echo can cause trouble (eg if elem=-n) it's better to use printf, in
fact printf can print multiple arguments
Post by Jerry
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
The problem is here, this just doesn't work, the keyword you would need
would be "indirection"
or name reference.

Before that though, a simpler solution is to pass the elements something
like:

array2file () {
local file elem
file =$1
shift
for elem in "$@";doe
echo "$elem"
done >> "$file"
}

array2file "filename.txt" "${array[@]}"


with indirections it could be something like:

array2file () {
declare -n array2file_array="$2"
printf "%s\n" "${array2file_array[@]}" >> "$1"
}

array2file filename.txt array
Eduardo A. Bustamante López
2017-10-15 13:35:17 UTC
Permalink
On Sun, Oct 15, 2017 at 08:30:11AM +0300, Pierre Gaston wrote:
[...]
Post by Pierre Gaston
Before that though, a simpler solution is to pass the elements something
array2file () {
local file elem
file =$1
shift
echo "$elem"
done >> "$file"
}
v
Just a small correction, the space here: `file =$1' should be removed.
Matthew Cengia
2017-10-15 21:17:22 UTC
Permalink
Also "do", not "doe".
Post by Pierre Gaston
[...]
Post by Pierre Gaston
Before that though, a simpler solution is to pass the elements
something
Post by Pierre Gaston
array2file () {
local file elem
file =$1
shift
echo "$elem"
done >> "$file"
}
v
Just a small correction, the space here: `file =$1' should be removed.
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
Loading...