2
\$\begingroup\$

I am running a scheduled script on my iPhone that logs in to a website, downloads a PDF, adds it to iBooks and uploads it to dropbox. All of this happens only when it has WiFi access.

I want to make it as error proof as possible. So I am open for criticism and tipps since it is my first bash script.

#!/bin/sh


DATE=$(date "+%Y%m%d")
DATE_LOCALE=$(date "+%d.%m.%Y")
#LOGDATE=$(date "+%F %T")
DOMAIN=http://url.to.site
DOMAIN_LOGOUT=http://url.to.site/logout
URL1=http://url.to.site/issuefiles/
URL2=_paper/pdfs/paper
URL3=_complete.pdf
url=$URL1$DATE$URL2$DATE$URL3
FILENAME="/path/paper"$DATE"_complete.pdf"
ITEMNAME="paper"$DATE"_complete"
COOKIEFILE=/path/cookie.txt
ICONFILE=/path/title.jpg
INFOFILE=/path/Info.plist
USERAGENT="Mozilla/5.0 (compatible; MSIE 7.01; Windows NT 5.0)"
IP_ADDRESS=$(ifconfig en0 | grep inet | cut -d: -f2 | awk '{ print $2}')
IP_ADDR_VAL=$(echo "$IP_ADDRESS" | grep -Ec '^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])')



if [ $IP_ADDR_VAL -eq 1 ]; then

  #Get the title image for the app icon
  cd /path
  wget http://path.to.site/title.jpg >/dev/null 2>&1

  #Debug last command
  if [ $? == 0 ]
  then 
    echo $(date "+%F %T") : wget successful
  else
    echo $(date "+%F %T") : wget not successful
  fi

  #Check if downloaded correctly
  if [ -e "$ICONFILE" ]
  then 
    echo $(date "+%F %T") : title.jpg downloaded
  else
    echo $(date "+%F %T") : title.jpg not downloaded
  fi

  #Get token
  TOKEN=$(curl -A "$USERAGENT" --cookie $COOKIEFILE --cookie-jar $COOKIEFILE http://url.to.site/ | grep "token" | sed -e 's/<input type=\"hidden\" name=\"token\" value=\"//' | sed -e 's/\" \/>//' )
  curl -A "$USERAGENT" --data-urlencode "username=myuser" --data-urlencode "password=mypassword" --data-urlencode "token="$TOKEN --cookie $COOKIEFILE $DOMAIN  >/dev/null 2>&1

  #Debug last command
  if [ $? == 0 ]
  then 
    echo $(date "+%F %T") : curl for token successful 
  else
    echo $(date "+%F %T") : curl for token not successful 
  fi

  #Download of epaper file
  curl -A "$USERAGENT" -L --progress-bar -o $FILENAME --cookie $COOKIEFILE $url 


  #Debug last command
  if [ $? == 0 ]
  then 
    echo $(date "+%F %T") : curl for Download of pdf successfull
  else
    echo $(date "+%F %T") : curl for Download of pdf successfull
  fi


  #logout
  LTOKEN=$(curl -A "$USERAGENT" --cookie $COOKIEFILE --cookie-jar $COOKIEFILE $DOMAIN | grep "ltoken" | sed "s/.* value=\"\(.*\)\".*/\1/")
  curl -A "$USERAGENT" --data-urlencode "ltoken="$LTOKEN --cookie $COOKIEFILE $DOMAIN_LOGOUT

  #delete cookiefile
  if [ -e "$COOKIEFILE" ]
  then
     rm $COOKIEFILE
     #Debug last command
     if [ $? == 0 ]
     then 
        echo $(date "+%F %T") : Cookie file deleted
     else
        echo $(date "+%F %T") : Cookie file not deleted
     fi
  fi

  #if file exists
  if [ -e "$FILENAME" ]
  then
    #If downloaded file exeeds 1MB it should have been a success
    #fsize=$(stat -c %s $FILENAME)
    #echo $fsize
    if [ $(stat --format="%s" "$FILENAME") -gt 1000000 ]; then
        #import to iBooks and add to newsstand
        cd /private/var/mobile/Media/Books/Purchases/
        echo $(date "+%F %T") : $(python /path/ibooks.py import $FILENAME)
        md5=($(md5sum $FILENAME))
        appdir=/private/var/stash/Applications/$md5.app

        if [ ! -e "$appdir" ]
        then    
            mkdir $appdir
            if [ -e "$ICONFILE" ]
            then
                cp $ICONFILE $appdir/Icon.png
                rm $ICONFILE
            else 
                echo $(date "+%F %T") : Error. No Iconfile available.
            fi          
            cd /path
            python parse.py $ITEMNAME $md5
            if [ -e "$INFOFILE" ]
            then
                cp $INFOFILE $appdir
                rm $INFOFILE
            else
                echo $(date "+%F %T") : Error. No Info file available
            fi  
        else
            echo $(date "+%F %T") : Appdirectory alsready exists!
        fi
        #Upload to dropbox
        echo $(date "+%F %T") : Uploading to Dropbox... 
        cd /path
        ./dropbox_uploader.sh upload $FILENAME "/DestPathOnDropbox/"$ITEMNAME".pdf"

         #Debug last command
         if [ $? == 0 ]
         then 
            echo $(date "+%F %T") : Upload to Dropbox successful!
          #curl -k -s $PROWL_URL >/dev/null 2>&1
         else
            echo $(date "+%F %T") : Upload to Dropbox not successful!
         fi


        rm $FILENAME
    else
    #Filesoize too small. Delete download
    echo $(date "+%F %T") : Filesize too small. Expected more!
    rm $FILENAME
    fi
  fi

  #Delete title.jpg
  if [ -e /path/title.jpg ]
  then
    rm /path/title.jpg
  fi

  echo $(date "+%F %T") : Done!!

else
  echo $(date "+%F %T") : No WiFi!  Aborted...
fi
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

I don't have time to look through everything, but here's some low-hanging fruit:

if [ -e /path/title.jpg ]
then
    rm /path/title.jpg
fi

can be reduced to:

rm -f /path/title.jpg

Because man rm says about -f:

ignore nonexistent files and arguments, never prompt

EDIT: Another thing. Instead of

some_command arg1 arg2 arg3

if [ $? == 0 ]
then
    ...

You can just say:

if some_command arg1 arg2 arg3
then
    ...
\$\endgroup\$
0

Your Answer

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

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