Search code examples
shellpipingdev-null

Shell Script output piping in test does not work


I'm trying to control error output of a command parametrically but piping command is handled as another parameter. Test scripts below;

...$ cat printOutput.sh 
#!/bin/sh
if [ $# -gt 0 ]; then echo "Wrong parameter $1"; exit; fi
echo "stdout"
echo "errout" >&2

...$ cat test.sh
#!/bin/sh
cmdBase="./printOutput.sh"
if [ -z $1 ]; then
    #Do not pipe
    cmd="$cmdBase"
else
    #Pipe err
    cmd="$cmdBase 2>/dev/null"
fi
echo "`$cmd`"

Error output should only print when --verbose option is selected but it prints anyways.Test script shows that 2>/dev/null piping is handled as a parameter.

...$ ./test.sh --verbose
Wrong parameter 2>/dev/null

...$ sh -x ./test.sh --verbose
+ cmdBase=./printOutput.sh
+ [ -z --verbose ]
+ cmd=./printOutput.sh 2>/dev/null
+ ./printOutput.sh 2>/dev/null
+ echo Wrong parameter 2>/dev/null
Wrong parameter 2>/dev/null

Why/How is the piping handled as an argument here?


Solution

  • It appears to me that output redirection is handled before the variable expansion.

    The most obvious way is to handle this within your if statement:

    #!/bin/sh
    cmdBase="./printOutput.sh"
    
    if [ -z $1 ]; then
       #Do not pipe
       cmdOut="`$cmdBase`"
    else
       #Pipe err
       cmdOut="`$cmdBase 2>/dev/null`"
    fi
    
    echo "$cmdOut"