How to access & use command line utilities in bash scripts? (spec. SoX)
Hi all,
Stumped on a project. My question is: how do I use command line utilities in a bash script? I'm writing a script that I want to access SoX. I want to iterate through a directory of .ogg files, grab the length of each file, do some math to determine the speed at which to process the file so that all of the files in the directory will be the same length. Here's what I've got: Code:
#!/bin/bash Code:
trackLength=sox --i -D "$i" Code:
./test.sh: line 5: --i: command not found I next tried putting the first SoX call in quotes, which, when run, only echos the call itself not the integer I'm looking for. How do I go about doing this? Any help is greatly appreciated. Many thanks in advance. |
Quote:
Perhaps running Code:
sox --i -D file.ogg Code:
trackLength=sox --i -D file.ogg You need to wrap commands in $() in order to run them that way, just like you're doing with your printf command a few lines down. A BASH script is nothing more than a sequence of ordinary commands, all of which can be run on the normal command line in the exact same way. Also, Code:
trackSpeed=(${trackLength} / 1000) Code:
trackSpeed=$(echo "$trackLength / 1000" | bc -l) |
Thanks!
Yes, I meant "sox --i -D file.ogg". Should have clarified. Also, thanks for pointing me in the direction of bc. That would have been my next question. |
Code:
trackSpeed=(${trackLength} / 1000) 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. :scratch: 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 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. |
All times are GMT -5. The time now is 12:45 AM. |