patternshellMinor
Directory of Snapshots
Viewed 0 times
directorysnapshotsstackoverflow
Problem
I have snapshots (photos) that I load off my camera's memory card, and store in a folder. Sometimes I have videos, and occasionally plain audio too (the camera can record just sound).
Additionally, my wife, and kids have their point-and-shoots too.
I have a workflow where I dump the photos on to a designated folder on a linux samba share, and then run this script which identifies the photos, auto-rotates the portrait picturs, and renames them according to the date and time the picture was taken (rather than a sequential number from the camera).
This way, assuming the various cameras have the correct clock-time set, the different cameras all have photos that are named similar things (and can be sorted alphabetically to get them in chronological order too.
Notes:
The script is relatively simple, but has a fair amount of repetition. Any suggestions for how to improve it?
``
jhead -anef -ft -autorot -n DSC*.jpg
else
Additionally, my wife, and kids have their point-and-shoots too.
I have a workflow where I dump the photos on to a designated folder on a linux samba share, and then run this script which identifies the photos, auto-rotates the portrait picturs, and renames them according to the date and time the picture was taken (rather than a sequential number from the camera).
This way, assuming the various cameras have the correct clock-time set, the different cameras all have photos that are named similar things (and can be sorted alphabetically to get them in chronological order too.
Notes:
- the jhead program the script calls will rename/rotate the photos, and the name will become something like:
Img20140217-164850.20.jpg
- the jhead also identifies that some pictures may have 'raw' partners, for example, my Nikon camera takes a picture called
DSC1234.JPGand simultaneously the pictureDSC1234.NEF(the raw photo). The commandjhead -aNEF -ft -autorot -n DSC.JPGwill identify the JPG photo, strip the .JPG extensions, and, because there is also the-aNEFargument, will rename the matching NEF file to the same base-name (Img) as the JPG.... in other words, for the filesDSC1234.JPGandDSC1234.JPG, the jhead program will produce, for example, bothImg20140217-164850.20.jpgandImg20140217-164850.20.NEF
- the script deals with both Canon, and Nikon raw files (
RW2andNEFrespectively)
The script is relatively simple, but has a fair amount of repetition. Any suggestions for how to improve it?
``
#!/usr/bin/tcsh -f
#cd /valuable/picxfer
# Do the .jpg first because the jhead program will write .jpg even for .JPG input.
echo 'Checking DSC*.jpg'
if ( tcsh -f -c "ls -1 DSC*.jpg >& /dev/null" && echo yes` == "yes" ) thenjhead -anef -ft -autorot -n DSC*.jpg
else
Solution
Your
You could write like this:
That is:
If you don't mind using Bash instead of Tcsh, then the ifs can be even more simple:
A lot less useless fluff.
With Bash you could simplify the repetitive blocks with a function, for example:
if conditions can be simplified. Instead of this:if ( `tcsh -f -c "ls -1 DSC*.jpg >& /dev/null" && echo yes` == "yes" ) thenYou could write like this:
if ( `ls DSC*.jpg >& /dev/null && echo x` != "" ) thenThat is:
- you don't need to spawn a
tcshsub-shell
- you don't need the
-1forlsto list files in a single column because you redirect that to/dev/nullanyway
- the `
...part will only output anything if there were files, so you don't needyes == yes`, it's enough to check for emptiness
If you don't mind using Bash instead of Tcsh, then the ifs can be even more simple:
if ls DSC*.jpg &> /dev/null; thenA lot less useless fluff.
With Bash you could simplify the repetitive blocks with a function, for example:
check_files() {
glob=$1
flags=$2
nomsg=$3
echo Checking "$glob"
if ls $glob &>/dev/null; then
jhead -a$flags -ft -autorot -n $glob
else
echo $nomsg
fi
echo
}
check_files 'DSC*.jpg' nef 'no Nikon jpg files'
check_files 'DSC*.JPG' NEF 'no Nikon JPG files'
check_files 'p*.jpg' rw2 'no Canon jpg files'
check_files 'P*.JPG' RW2 'no Canon JPG files'Code Snippets
if ( `tcsh -f -c "ls -1 DSC*.jpg >& /dev/null" && echo yes` == "yes" ) thenif ( `ls DSC*.jpg >& /dev/null && echo x` != "" ) thenif ls DSC*.jpg &> /dev/null; thencheck_files() {
glob=$1
flags=$2
nomsg=$3
echo Checking "$glob"
if ls $glob &>/dev/null; then
jhead -a$flags -ft -autorot -n $glob
else
echo $nomsg
fi
echo
}
check_files 'DSC*.jpg' nef 'no Nikon jpg files'
check_files 'DSC*.JPG' NEF 'no Nikon JPG files'
check_files 'p*.jpg' rw2 'no Canon jpg files'
check_files 'P*.JPG' RW2 'no Canon JPG files'Context
StackExchange Code Review Q#48295, answer score: 3
Revisions (0)
No revisions yet.