0

I am looking to execute the following script below. The issue I am encountering is it will not execute anything after the do loop. It doesn't matter what I happen to have after the loop, it never executes, so I am def missing something somewhere.

Also, any suggestions on a more efficient way or writing this script? I am very new to the scripting environment and very open to better ways of going about things.

#!/bin/bash

# mcidas environment

PATH=$HOME/bin:
PATH=$PATH:/usr/sww/bin:/usr/local/bin:$HOME/mcidas/bin
PATH=$PATH:/home/mcidas/bin:/bin:/usr/bin:/etc:/usr/ucb
PATH=$PATH:/usr/bin/X11:/common/tool/bin:.

export PATH

MCPATH=$HOME/mcidas/data
MCPATH=$MCPATH:/home/mcidas/data

export MCPATH

#variables
basedir1="ftp://ladsweb.nascom.nasa.gov/allData/6/MOD02QKM" #TERRA
basedir2="ftp://ladsweb.nascom.nasa.gov/allData/6/MYD02QKM" #AQUA
day=`date +%j`
day1=`date +"%j" -d "-1 day"`
hour=`date -u +"%H"`
min=`date -u +"%m"`
year=`date -u +"%Y"`
segment=1
count=$(ls /satellite/modis_processed/ | grep -v ^d | wc -l) 
count_max=25
files=(/satellite/modis_processed/*)



if [ $hour -ge "17" ]; then

workinghour="16"
echo "Searching for hour $workinghour"
url="${basedir2}/${year}/${day1}/MYD02QKM.A${year}${day1}.${workinghour}*.006.${year}*" 
wget -r -nd --no-parent -nc -e robots=off  -R 'index.*' -P /satellitemodis/ $url 
#find /satellite/modis/ -type f -mmin -30 -exec cp "{}" /satellite/modis_processed/ \;

for files in /satellite/modis_processed/*
do

    echo "The number used for the data file is ${count}"
    echo "The number used for the image file is ${segment}"

    export segment
    export count

    #Run McIDAS
mcenv <<- 'EOF'
imgcopy.k MODISD.${count} MODISI.${segment} BAND=1 SIZE=SAME
imgremap.k MODISD.${segment} MODISI.${segment} BAND=1 SIZE=ALL PRO=MERC 
imgcha.k MODISI.${segment} CTYPE=BRIT
exit
EOF

    segment=`expr ${segment} + 1`
    count=`expr ${count} - 1`

    #Reset Counter if equal or greater than 25
    if [[ $segment -ge $count_max ]]; then
    segment=1
    fi

    find /satellite/awips -type f -name "AREA62*" -exec mv "{}" /awips2/edex/data/manual/ \; 
done;

echo "We have exported ${segment} converted modis files to EDEX."
fi
WxJack
  • 21
  • 6
  • 1
    Do you get any error messages? You seem to have one `fi` too many. – that other guy Jul 25 '16 at 19:27
  • I recall that, it's bash (marked as `/bin/sh`, a should-be shooting offence ...). But I still see nothing that makes the loop terminate, since `files` is never modified. – dhke Jul 25 '16 at 19:35
  • @that-other-guy I made a mistake when adding it to stackoverflow. I removed one of the closing `fi`. I am not receiving any errors. I wasn't sure if I had nested the `if` statement wrong or what, but I am not getting anything syntactically wrong on my end. – WxJack Jul 25 '16 at 19:36
  • @dhke the shebang should be bash instead? – WxJack Jul 25 '16 at 19:38
  • See http://stackoverflow.com/help/mcve -- including the "minimal" part: Code examples should be trimmed down to the smallest possible thing that can demonstrate the same problem. (Likewise, "verifiable": Code samples should be made to operate in environments other than the OP's, so anyone with a proposed solution can test whether that solution actually does fix the problem in practice). – Charles Duffy Jul 25 '16 at 19:41
  • @WxJack Yeah, the script uses arrays and at least another bashism (test -f on array). But the main problem seems to be that I see nothing that makes `while [ -f "$files" ]` terminate, which happens more or less only when `/satellite/modis_processed/` is cleaned out. – dhke Jul 25 '16 at 19:42
  • 1
    @WxJack, yes, the shebang absolutely should be bash -- you're using arrays in here, and the POSIX sh standard doesn't provide them. – Charles Duffy Jul 25 '16 at 19:42
  • 2
    BTW, http://shellcheck.net/ is your friend. – Charles Duffy Jul 25 '16 at 19:42
  • @CharlesDuffy thank you very much -- I will change it up and I will def use that site in the future. @dhke would it be better to change it to `for files in /satellite/modis_processed/*` or would that not make any difference? – WxJack Jul 25 '16 at 19:48
  • I changed to bash, but I am still running into the same issue... the final line of the script is not displaying. ShellCheck is not popping up with any significant errors. – WxJack Jul 25 '16 at 21:51
  • This [mcve] is working for me. Maybe in your original script something larger is causing it. Can you test it by replacing `mcenv <<-EOF` with `cat<<-EOF`? If the test passes and prints the last line then the issue is with mcidas. – alvits Jul 25 '16 at 22:40
  • @alvits I commented the EOF stuff out totally and it still does not execute the last echo. I think it is something with the loop itself. – WxJack Jul 26 '16 at 00:14
  • Well, the [mcve] is working. Must be something with your original code that we cannot guess. – alvits Jul 26 '16 at 01:25
  • I actually rewrote the script from scratch. It was driving me crazy trying to figure this out. It appears to be working correctly now. I appreciate everyone's time on this. Thank you kindly. – WxJack Jul 26 '16 at 03:31

1 Answers1

0

You have a here-document in that script. That here-document is not properly terminated. The end marker, EOF, needs to be in the first column, not indented at all.

If you indent it, it has to be with tabs, and the start of the here-document should be <<-'EOF'.

The effect of the wrongly indented EOF marker is that the rest of the script is read as the contents of the here-document.

As Charles Duffy points out, ShellCheck is your friend.

Kusalananda
  • 14,885
  • 3
  • 41
  • 52
  • It was only indented like that on stackoverflow. It was properly indented in my script. I have added the dash you mentioned, but there is no change in the output. I am still not seeing echo "We have exported ${segment} converted modis files to EDEX." when the script is run. – WxJack Jul 25 '16 at 21:48
  • @WxJack Well, what can I say. – Kusalananda Jul 26 '16 at 06:30
  • I rewrote the script from scratch and it appears to be working now. Seems like it was just a quirk in my coding somewhere. I appreciate your help regardless though. Thank you. :) – WxJack Jul 26 '16 at 17:30