I am attempting to build a loop to check for files in a folder then run a loop if files exist in the folder and keep running the loop until no more files are left, but I am running into the token error, "syntax error near unexpected token `do'. Here is the script I am trying to run.

if [ "${#files[@]}" -gt 0 ]; then
    mcenv << 'EOF'
    imgremap.k MSGVI6I MSGVI6I.2 SIZE=ALL PRO=MERC RES=3.00
    imgcha.k MSGVI6I.2 STYPE=GVAR
    imgdel.k MSGVI6I.1
    find /satellite/awips/ -type f -name 'AREA6022*' -exec mv '{}' /awips2/edex/data/manual/ \;
    rm -rf "$files"

echo ''
echo "No files downloaded. New area files will not be created."

exit 0

Any thoughts on what I may be writing incorrectly?

  • Leave the do out and put a semicolon between if [ "${#files[@]}" -gt 0 ]; then or put the then onto the next line.
    – Videonauth
    Jun 15, 2016 at 16:55
  • @Videonauth I am looking for it to keep running until there are no more files left in the folder. Will it keep doing that without the "do loop" in place?
    – Justin B
    Jun 15, 2016 at 16:57
  • The dash shell doesn't support arrays. Are you sure that's what you want to use?
    – muru
    Jun 15, 2016 at 17:36
  • @muru Sorry, that was a mistake on my side. I am removing dash.
    – Justin B
    Jun 15, 2016 at 17:44

1 Answer 1


Assuming bash, in a simple variable assignment, globbing is not performed:

$ a=*
$ echo "$a"

To get an array, you need to use parentheses:


For example, in /usr:

$ a=(*)
$ echo "$a" "${a[@]}"
bin bin include lib lib32 lib64 local sbin share src

So, your first line should actually read:


To expand to all elements in an array a, use "${a[@]}":

rm -rf "${files[@]}"

Other notes:

  • You can use mv -t to make the find more efficient with {} +:

    find /satellite/awips/ -type f -name 'AREA6022*' -exec mv -t /awips2/edex/data/manual/ {} +
  • Just use echo instead of echo '':

    echo "No files downloaded. New area files will not be created."
  • The exit 0 is pointless.
  • I was actually using a shell script instead of bash. Will the shell script not work in this case?
    – Justin B
    Jun 15, 2016 at 17:57
  • Your shell script is run using some shell. I'm assuming you're using bash (Bourne Again Shell) for your shell script. If not, which shell are you using?
    – muru
    Jun 15, 2016 at 17:58
  • The shebang was run as shell... "#!/bin/sh"
    – Justin B
    Jun 15, 2016 at 18:01
  • On Ubuntu, /bin/sh is dash, which, as I already said, doesn't support arrays.
    – muru
    Jun 15, 2016 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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