1

I have a small script which should print a couple of calls to a makefile i got.

mylist='$(call list_samples,AON_9,NT_1,SC_17) $(call list_samples,AON_10,NT_2,SC_18) $(call list_samples,AON_11,NT_3,SC_19) $(call list_samples,AON_12,NT_4,SC_20) $(call list_samples,AON_13,NT_5,SC_21) $(call list_samples,AON_14,NT_6,SC_22) $(call list_samples,AON_15,NT_7,SC_23) $(call list_samples,AON_16,NT_8,SC_24)'

for SAMPLES_OUT in $mylist;
do
echo "$SAMPLES_OUT"
done

Output:

$(call
list_samples,AON_9,NT_1,SC_17)
$(call
list_samples,AON_10,NT_2,SC_18)
$(call
list_samples,AON_11,NT_3,SC_19)
$(call
list_samples,AON_12,NT_4,SC_20)
$(call
list_samples,AON_13,NT_5,SC_21)
$(call
list_samples,AON_14,NT_6,SC_22)
$(call
list_samples,AON_15,NT_7,SC_23)
$(call
list_samples,AON_16,NT_8,SC_24)

the problem i am experiencing is that the for loop is splitting on spaces and therefor the $call and list_samples are taken apart while they should be actually one call. like this:

$(call list_samples,AON_9,NT_1,SC_17)
$(call list_samples,AON_10,NT_2,SC_18)
$(call list_samples,AON_11,NT_3,SC_19)
$(call list_samples,AON_12,NT_4,SC_20)
$(call list_samples,AON_13,NT_5,SC_21)
$(call list_samples,AON_14,NT_6,SC_22)
$(call list_samples,AON_15,NT_7,SC_23)
$(call list_samples,AON_16,NT_8,SC_24)

I have tried putting the strings in "" but that does not work since it is a list created with '' it sees everything between that as a string. Any hints are appreciated. Thanks!

2

You need to use an array to keep the elements together:

mylist=(
    '$(call list_samples,AON_9,NT_1,SC_17)'
    '$(call list_samples,AON_10,NT_2,SC_18)'
    '$(call list_samples,AON_11,NT_3,SC_19)'
    '$(call list_samples,AON_12,NT_4,SC_20)'
    '$(call list_samples,AON_13,NT_5,SC_21)'
    '$(call list_samples,AON_14,NT_6,SC_22)'
    '$(call list_samples,AON_15,NT_7,SC_23)'
    '$(call list_samples,AON_16,NT_8,SC_24)'
)
for SAMPLES_OUT in "${mylist[@]}"    # crucial to use quotes here
do
    echo "$SAMPLES_OUT"
done
3

You can also use a different separator from the default (the default being space, tab and newline):

mylist='
$(call list_samples,AON_9,NT_1,SC_17) 
$(call list_samples,AON_10,NT_2,SC_18) 
$(call list_samples,AON_11,NT_3,SC_19) 
$(call list_samples,AON_12,NT_4,SC_20) 
$(call list_samples,AON_13,NT_5,SC_21) 
$(call list_samples,AON_14,NT_6,SC_22) 
$(call list_samples,AON_15,NT_7,SC_23) 
$(call list_samples,AON_16,NT_8,SC_24)
'

IFS='
'       # split on newline only
set -f  # disable globbing
for samples_out in $mylist # invoke the split+glob operator
do
  printf '%s\n' "$samples_out"
done

That way, you can keep using standard sh syntax.

0

From what I can tell, the problem might be that you're including so much static data in your variable definition. It looks to me like your actual variable data is just a simple set of three numbers, each of which is incremented by one per iteration. The rest is just dressing. So maybe...

 pr_samples()( 
     for n in lmt AON NT SC
     do  : "$(($n=(${1##*[!-0-9]*}0/10)-1))"
            shift "$((!!$#))"
     done;  lmt=$((lmt+AON+1))
     until [ "$AON" -gt "$lmt" ]
     do    printf '$(call list_samples,AON_%d,NT_%d,SC_%d)\n' \
                  "$((AON+=1))" "$((NT+=1))" "$((SC+=1))"
     done
)

...which would build out your set if called like:

 pr_samples 7 9 1 17

...where the first argument is the iteration count and the next three are the initial values for $AON, $NT, and $SC respectively. A null value argument is interpreted as a zero.

Anyway, called as above, it prints:

$(call list_samples,AON_9,NT_1,SC_17)
$(call list_samples,AON_10,NT_2,SC_18)
$(call list_samples,AON_11,NT_3,SC_19)
$(call list_samples,AON_12,NT_4,SC_20)
$(call list_samples,AON_13,NT_5,SC_21)
$(call list_samples,AON_14,NT_6,SC_22)
$(call list_samples,AON_15,NT_7,SC_23)
$(call list_samples,AON_16,NT_8,SC_24)

It could be useful to pass the printf format string as an argument as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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