Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I was expecting

excludes="${excludes:-( ${default_excludes[@]} )}"

to be an array if $excludes is empty. Unfortunately the stuff after :- is taken to be a string. Did I miss some syntax contortion, or do I have to use the clunky

if [ -z "${excludes:-}" ]
then
    excludes=( "${default_excludes[@]}" )
fi

?

share|improve this question
1  
Maybe they didn't like the :-(... –  l0b0 May 9 '11 at 14:02
add comment

1 Answer

up vote 2 down vote accepted

UPDATE: I've been testing this further...It is behaving oddly!!, or as you mention, it may not be the right syntax contortion :)

I'm starting to think that this construct is not appropriate for arrays... It works when x is unset, but I've just discoverd that it behaves oddly when x is set .. The LHS 'x' is assigned to just the first elemnet of the RHS 'x' array ... Perhaps := may do the trick...

UPDATE 2: I'm convinced that this won't work with arrays (but if I'm wrong, it wouldn't be the first time) ... I've added more tests to the script.. and the nett result is that whenever x is assigned a value via this method, it is only assigned the $x / $x[0] value....

An interesting page: The Deep, Dark Secrets of Bash


#
echo "===============1  'manually' set Y"
i=0; for T in A "B B" "C   C   C" ; do
   Y[$i]="$T"; echo Y[$i]="${Y[i]}" ; ((i++)) 
done
#
echo "===============2a  'manually' set x"
unset x
i=0; for T in 2 "2 2" "2   2   2" ; do
   x[$i]="$T"; echo x[$i]="${x[i]}" ; ((i++)) 
done
#
echo "===============2b  (X is already set); ':-'set to Y array, but should keep previous X vals" 
x=("${x:-${Y[@]}}"); for ((i=0;i<${#x[@]};i++)) ; do 
   echo x[$i]="${x[i]}"
done
#
echo "===============2c  (X is unset); ':-'set to Y array, and should take on new Y vals"
unset x
x=("${x:-${Y[@]}}"); for ((i=0;i<${#x[@]};i++)) ; do 
   echo x[$i]="${x[i]}"
done
#
echo "===============3a  'manually' set x"
unset x
i=0; for T in 3 "3 3" "3   3   3" ; do
   x[$i]="$T"; echo x[$i]="${x[i]}" ; ((i++)) 
done
#
echo "===============3b  (X is already set); ':='set to Y array, but should keep previous X vals" 
echo "${x:=(${Y[@]})}" >/dev/null ; for ((i=0;i<${#x[@]};i++)) ; do 
   echo x[$i]="${x[i]}"
done
#
echo "===============3c  (X is unset); ':='set to Y array, and should take on new Y vals" 
unset x
echo "${x:=(${Y[@]})}" >/dev/null ; for ((i=0;i<${#x[@]};i++)) ; do 
   echo x[$i]="${x[i]}"
done
echo "==============="
echo
#

The output is:

===============1  'manually' set Y
Y[0]=A
Y[1]=B B
Y[2]=C   C   C
===============2a  'manually' set x
x[0]=2
x[1]=2 2
x[2]=2   2   2
===============2b  (X is already set); ':-'set to Y array, but should keep previous X vals
x[0]=2
===============2c  (X is unset); ':-'set to Y array, and should take on new Y vals
x[0]=A
x[1]=B B
x[2]=C   C   C
===============3a  'manually' set x
x[0]=3
x[1]=3 3
x[2]=3   3   3
===============3b  (X is already set); ':='set to Y array, but should keep previous X vals
x[0]=3
x[1]=3 3
x[2]=3   3   3
===============3c  (X is unset); ':='set to Y array, and should take on new Y vals
x[0]=(A B B C   C   C)
===============
share|improve this answer
    
eval "${var:-${varArr[@]}" ? Just guessing, don't have time to test ;-) –  shellter Apr 9 '11 at 19:10
    
@shellter: eval is evil. It might work for simple arrays, but I wouldn't want to try to handle all the special strings which could break it. –  l0b0 Apr 12 '11 at 11:41
add comment

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.