help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Passing elements to function


From: Tim Visher
Subject: Re: [Help-bash] Passing elements to function
Date: Sat, 14 Oct 2017 20:21:30 -0400

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.

On Sat, Oct 14, 2017 at 5:13 PM, Jerry <address@hidden> wrote:

> 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]