Shell script failing for unknown reasons

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Shell script failing for unknown reasons Sun Mar 09, 2014 5:02 pm • by Zettt
Hi,

I'm executing a shell script on a file. My idea is to use Hazel to process a folder containing a text file and maybe a photo to create automatic journal entries with it. The script runs fine when I run it in Terminal, it doesn't execute in Hazel. I don't even get an error. I run this in /bin/sh.

Code: Select all
journalentry=$(grep -v -e "Written with major efforts on an iPhone." -e "Written with major efforts on an iPad." "$1")

if [[ -e "$HOME/Dropbox/Apps/Attachments/photo.JPG" && -e "$1" ]]; then
   echo "$journalentry" | /usr/local/bin/dayone -p="$HOME/Dropbox/Apps/Attachments/photo.JPG" new
   rm "$HOME/Dropbox/Apps/Attachments/photo.JPG"
elif [[ ! -e "$HOME/Dropbox/Apps/Attachments/photo.JPG" && -e "$1"]]; then
   echo "$journalentry" | /usr/local/bin/dayone new
fi


The first grep command is to filter out my usual email signature. The other commands should be self explaining. What am I missing here?

Thanks,
Zettt
Zettt
Zettt
 
Posts: 30
Joined: Wed Aug 29, 2007 8:50 am

Re: Shell script failing for unknown reasons Mon Mar 10, 2014 9:40 am • by rjames86
Found the error :)

Looks like you're missing a space here

Code: Select all
elif [[ ! -e "$HOME/Dropbox/Apps/Attachments/photo.JPG" && -e "$1"]]


Should be

Code: Select all
elif [[ ! -e "$HOME/Dropbox/Apps/Attachments/photo.JPG" && -e "$1" ]]


I also found on my specific setup that DayOne didn't think I had a default location set. I had to add the additional param for it to work both in the CLI and in Hazel:

Code: Select all
-j=~/Dropbox/Apps/Day\ One/Journal.dayone/
rjames86
 
Posts: 7
Joined: Mon Mar 10, 2014 9:38 am

Re: Shell script failing for unknown reasons Mon Mar 10, 2014 5:16 pm • by Zettt
That's right. Oh man, syntax bugs. I love them. Thanks a ton!
Zettt
Zettt
 
Posts: 30
Joined: Wed Aug 29, 2007 8:50 am

Re: Shell script failing for unknown reasons Mon Mar 10, 2014 5:44 pm • by Zettt
OK. Here's the final version. Turns out the iOS Photos app uses "photo.JPG" when a photo is sent, and "photo.PNG" if it's a screen shot.

Explanation of this workflow can be found in my blog post.

Code: Select all
theFile=$(echo "$1")
journalentry=$(grep -v -e "Written with major efforts on an iPhone." -e "Written with major efforts on an iPad." "$theFile")
photo="$HOME/Dropbox/Apps/Attachments/photo"

if [[ -e "$photo.JPG" ]]; then
   echo "JPG"
   photo="$photo.JPG"
elif [[ -e "$photo.PNG" ]]; then
   echo "PNG"
   photo="$photo.PNG"
fi


if [[ -e "$photo" && -e "$theFile" ]]; then
   echo "$journalentry" | /usr/local/bin/dayone -p="$photo" new
   rm "$photo"
elif [[ ! -e "$photo" && -e "$theFile" ]]; then
   echo "$journalentry" | /usr/local/bin/dayone new
fi
Zettt
Zettt
 
Posts: 30
Joined: Wed Aug 29, 2007 8:50 am


Return to Support