Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have written a zsh script to automize an analysis in high energy physics and now I would like to use an element of the one of the defined array some string and another element of some another array in a command that is passed to one of the string. My code follows below:

bkgarr=(TopJets BosonJets DiBoson TTbar)
sigarr=(NM1 NM2 NM3 Scenario4 Scenario6)
puarr=(50PU 140PU)
lumarr=(30 300 3000)

echo Please type 1 for 50PU samples and 2 for 140PU samples
read PU
if [[ $PU -ne 1 && $PU -ne 2 ]] ; then
    echo You have to enter 1 or 2
    return 1
fi

echo Please type 1 for 300fb-1 and 2 for 3000fb-1
read lum

if [[ $lum -ne 1 && $lum -ne 2 ]] ; then
    echo You have to enter 1 or 2
    return 1
fi

if [ $PU = 1 ]; then
    let "lum = $lum + 1"
    #echo $lum
fi

root -l << EOF
.L readerSummerStd.C+
.q
EOF

ex NEWrunReader.py  <<EOEX
  :43s/Lumi.*/Lumi=$lumarr[lum]/
  :x
EOEX

echo Press any key to proceed or Ctrl+C to abort!
read
for index in $bkgarr
do
    screen -dmS $index"_"$lumarr[lum]
    #screen -S $index -p 0 -X stuff "$(typeset -p bkgarr)"$'\r'
    screen -S $index"_"$lumarr[lum] -p 0 -X stuff "./NEWrunReader.py SummerStd $puarr[PU]_$index >& $index"_"$lumarr[lum].txt &"$'\r'
done
for sigind in $sigarr
do
    screen -dmS $sigin"_"$lumarr[lum]
    #screen -S $sigind -p 0 -X stuff "$(typeset -p bkgarr)"$'\r'
    screen -S $sigin"_"$lumarr[lum] -p 0 -X stuff "./NEWrunReader.py SummerStd $puarr[PU]_$sigind >& $sigind"_"$lumarr[lum].txt &"$'\r'
done
return 0

I thought the following code snippets would do, but they failed:

$index+"_"+$lumarr[lum]
$index"_"$lumarr[lum]

I would appreciate it if you could help me with this.

share|improve this question
    
Change $lumarr[lum] to ${lumarr[$lum]} and retry. – cuonglm Aug 31 '14 at 16:00
2  
Are you sure your script is being interpreted by zsh. screen -S $sigin"_"$lumarr[lum] would work in zsh. – Stéphane Chazelas Aug 31 '14 at 18:14
up vote 1 down vote accepted

Use this:

"${index}_${lumarr[lum]}"

Generally:

  1. Interpolate all variables using ${...} notation.
  2. Unless you expressly want to use word-splitting, always enclose variable interpolations in double-quoted strings.
share|improve this answer
    
Thanks for the answer, I am at a bit of lost with the technical terms interpolate and word-splitting. – Vesnog Aug 31 '14 at 16:03
    
Interpolation means replacing $index with TopJets. Word splitting means replacing the single word $bkgarr with the 4 words TopJets BosonJets DiBoson TTbar instead of the single word "TopJets BosonJets DiBoson TTbar". – aecolley Aug 31 '14 at 16:05
    
Thanks, I got it now. I would like to use word-splitting always so should I enclose the elements of the array in double quotes? I am asking this because my second for loop does not work after the modification and five screen is terminating messages are displayed in stdout after some delay. – Vesnog Aug 31 '14 at 16:30
    
If you want word-splitting, don't use double-quotes. The second for loop has a typo of sigin for sigind; is that it? – aecolley Aug 31 '14 at 16:49
    
I think it was some sort of typo introduced using search and replace function of the text editor. It works fine now, but I am not sure of the exact problem I just copied the above for loop which was working fine. – Vesnog Aug 31 '14 at 16:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.