Redirect output of 'if'

Started by steveTu, July 05, 2018, 01:51:23 PM

Previous topic - Next topic

steveTu

Good evening/morning/night/afternoon  all.
First time (and hopefully last if all goes ok) of posting here....
I have a simple script that searches my photos for tags with certain values - I want that script to output the filename and tags that then match into another file that I can process at some point.

The line that does the processing is simply:
...
exiftool -n -if "\$$tag =~ /${tagValue}/i" -p "${printFormat}" -r ${imageDir} |tee -a "${exifFile}"
...

The printFormat file has the file name and tags defined. All that works ok (the script find the photos with the right tag/tag value combinations) apart from the tee into my output file. So if I remove the tee, the listed output is what I want. I think I need the tee to be after the 'if' and before the recursive command, and I've tried a couple of attempts but am not having any joy.
I want to see what the script is doing, so I don't want a simple redirect (having said that, even the simple redirect of the command in the same place didn't work anyway - presumably for the same reason).

Thanks,
Steve T




Phil Harvey

Hi Steve,

I've never used "tee", but it sounds like you should be able to get what you want by sending the output to a temporary file, then doing what you want and deleting the file afterwards.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

steveTu

Phil,
Sorry for being stupid, but what option outputs to a temporary file?

Steve T

Phil Harvey

That is done by a shell redirection:

SOME_COMMAND > out.txt

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

steveTu

Sorry Phil, I wasn't used to redirection to a file being described as a 'temporary file'!
I should have said before posting that I'm from a Linux environment, so piping and redirection are common in scripting - and the tee command I used is the same as piping (|) but it creates a 'T' in the pipe meaning the output goes to two places (in my case on the screen and to the file I wanted to create). It was the redirection and piping that seemed to be causing me the issue yesterday as I thought the output wasn't appearing - but it turns out it was - it was just slower and the messages came up in a different order (if I put a tee in the pipe, or redicrect the output, I get two or three warning messages appear BEFORE the printformat messages and if I leave the pipe/redirection off, all the messages come out intermingled).
So, I think I'm fine - apologies for wasting your time.

In case it's of any use, the 'bash' script I ended up with was:

#! /bin/bash
# Reads exif tags in images files, finds a specific keyword and creates a list of files carrying that tag.......
# Normally run as 'exiftool.job Keywords Chris'

tag=${1:-Keywords}
tagValue=${2:-Value}
imageDir=${3:-/home/u3/MYIMAGES/MYPHOTOS/IMPORT}
exifDir=${4:-~/EXIFTOOL}
date=`date +'%Y%m%d'`
exifFile=${5:-${exifDir}/pf_${date}_"${tagValue}".txt}
printFormat=${6:-${exifDir}/printFormat.txt}

# This gives me a clue (in a few months time why/what I ran the job for and what the output then is)....
echo "Run Args: (1=${tag}) (2=${tagValue}) (3=${imageDir}) (4=${exifDir}) (date=${date}) (5=${exifFile}) (6=${printFormat})" > ${exifFile}

# -- Create the printformat file
# -- The printformat file contains the format command to layout the output from exiftool......
echo "# This is a comment" > ${printFormat}
echo "File: <\$FilePath/\$FileName> - \$$tag" .> "${printFormat}"

# This matches the tag to the tag value, ignoring the case (the trailing i) and matching to word boundaries (the two \b) .....
exiftool -n  -p "${printFormat}" -r ${imageDir} -if "\$$tag =~ /\b${tagValue}\b/i"  | tee -a "${exifFile}"


Phil Harvey

Quote from: steveTu on July 06, 2018, 08:15:34 AM
Sorry Phil, I wasn't used to redirection to a file being described as a 'temporary file'!

Ah.  I see.  It is temporary if you erase it afterwards. ;)

Quotethe tee command I used is the same as piping (|) but it creates a 'T' in the pipe meaning the output goes to two places (in my case on the screen and to the file I wanted to create).

That's what I had guessed, so I didn't really understand why you were asking the question.

QuoteI thought the output wasn't appearing - but it turns out it was - it was just slower and the messages came up in a different order

This explains it.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).