((${#fileName} > maxLength))
${#variable} is the character length of 'variable', so this command is testing if the length is greater than maxlength.
sed "s/.*\(.\{$maxLength\}\)/\1/"
The above command evaluates to...
sed 's/.*\(.\{31\}\)/\1'
What this is doing is matching 0 or more characters ('.' is a wildcard in sed), followed by 31 more characters. The .\{31\} pattern is contained in parentheses \(\) and so the matched pattern is held in a buffer by sed. As it is the first (and in this case, only) pattern in parentheses, the related buffer is buffer 1, referenced by \1. Simply put, it is replacing the whole string with the last 31 characters of that string.
What I want to change is that the name is truncated before .jpg and that a duplicate gets a number added 00, 01, 02 and so on.
I lowered the variable maxlength to 29 to create space for this number.
Hmmm... Something like this...
maxLength=31
typeset -Z2 fileCounter=0
while :; do
for fileName in *.jpg; do
((${#fileName} > maxLength)) && {
newFileName=$(echo $fileName | sed "s/.*\(.\{$maxLength\}\)/\1/") &&
while test -f $newFileName; do
newFileName=$fileCounter$(echo $fileName | sed "s/.*\(.\{$((maxLength - 2))\}\)/\1/")
fileCounter=$((fileCounter + 1))
done
mv $fileName $newFileName
}
fileCounter=0
done
sleep 60
done