|
From: Fmiser <fm...@gm...> - 2012-07-22 06:47:14
|
> > Michael Karr wrote:
> >
> > I'm new to scripting...
> Pascal Giard wrote:
> To get exactly what he asked for, I'd go with:
>
> for i in *.wav; do sox $i -n spectrogram -m -o ${i%%.wav}.png;
> done
>
> This assumes that the shell is either bash or dash.
It would probably be a good idea to quote the variables. So
for i in *.wav; do sox "$i" -n spectrogram -m -o "${i%%.wav}.png"; done
And then to explain it.
The quotes I added are so it will correctly handle things like
spaces, commas, parentheses, etc.
"for" "in", "do", and "done" are special words that indicate a
loop, in this case, a for loop. Basically, all commands between
"do" and "done" are run on each pass of the loop as determined
by the list provided with "for".
"for i in *.wav" creates a list of all files that end in ".wav"
in the current directory and sets the variable "i" to be the
first of those files. Each time around the loop, "i" is set to
then next file that ends with ".wav"
In the command, the variable "i" is recalled by preceding it
with a "$", so "$i" is replaced with name of one of the wav
files, and each time around the loop it becomes a different one.
The next fancy part is "${i%%.wav}.png". This is taking the
filename that "i" is currently set to, stripping .wav from the
end, and then adding .png. This results in the creation of
a .png file that has exactly the same name as the .wav file -
but with a different extension.
Scripting can do a lot more, and I've glossed over some stuff,
but maybe this will be enough of an explanation you will be able
to rework the command (one liner script) to a new and different
situation next time. *smiles*
-- Philip
|