vendredi 29 octobre 2021

Collect External Output (echo) and Save as a Variable for Testing

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

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

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

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

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

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.

Aucun commentaire:

Enregistrer un commentaire