Created
January 12, 2022 17:00
-
-
Save garfieldnate/26121c5a2d3e78428a0cb3d5af9b955e to your computer and use it in GitHub Desktop.
assert command output in Bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
pass() { | |
message=$1; | |
# bold green | |
echo -e "$(tput setaf 2)$(tput bold)$message$(tput sgr 0)" | |
} | |
fail() { | |
message=$1; | |
# bold red | |
echo -e "$(tput setaf 1)$(tput bold)$message$(tput sgr 0)" | |
} | |
# assert that output of cmd contains search_string | |
# example: | |
# >assert_outputs "docker run hello-world" 'Hello from Docker!' 'Docker installed successfully' 'Docker install failed' | |
assert_outputs() { | |
cmd=$1; | |
search_string=$2; | |
success_msg=$3; | |
fail_msg=$4; | |
if $cmd | grep -q "$search_string"; | |
then | |
pass "$success_msg"; | |
else | |
fail "$fail_msg"; | |
exit 1; | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment