I am trying to write a script in zsh and I need to make use of arrays in order to achieve my aim in an elegant way. After reading some information over the Net, I tried to implement and access the elements of array in the following way:
### Script for running everything in screens ###
killall -15 screen ## We make sure that no screens are running for now
#bkgarr=(TopJets BosonJets DiBoson TTbar)
bkgarr=(1 2 3)
screen -dmS "MYSCREEN"
screen -S "MYSCREEN" -p 0 -X stuff 'echo "The array has ${#bkgarr[*]} of elements."^M'
screen -S "MYSCREEN" -p 0 -X stuff 'echo ${#bkgarr[0]}^M'
However, this did not work and the output I get is as follows:
~/Summer_Students/nTupler_topness/tupleAnalyzer% echo "The array has ${#bkgarr[*]} of elements."
The array has 0 of elements.
~/Summer_Students/nTupler_topness/tupleAnalyzer% echo ${#bkgarr[0]}
0
Furthermore, I need to have an array of strings like the one commented out in this minimal working example. I would be glad if someone can pinpoint my mistakes and why the array is apparently not stored in computers memory.
Code edit:
### Script for running everything in screens ###
killall -15 screen ## We make sure that no screens are running for now
#bkgarr=(TopJets BosonJets DiBoson TTbar)
bkgarr=(1 2 3)
screen -dmS "MYSCREEN"
screen -S "MYSCREEN" -p 0 -X stuff "$(typeset -p bkgarr)"$'\r'
#screen -S "MYSCREEN" -p 0 -X stuff 'bkgarr=(1 2 3)^M'
screen -S "MYSCREEN" -p 0 -X stuff 'echo "The array has ${#bkgarr[*]} of elements."^M'
screen -S "MYSCREEN" -p 0 -X stuff 'echo ${#bkgarr[0]}^M'
Output afterwards:
~/Summer_Students/nTupler_topness/tupleAnalyzer% declare -a bkgarr='([0]="1" [1]="2" [2]="3")'
declare: bkgarr: can't assign initial value for array
~/Summer_Students/nTupler_topness/tupleAnalyzer% echo "The array has ${#bkgarr[*]} of elements."
The array has 0 of elements.
~/Summer_Students/nTupler_topness/tupleAnalyzer% echo ${#bkgarr[0]}
0
~/Summer_Students/nTupler_topness/tupleAnalyzer%
Thanks in advance