Code:
trackSpeed=(${trackLength} / 1000)
Not to mention that this isn't
arithmetic expression syntax anyway, but the pattern for setting an
array.
For the most part it looks like pretty good. Just a fcouple more points, starting with how I can't get "
sox --i" to work for me. For some reason it balks at the syntax.
But "
soxi -D" does work.
Second, always remember to quote your variables and other parameters. It may not matter much here, since you're only dealing with digits, but it's very important to get into the habit of doing it properly.
The rest is mostly just a matter of streamlining (untested, but should work):
Code:
#!/bin/bash
for i in *.ogg; do
trackLength=$( soxi -D "$i" )
echo "$trackLength"
trackSpeed=$( bc <<<"scale=8 ; $trackLength / 1000" )
echo "$trackSpeed"
printf -v new "converted/%03d.ogg" "$(( ++a ))"
sox "$i" "$new" speed "$trackSpeed"
done
In case you aren't aware, "
++a" is the
pre-increment operator. It adds one to the variable just before it's used. So the first time you use it it will equal 1. The opposite
post-increment operator "
a++", adds the number just after it's used.
And as you can see,
bash's version of
printf has an option for directly setting the output to a variable, instead of printing it to stdout.
I added a
scale to the
bc calculation to specify the decimal precision, instead of the "
-l" mathlib option used above. And I used a
here string to pass it the expression, instead of
echo+pipe. It's a little cleaner and means one less forked sub-shell.