From a0ed1fb8e76217c1013acff9169ba987c0c5044a Mon Sep 17 00:00:00 2001 From: mo Date: Sat, 25 May 2019 19:31:52 -0600 Subject: check multiple hostnames at once --- bin/problem-7.sh | 25 ++++++++++++++++--------- test/problem-7.bats | 6 ++++++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/bin/problem-7.sh b/bin/problem-7.sh index 4e53180..8ec6e0c 100755 --- a/bin/problem-7.sh +++ b/bin/problem-7.sh @@ -5,13 +5,20 @@ if [ $# -eq 0 ]; then exit 1 fi -destination="$1" -host=$(host "$1" | grep 'has address') -ip=$(echo "$host" | cut -d' ' -f4) -ping -c1 $ip > /dev/null 2>&1 +check() { + destination="$1" + host=$(host "$1" | grep 'has address') + ip=$(echo "$host" | cut -d' ' -f4) + ping -c1 $ip > /dev/null 2>&1 -if [ $? -eq 0 ]; then - echo "$1" is on the network -else - echo "$1" is NOT on the network -fi + if [ $? -eq 0 ]; then + echo "$1" is on the network + else + echo "$1" is NOT on the network + fi +} + +for i in "$@"; do + check "$i" +done +exit 0 diff --git a/test/problem-7.bats b/test/problem-7.bats index 6b5e193..061df2e 100644 --- a/test/problem-7.bats +++ b/test/problem-7.bats @@ -19,3 +19,9 @@ load test_helper assert_failure "error: a hostname is required" } + +@test "invoke with multiple hostnames" { + run problem-7.sh localhost invalid + + assert_success $'localhost is on the network\ninvalid is NOT on the network' +} -- cgit v1.2.3