0

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.

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

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

exit 0

Any thoughts on what I may be writing incorrectly?

4
  • Leave the do out and put a semicolon between if [ "${#files[@]}" -gt 0 ]; then or put the then onto the next line.
    – Videonauth
    Commented 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
    Commented Jun 15, 2016 at 16:57
  • The dash shell doesn't support arrays. Are you sure that's what you want to use?
    – muru
    Commented Jun 15, 2016 at 17:36
  • @muru Sorry, that was a mistake on my side. I am removing dash.
    – Justin B
    Commented Jun 15, 2016 at 17:44

1 Answer 1

1

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

$ a=*
$ echo "$a"
*

To get an array, you need to use parentheses:

a=(*)

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:

files=(/satellite/MSGVIS6/*) 

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
    echo "No files downloaded. New area files will not be created."
    
  • The exit 0 is pointless.
4
  • I was actually using a shell script instead of bash. Will the shell script not work in this case?
    – Justin B
    Commented 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
    Commented Jun 15, 2016 at 17:58
  • The shebang was run as shell... "#!/bin/sh"
    – Justin B
    Commented Jun 15, 2016 at 18:01
  • On Ubuntu, /bin/sh is dash, which, as I already said, doesn't support arrays.
    – muru
    Commented Jun 15, 2016 at 18:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .