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

Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
StuStirling
  • 15,601
  • 23
  • 93
  • 150

4 Answers4

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=("$@")
choroba
  • 231,213
  • 25
  • 204
  • 289
  • 1
    What if I pass multiple arrays to this second script? Is there a way to declare which one ie `arrayOne=("$1@")` etc – StuStirling Apr 15 '13 at 16:17
  • 1
    @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. – choroba Apr 15 '13 at 18:52
  • 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! – StuStirling Apr 16 '13 at 10:14
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.

Henry Yang
  • 11
  • 1
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
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
wittn
  • 298
  • 5
  • 16