|
Home > Archive > Unix Shell > November 2006 > error in bash array
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
error in bash array
|
|
| osiris@abydos.kmt 2006-11-28, 7:29 am |
| So I tried creating a array of the opions to the find command below
(as Chris suggested) but I am getting this error:
+ EXCLUDE=(${EXCLUDE[@]} . "${CWD}" "${CWD}.html" "${CWD}.png")
+ BOOLE=-and
+ J=0
+ '[' 0 -lt 11 ']'
+ '[' 1 -eq 11 ']'
++ echo
+ CONSTRUCT=
+ '[' '' '!=' '' ']'
+ OPT[${J}]=(-name "${EXCLUDE[$J]}" $BOOLE)
myscript: line 108: OPT[${J}]: cannot assign list to array member
On the command line, I can assign a list to an array element
so why can't I do it in the script?
The scalar, $CONSTRUCT, was my first attempt to get around the error
by assigning the string (all the array elements) to a scalar variable.
In the first iteration of the while loop, the array content should be null,
thus $CONSTRUCT is assigned null the first time around (as you see above).
The if-else was my second attempt to get around the error. It too failed.
Originally, I had just one line instead of the six lines you now see
in the middle of the loop (the var assignment and if-else).
Below is the actual code:
declare -a OPT
...
EXCLUDE=( ${EXCLUDE[@]} . "${CWD}" "${CWD}.html" "${CWD}.png" )
BOOLE="-and"
#
# In the loop below, construct the options for the find command.
#
J=0
while [ $J -lt ${#EXCLUDE[@]} ] ; do
if [ $(( $J + 1 )) -eq "${#EXCLUDE[@]}" ] ; then
BOOLE="-print"
fi
CONSTRUCT="${OPT[@]}"
if [ "$CONSTRUCT" != "" ] ; then
OPT[${J}]=( "$CONSTRUCT" -name "${EXCLUDE[$J]}" $BOOLE )
else
OPT[${J}]=( -name "${EXCLUDE[$J]}" $BOOLE )
fi
J=$(( $J + 1 ))
done
#
CMD='find . -maxdepth 1 '
...
"${CMD} ${OPT[@]}" |
Thanks for any help.
| |
| Chris F.A. Johnson 2006-11-28, 7:24 pm |
| On 2006-11-28, osiris@abydos.kmt wrote:
> So I tried creating a array of the opions to the find command below
> (as Chris suggested) but I am getting this error:
>
> + EXCLUDE=(${EXCLUDE[@]} . "${CWD}" "${CWD}.html" "${CWD}.png")
> + BOOLE=-and
> + J=0
> + '[' 0 -lt 11 ']'
> + '[' 1 -eq 11 ']'
> ++ echo
> + CONSTRUCT=
> + '[' '' '!=' '' ']'
> + OPT[${J}]=(-name "${EXCLUDE[$J]}" $BOOLE)
> myscript: line 108: OPT[${J}]: cannot assign list to array member
That error message would be clearer if written: "cannot assign
array to array member".
> On the command line, I can assign a list to an array element
> so why can't I do it in the script?
You cannot do it on the command line, either.
> The scalar, $CONSTRUCT, was my first attempt to get around the error
> by assigning the string (all the array elements) to a scalar variable.
> In the first iteration of the while loop, the array content should be null,
> thus $CONSTRUCT is assigned null the first time around (as you see above).
> The if-else was my second attempt to get around the error. It too failed.
> Originally, I had just one line instead of the six lines you now see
> in the middle of the loop (the var assignment and if-else).
>
> Below is the actual code:
>
>
> declare -a OPT
> ...
>
> EXCLUDE=( ${EXCLUDE[@]} . "${CWD}" "${CWD}.html" "${CWD}.png" )
> BOOLE="-and"
> #
> # In the loop below, construct the options for the find command.
> #
> J=0
> while [ $J -lt ${#EXCLUDE[@]} ] ; do
> if [ $(( $J + 1 )) -eq "${#EXCLUDE[@]}" ] ; then
> BOOLE="-print"
> fi
>
> CONSTRUCT="${OPT[@]}"
> if [ "$CONSTRUCT" != "" ] ; then
> OPT[${J}]=( "$CONSTRUCT" -name "${EXCLUDE[$J]}" $BOOLE )
> else
> OPT[${J}]=( -name "${EXCLUDE[$J]}" $BOOLE )
You cannot assign an array to a member of an array. You can do:
OPT=( -name "${EXCLUDE[$J]}" $BOOLE )
Or:
OPT[${J}]="-name ${EXCLUDE[$J]} $BOOLE "
> fi
>
> J=$(( $J + 1 ))
> done
> #
> CMD='find . -maxdepth 1 '
>
> ...
> "${CMD} ${OPT[@]}" |
>
> Thanks for any help.
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
|
|
|
|
|