0

I am not sure this is doable?

But i have 2 functions

do_get_array() {
        getArray "/root/1.txt"
        for e in "${array[@]}"
        do
                do_otherfunction $e
        done

}

do_otherfunction() {
        cmd="Print Me $e"
}



getArray() {
    i=0
    while read line # Read a line
    do
        array[i]=$line
        i=$(($i + 1))
    done < $1
}

echo "cmd"

SO, passing the parameter from 1 function to the other works.. but i am not sure how to loop until it the last $e in the array? Not sure i explained it correct. It Echo's out the 1st $e but the rest dont. it just stops.

1
  • 1
    There are three functions. What do you want to achieve? Commented Jun 10, 2014 at 16:27

1 Answer 1

1

It Echo's out the 1st $e but the rest dont. it just stops.

You probably meant to do the echo inside the function?

do_otherfunction() {
    cmd="Print Me $e"
    echo "$cmd"  ## Or simply echo "Print Me $e"
}

Or perrhaps you want to save all messages to the variable:

do_otherfunction() {
    cmd=${cmd}"Print Me $e"$'\n'
}

It would be inefficient though.

Just some suggestions:

function do_get_array {
    get_array "/root/1.txt"
    for e in "${array[@]}"; do
        do_other_functions "$e"
    done
}

function do_other_functions {
    echo "Print Me $e"
}

function get_array {
    if [[ BASH_VERSINFO -ge 4 ]]; then
        readarray -t array
    else
        local line i=0
        while IFS= read -r line; do
            array[i++]=$line
        done
    fi < "$1"
}
Sign up to request clarification or add additional context in comments.

5 Comments

What would be a better way. I basically have a couple of commands at the bottom of my bash script that ones the function is done it continues at the end of the script.
What function do you refer to? And what exactly do you want to achieve? Perhaps add more codes or details to your question?
At the end of the bash script (not a function) there are a set of commands, echo, etc.. I guess it WOULD be smart to make it a function. But how would i loop the function for every string in the array?
Aren't you doing that with do_other_functions already? Or perhaps you want to alter the contents of the array? You can do it by accessing indices instead with for i "${!array[@]}"; do do_other_functions "$i"; done then in your functions access array[$1]. I'm sorry if I can't follow what you mean much. Perhaps giving more examples and adding comments to what you want to do would help.
Perfect, i made a function to do the end result and its working just fine. thank you guys!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.