Home > Software engineering >  Collect External Output (echo) and Save as a Variable for Testing
Collect External Output (echo) and Save as a Variable for Testing

Time:10-30

I have a command that should fail and output a specific echo.

IF the user submits:

./submit_script.sh path/1/to/file1 path/2/to/file2 --bat

THEN

  1. The script should fail
  2. Echo "Unrecognized argument. Possible arguments: cat, dog, human".

I am trying to save this echo in a variable in order to run a simple test case. Basically: WHEN the user runs the script containing the test case (see my strategies below)

./test_script.sh

THEN they receive an echo back stating: "Unrecognized argument test case: pass"

My strategies (I've tried these and every small variation I can think of):

1) Input: test_script.sh contains "echo" when saving the output of the submit_script.sh file into a variable.

Output: nothing is echoed when ./test_script.sh is run

bat_input=$(echo ./submit_script.sh path/1/to/file1 path/2/to/file2 --bat)   

if [[ "$bat_input" =~ "Unrecognized argument. Possible arguments: cat, dog, human" ]]; then                                         
    echo "Unrecognized argument test case: pass"                    
fi
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

2) Input: test_script.sh does not include echo when saving the output of the submit_script.sh file into a variable

Output: Echo "Unrecognized argument. Possible arguments: cat, dog, human" (not the right echo, expecting "Unrecognized argument test case: pass")

bat_input=$(./submit_script.sh path/1/to/file1 path/2/to/file2 --bat)   

if [[ "$bat_input" =~ "Unrecognized argument. Possible arguments: cat, dog, human" ]]; then                                         
    echo "Unrecognized argument test case: pass"                    
fi
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

3) Input: test_script.sh includes ">/dev/null 2>&1" when saving the output of the submit_script.sh file into a variable

Output: nothing is echoed when ./test_script.sh is run

bat_input=$(./submit_script.sh path/1/to/file1 path/2/to/file2 --bat >/dev/null 2>&1)  

if [[ "$bat_input" =~ "Unrecognized argument. Possible arguments: cat, dog, human" ]]; then                                         
    echo "Unrecognized argument test case: pass"                    
fi
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

4) Input: test_script.sh removes the quotations around the bat_input variable in the if statement

Output: Echo "Unrecognized argument. Possible arguments: cat, dog, human" (not the right echo, expecting "Unrecognized argument test case: pass")

bat_input=$(./submit_script.sh path/1/to/file1 path/2/to/file2 --bat)   

if [[ $bat_input =~ "Unrecognized argument. Possible arguments: cat, dog, human" ]]; then                                         
    echo "Unrecognized argument test case: pass"                    
fi
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

5) *Input: test_script.sh adds 's to the regex command in the if statement

Output: Echo "Unrecognized argument. Possible arguments: cat, dog, human" (not the right echo, expecting "Unrecognized argument test case: pass")

bat_input=$(./submit_script.sh path/1/to/file1 path/2/to/file2 --bat)   

if [[ "$bat_input" =~ *"Unrecognized argument. Possible arguments: cat, dog, human"* ]]; then                                         
    echo "Unrecognized argument test case: pass"                    
fi
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

In all of these cases, there is no output aside from the "Unrecognized argument. Possible arguments: cat, dog, human" echo, which I would ideally like to suppress. I don't understand why these if statements aren't triggering an echo stating ""Unrecognized argument test case: pass". Ideas? Let me know if I need to make any clarifications.

CodePudding user response:

bat_input=$(./submit_script.sh path/1/to/file1 path/2/to/file2 --bat)   

if [[ "$bat_input" =~ "Unrecognized argument. Possible arguments: cat, dog, human" ]]; then                                         
    echo "Unrecognized argument test case: pass"                    
fi
<iframe name="sif6" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

bat_input=$(./submit_script.sh path/1/to/file1 path/2/to/file2 --bat)   

if [[ $bat_input =~ "Unrecognized argument. Possible arguments: cat, dog, human" ]]; then                                         
    echo "Unrecognized argument test case: pass"                    
fi
<iframe name="sif7" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Ok, so I am pretty sure solving my question is not possible because of how bash handles strings. Ie the echo output is Unrecognized argument. Possible arguments: cat, dog, human not "Unrecognized argument. Possible arguments: cat, dog, human".

What I ended up doing is using the exit 1 status as a proxy. Ie:

bat_input=$(./submit_script.sh path/1/to/file1 path/2/to/file2 --bat)   
    if [[ $bat_input -eq 1 ]]; then
        echo "Unrecognized argument test case: pass"
    fi
<iframe name="sif8" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

This isn't great because an exit 1 error code can occur for many reasons, but it's all I have been able to come up with.

Is my issue in fact due to how bash handles strings? Or am I missing something else?

  • Related