I'm writing a shell script (Bash on Mac OS X) to rename a bunch of image files. I want the results to be:
frame_001
frame_002
frame_003
etc.
Here is my code:
let framenr=$[1 + (y * cols * resolutions) + (x * resolutions) + res]
echo $framenr:
let framename=$(printf 'frame_%03d' $framenr)
echo $framename
$framenr
looks correct, but $framename
always becomes 0
. Why?
The let
command forces arithmetic evaluation, and the referenced "variable" does not exist, so you get the default value 0.
y=5
x=y; echo $x # prints: y
let x=y; echo $x # prints: 5
Do this instead:
framenr=$(( 1 + (y * cols * resolutions) + (x * resolutions) + res ))
echo $framenr:
# if your bash version is recent enough
printf -v framename 'frame_%03d' $framenr
# otherwise
framename=$(printf 'frame_%03d' $framenr)
echo $framename
I recall reading somewhere that $[ ]
is deprecated. Use $(( ))
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With