60

I am working on a shell script with exiftool to automatically change some exif tags on pictures contained in a certain folder and I would like to use the output to get a notification on my NAS (a QNAP) when the job is completed.

Everything works already, but - as the notification system truncates the message - I would like to receive just the information I need, i.e. the last line of the shell output, which is for example the following:

Warning: [minor] Entries in IFD0 were out of sequence. Fixed. - 2015-07-12 15.41.06.jpg                                                                             
 4512 files failed condition                                                      
  177 image files updated

The problem is that currently I only receive the following notification:

Exiftool cronjob completed on Camera: 4512 files failed condition

What I would like to get instead is:

Exiftool cronjob completed on Camera: 177 image files updated

The script is the following:

#!/bin/sh
# exiftool script for 2002 problem
dir="/share/Multimedia/Camera"
cd "$dir"
FOLDER="$(printf '%s\n' "${PWD##*/}")"
OUTPUT="$(exiftool -overwrite_original -r '-CreateDate<DateTimeOriginal' -if '$CreateDate eq "2002:12:08 12:00:00"' -if '$DateTimeOriginal ne $CreateDate' *.[Jj][Pp][Gg])"
/sbin/notice_log_tool -a "Exiftool cronjob completed on ${FOLDER}: ${OUTPUT}" --severity=5
exit 0

To do that I played with the $OUTPUT variable using | tail -1, but probably I make some basic errors and I receive something like:

Exiftool cronjob completed on Camera: 4512 files failed condition | tail -1

How to do it in the right way? Thanks

giopas
  • 645
  • 1
  • 6
  • 10

2 Answers2

94

Put the tail inside the capturing parens.

OUTPUT=$(exif ... | tail -1)

You don't need the double quotes here. I'm guessing that you tried

OUTPUT="$(exif ...) | tail -1"
user464502
  • 2,203
  • 11
  • 14
  • Thanks for your answer. Unfortunately it seems not working, as I receive two notification errors now: "Exiftool cronjob completed on Camera: Syntax: exiftool [OPTIONS] FILE Consult the exiftool documentation for a full list of options." and "Exiftool cronjob completed on Camera: 4689 files failed condition". – giopas Jul 14 '15 at 16:42
  • @giopas, `exif` can't tell if it was started by a subshell or not, so there's no impact at all on its usage. Perhaps there was a typo introduced at the same time? Put `set -x` at the top of your script to enable logging, and compare the `exif` command as logged in its original form to the one as logged when in a command substitution. – Charles Duffy Jul 16 '18 at 13:04
31

Probably an old post to be answering now, but try using the -n flag (see tail --help) and wrap the command output using ticks.

OUTPUT=`exif ... | tail -n 1`

(user464502's answer did not work for me as the tail command does not recognize the parameter "-1")

IanK.CO
  • 563
  • 5
  • 13
  • 4
    At the time, I was mirroring the OP's syntax. '-n' is Posix and is preferred. The modern style is $(...) rather than backticks, though both work. – user464502 Jun 29 '17 at 22:33
  • You can [edit] your answer, rather than just describing potential improvements in comments. :) – Charles Duffy Jul 16 '18 at 13:02