Passing An Array From One Bash Script to Another
Asked Answered
A

5

12

I am new to writing Shell Scripts and am having some difficulties.

What I Want To Achieve

I have an array of strings in scriptOne.sh that I want to pass to scriptTwo.sh

What I Have Done So Far

I can execute the second script from inside the first using ./scriptTwo.sh and I have passed string variables from one to the other using ./scriptTwo.sh $variableOne.

The issues are when I try to pass an array variable it doesn't get passed. I have managed to get it to pass the first entry of the array using ./scriptTwo.sh "${array[@]}" however this is only one of the entries and I need all of them.

Thanks in advance for your help

Allness answered 15/4, 2013 at 15:45 Comment(1)
Related: #31727205Ralf
N
14

Your way of passing the array is correct

./scriptTwo.sh "${array[@]}"

The problem is probably in the way how you receive it. In scriptTwo.sh, use

array=("$@")
Nanon answered 15/4, 2013 at 15:51 Comment(3)
What if I pass multiple arrays to this second script? Is there a way to declare which one ie arrayOne=("$1@") etcAllness
@DiscoS2: Passing multiple arrays is much more problematic. It might be possible to prepend the array size before each of the arrays and then fill in the arrays in the second script, though.Nanon
Thanks for the reply. I decided to change the way I passed the data from one script to another as it made it easier in the long wrong but thanks for your answer. It definitely helped!Allness
L
0

I think we can directly read the array content from sriptOne.sh if you use a declare command.

I declare an Associative array named wes_batch_array in scriptOne.sh and I want to pass it to scriptTwo.sh so I put the command below in scriptTwo.sh:

declare -A wes_batch_array=$(awk -F '=' '{if ($1 ~ /^declare -A wes_batch_array/) {for (i=2;i<NF;i++) {printf $i"=";} printf $NF"\n";}}' < scriptOne.sh)

I've tested this command and it works fine.

If you do not use the declare command. You can echo the content of array to another middle temp file and use the awk command above to grab the content of bash array.

Lebkuchen answered 26/5, 2020 at 7:56 Comment(0)
K
0

The way I solved this problem was to step through the array in script1.sh, then pass the array elements to script2.sh using a for loop. In my case I was passing many arrays with varying numbers of elements to the same script2.sh on different servers.

So for example:

script1.sh:

records="111111111 222222222 333333333"
myRecordsArray=($records)

for (( i=0 ; i < $(#myRecordsArray[@]} ; i++ )); do

./script2.sh ${myRecordsArray[i]}

done

script2.sh:

main () {
<some code here>
}

myRecord=$1
main $myRecord
Kemme answered 23/2, 2021 at 20:51 Comment(0)
A
0

Following up on the question about passing multiple arrays, I have found the sort of dirty but yet working solution of passing the array as a string and parsing it to an array using the IFS:

IFS=',' read -ra array1 <<< "$1"
IFS=',' read -ra array2 <<< "$2"

for element1 in "${array1[@]}"; do
    for element2 in "${array2[@]}"; do
        echo "Element 1: $element1, Element 2: $element2"
    done
done
Amabelle answered 13/8, 2023 at 19:29 Comment(0)
B
0

Passing multiple arrays can be done like wittn suggested, but he omitted an important detail: On the calling side we need to expand the arrays with the '*' character into single words, so they are captured by single argument variables in the called script, like this:

script1.sh:

#!/usr/bin/bash

array1=(
    hello
    world
)

array2=(
    fish
    love
)

./script2.sh "${array1[*]}" "${array2[*]}"

script2.sh:

#!/usr/bin/bash
IFS=' ' read -r -a array1 <<< "$1"
IFS=' ' read -r -a array2 <<< "$2"

for word in "${array1[@]}"
do
    echo array1 $word
done

for word in "${array2[@]}"
do
    echo array2 $word
done
Bufflehead answered 2/2 at 10:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.