I want to write a small Bash function that I can pass a command as string and a search needle and if the execution output of the command contains the given search needle, it should print some "OKAY", else "ERROR".
That's what I came up with so far:
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
function psa_test {
result=$($1 2>&1)
echo "Result: $result"
if [[ $result == *"$2"* ]]; then
echo "[${green} OK ${reset}] $3"
else
echo "[${red}ERROR${reset}] $3"
fi
echo " Command: '$1' | Needle: '$2' | Name: '$3'"
}
If I invoke it like that
psa_test 'curl -v google.com' "HTTP 1.1" "Testing google.com"
it works beautifully:
# .. output
[ OK ] Testing google.com
Command: 'curl -v google.com' | Needle: 'HTTP 1.1' | Name: 'Testing google.com'
But if I have some embedded string in my command, it doesn't work anymore, e.g.:
psa_test 'curl --noproxy "*" http://ift.tt/2lp2hDt' "HALLO 1" "HTTP www1.in.example.com"
Output:
# Proxy answers with HTML error document ... <html></html> ... saying
# that it can't resolve the domain (which is correct as
# it is an internal domain). But anyway, the --noproxy "*" option
# should omit all proxies!
[ERROR] HTTP http://ift.tt/2lSAoaF
Command: 'curl --noproxy "*" http://ift.tt/2lSAoaF' | Needle: 'HALLO 1' | Name: 'HTTP http://ift.tt/2lSAoaF'
Note that if I execute
curl --noproxy "*" http://ift.tt/2lSAoaF
in my shell does ignore proxies (which we want), while
curl --noproxy * http://ift.tt/2lSAoaF
does not.
I got some similar behaviour of i try to test a MySQL database with mysql -u ... -p ... -e "SELECT true"
. So I guess it has something to do with the quotes. Would be glad if someone can give me a hint to improve this function. Thanks!
Aucun commentaire:
Enregistrer un commentaire