0

I have been trying to write a program that will take the volume adjustment stat in Sox, and store it in a variable.

To do this you have to do

sox *your-audio-file* -n stat,

and the final line will show the stat that I want.

However, when I try to store the whole output of that command in the variable INITSTAT, it remains blank, and the line of code that should be storing the output in the variable is just printing the output to the terminal. This is what I have:

INITSTAT=`sox $audioFilePath -n stat`
echo $INITSTAT

Where "$audioFilePath" is the path to the audio file I am trying to get the information about.

If anybody knows what is wrong, any help would be appreciated.

  • So `sox $audioFilePath -n stat` on it's own executes as expected? As a side note, you can wrap `$audioFilePath` with `"`, to avoid issues in case a path contains spaces. – Zois Tasoulas Sep 26 '21 at 19:23
  • Try this `INITSTAT=$(sox "$audioFilePath" -n stat)` and let us know if it works. – user1984 Sep 26 '21 at 19:29
  • 1
    From Sox manual on `stat`, "The information is output to the ‘standard error’ (stderr) stream", http://sox.sourceforge.net/sox.html – Zois Tasoulas Sep 26 '21 at 19:30
  • 1
    @zois yes, `sox $audioFilePath -n stat` will work on its own. How would I be able to store the stderr in a variable? *EDIT* I was able to get it to work with `INITSTAT=$((sox $audioFilePath -n stat) 2>&1)`, hooray! thanks for telling me about the stderr thing. – SteepAtticStairs Sep 26 '21 at 19:33
  • @user1984 it doesn't work, it has the same output. I think that's because they are both ways of storing commands in a variable – SteepAtticStairs Sep 26 '21 at 19:34
  • The inner parentheses are not required. – glenn jackman Sep 27 '21 at 03:16

1 Answers1

2

I suggest to redirect stderr to stdout:

INITSTAT=$(sox "$audioFilePath" -n stat 2>&1)
Cyrus
  • 84,225
  • 14
  • 89
  • 153