1

I want to batch processing a bunch of wav files with SoX (to Adjust their volume). I face a problem with assign the Volume Adjustment output from sox file.wav -n stat -v. I tried with this:

I found there are many examples on web about this operation, but these examples are not so complex like data=$(data). I tried VOLUME_ADJUST="$(sox $1 -n stat -v)" but the result output to my terminal not assign to the variable. When I echo it nothing shows up.

Thanks for your suggestion. My code is as follows:

#!/bin/sh

RED='\033[0;31m'
NC='\033[0m'
BLUE='\033[0;34m'
GREEN='\033[0;32m'

Process () {
    printf "Process with ${RED}$1${NC}\n";
    printf "Volume up as high as possible?[y/n]";
    read PASS;
    if test "$PASS" = "y"; then
        a="$(date)"
        VOLUME="$(sox "$1" -n stat -v)"
        #echo $VOLUME
        # eval "sox -v $VOLUME_ADJUST $1 $1"
        #printf $?
    elif test "$PASS" = "n"; then
        printf "${GREEN}Volume up skipped.${NC}"
    fi
}

Process "test.wav"

Here is a pastebin to all of my code

Alfabravo
  • 7,493
  • 6
  • 46
  • 82
Page David
  • 1,309
  • 2
  • 14
  • 25
  • 1
    I bet `sox` prints the output to standard error, not standard output. This is probably a duplicate but I'm drawing a blank right now. For the solution, see e.g. http://stackoverflow.com/questions/962255/how-to-store-standard-error-in-a-variable-in-a-bash-script – tripleee Feb 08 '17 at 08:26
  • @tripleee Thanks! `VOLUME=$( { sox $1 -n stat -v; } 2>&1);` works fine. – Page David Feb 08 '17 at 09:01
  • You don't need the braces for a single command. – tripleee Feb 08 '17 at 09:08
  • Please add the solution **as an answer** and mark it as accepted. :) – Alfabravo Jun 06 '17 at 20:45

1 Answers1

1

Here is my understanding.

For some reason sox print data in the stderr but not stdout. When we get value from a command like var=$(date) the message from stdout is assigned to the variable but message from stderr will not be assigned to. In order to get the value, it is easy to redirect stderr to stdout: just add 2>&1 after the command. In addtion we can direct stdout to stderr, like var=$(date 1>&2) the result printed and nothing in $var if we echo it.

Page David
  • 1,309
  • 2
  • 14
  • 25