Thank you very much,
technosauras and
amigo for you quick response and help.
Sorry for not saying "thanks" for so long time. I tried your script soon after your post, but then I was so busy both at work and at home.
I further modified your script and have learned few other thinks. I will put few comments about that here as it can be useful for others and I still have some questions.
1. Version with "touch -c -r" (with some problems)
Version of "touch" command in Quirky also support both "-c" and "-r" switch. It even list them in help (I just did not use/noticed them before). (The version of touch in Quirky is 6.9.) Therefore I decided to base the script on "touch -c -r", as suggested in last post of technosaurus. I took line 1-4 from previous post and replaced all "STAMP" lines just with "touch -c -r $FILE rotated/$FILE". Basically, this script works, but I have found few problems:
1) The first one was just lack of my knowledge. I know basic redirection of std output to file, but did not understand all
" 2>...". Only later I have learned that this is part of redirection, too - neccessary for redirecton of errors.
2) But even after that: the script works fine for single extension, but for multiple extension (*.jpg *.JPG *.jpeg *.JPEG) the script still makes error messages "Invalid argument" and I was not able to solve that. Even the original script of Technosaurus with "stat" command and all "STAMP" lines makes the same error. Just did not know why. And because meantime I have found that my sister's camera use JPEG extension, while mine use JPG, now I really want script for all 4 extensions.
2. Final (?) version with "touch -c -r" + improved error handling + rotation angle as input parameter
I came to decision that it would be nice to be able to choose between 90 and 270 angle = having single script instead of two almost identical scripts. Therefore I needed rotation angle as input parameter. I hope the script bellow is the final version (I did some testing, but not very extensive yet). There are still few thinks not very clear for me, see Comments and questions later.
1) After learning (a little) how to process input parameter, it was not so difficult to implement it (including default 90 angle if no input parameter is given). It is just 3 short lines of codes.
2) I also add basic error checking and information messages. (It is not foolprof checking for all possible errors.) Expression "in 90|180|270)" was most difficult for me and the syntax with no "(" but with ")" is still strange for me.
3) It is seems enough (after some tests) to write just "mkdir -p rotated" even if directory exists, therefore I ommit " 2>/dev/null" from technosaurus script.
4) After finding the correct syntax for testing existence of a file: if [[ -e $FILE ]]; then
it was quite easy to modify core lines of the script.
5) Surprisingly, the most difficult (or at least most time consumig) was to find some working syntax for counting number of files.
Code: Select all
#!/bin/sh
#=== Loseless rotate of all photos in current dir, maintaing original filestamp ========#
# Optional input parameter: rotate angle clockwise 90 or 180 or 270 (default = 90) #
# Rotated photos are created in subdir "rotated" of current directory #
#=======================================================================================#
if [[ $# -gt 0 ]]; then ANGLE=$1
else ANGLE=90
fi
case $ANGLE in 90|180|270)
echo "Processing photos in dir: $PWD"
echo "Rotation angle (clockwise): $ANGLE";;
*) echo "Loseless rotate of all photos in current dir."
echo "Rotate angle must be 90 (default) or 180 or 270 clockwise."
exit 1;;
esac
mkdir -p rotated
i="0"
for FILE in *.JPG *.jpg *.JPEG *.jpeg; do
if [[ -e $FILE ]]; then
jpegtran -copy all -rotate $ANGLE -optimize $FILE > rotated/$FILE
touch -c -r $FILE rotated/$FILE
i=$(( $i + 1 ))
fi
done
echo "Number of rotated photos: $i"
Few additional comments and questions
1) "btw Rox has a nifty shortcut to open the terminal in the current directory...". (technosaurus' tip)
Yes, I know that. But anyway 95% of time I am working in two-panel file manager (now Tux Commander: it has only basic functions, but it resembles Total Commanders most, I have tried several others) and it is also easy to launch rxvt in specific directory from Tux.
2) "apparently busybox touch in wary _does_ support -c and -r ... it just doesn't show up in the help text ... quite annoying yet all too commonplace"
I am quite suprised that there is such discrepancy or inconsistency (or what is the proper English word) in parameters for basic Linux commands. So far I know just few commands, but I was convinced that GNU CoreUtils are VERY standardised for years. (I am aware that Puppy has BusyBux for many common commands (and these are missing some switches) and only some "full" versions.)
3) How "touch -c -r" is changing filestamp of rotated images?.
a) The only thing that really suprised me is that even on partition with FAT32 (I have photos on such partiton) there are still "Linux style" atime, mtime and ctime attributes.
b) Looking at these attributes after the script run might be a little confusing at first, but logical at second look. Mtime in this case is always the oldest. It equals to the time of creating photo in camera: that is exactly what I want! (I want to see at first glance, when I created the photo.) Ctime (not important in this case) for the photo BEFORE rotation (of course) equals to the date/time of transfer of photos to the PC and for the photo AFTER rotation to the run time of the script. Atime (not important at all) obviously depends on whether this attribute is updated for particular partition or not.
Question 1: Maybe I really badly mistundestood that part of documentation for BASH, therefore I would like to know:
a) When assigning "zero" to some variable, is there any difference between i="0" and i=0 ?
b) And is (for me horrible) syntax
i=$(( $i + 1 )) the only one for increasing variable by one?
I was trying things like "i=i+1" or "i=$i+1" or "i++" or "$++" (see also Question 2).
Question 2: I am really confused when to reference variable with $ and when not. The only case which seems clear for me is passing variable to another program: like "jpegtran -copy all -rotate $ANGLE ...." or to echo command. Can this be formulated to some simply rules?
Question 3: I want to have one separate directory for all personal scripts. What is the right init script file for adding such directory to PATH?
I certainly do not want to take too much time of
amigo or
technosaurus (you already helped me a lot) or anyone else, but I would really appreciate to clarify remaining questions. It is my first script (not counting few very simple ones without any loop or conditional statements), therefore I think it is understandable thate some topics still confused me even after study of Bash documentation quite a lot.
Thank you very much in advance for your help.