I'm quite new at bash and I am trying to learn it by creating some small scripts.

I created a small script to look up the DNS entry for multiple domains at the same time. The domains are given as attributes.

COUNTER=0
DOMAINS=()

for domain in "$@"
do
    WOUT_WWW=$(dig "$domain" +short)
    if (( $(grep -c . <<<"$WOUT_WWW") > 1 )); then WOUT_WWW="${WOUT_WWW##*$'\n'}" ; fi

    WITH_WWW=$(dig "www.${domain}" +short)
    if (( $(grep -c . <<<"$WITH_WWW") > 1 )); then WITH_WWW="${WITH_WWW##*$'\n'}" ; fi


    DOMAINS[$COUNTER]="$domain|$WOUT_WWW|$WITH_WWW"
    COUNTER=$(($COUNTER+1))
done

Now I just want to loop through the new "multidimensional" array and give the output like mysql table:

+------------------------------+
| Row 1    | Row 2  | Row 3    |
+------------------------------+
| Value    | Value  | Value    |
+------------------------------+

How can I do that?

share|improve this question
    
The output of those dig commands may contain multiple lines (like dig www,google.com +short). Do you want to support that? – Stéphane Chazelas Sep 21 '16 at 12:14
    
Eh, no. I forgot to mention, that I filter out only the last line (in probably all cases it's the IP address) – Matt Backslash Sep 21 '16 at 12:15
    
@StéphaneChazelas I added the "filtering" in the question-code – Matt Backslash Sep 21 '16 at 12:19
1  
See also: unix.stackexchange.com/q/310900/117549 – Jeff Schaller Sep 21 '16 at 12:38
up vote 5 down vote accepted

Using perl's Text::ASCIITable module (also supports multi-line cells):

print_table() {
  perl -MText::ASCIITable -e '
    $t = Text::ASCIITable->new({drawRowLine => 1});
    while (defined($c = shift @ARGV) and $c ne "--") {
      push @header, $c;
      $cols++
    }
    $t->setCols(@header);
    $rows = @ARGV / $cols;
    for ($i = 0; $i < $rows; $i++) {
      for ($j = 0; $j < $cols; $j++) {
        $cell[$i][$j] = $ARGV[$j * $rows + $i]
      }
    }
    $t->addRow(\@cell);
    print $t' -- "$@"
}

print_table Domain 'Without WWW'    'With WWW' -- \
            "$@"   "${WOUT_WWW[@]}" "${WITH_WWW[@]}"

Where the WOUT_WWW and WITH_WWW arrays have been constructed as:

for domain do
  WOUT_WWW+=("$(dig +short "$domain")")
  WITH_WWW+=("$(dig +short "www.$domain")")
done

Which gives:

.---------------------------------------------------------------------.
| Domain            | Without WWW    | With WWW                       |
+-------------------+----------------+--------------------------------+
| google.com        | 216.58.208.142 |                 74.125.206.147 |
|                   |                |                 74.125.206.104 |
|                   |                |                 74.125.206.106 |
|                   |                |                 74.125.206.105 |
|                   |                |                 74.125.206.103 |
|                   |                |                  74.125.206.99 |
+-------------------+----------------+--------------------------------+
| stackexchange.com |  151.101.65.69 | stackexchange.com.             |
|                   |   151.101.1.69 |                   151.101.1.69 |
|                   | 151.101.193.69 |                 151.101.193.69 |
|                   | 151.101.129.69 |                 151.101.129.69 |
|                   |                |                  151.101.65.69 |
+-------------------+----------------+--------------------------------+
| linux.com         |  151.101.193.5 | n.ssl.fastly.net.              |
|                   |   151.101.65.5 | prod.n.ssl.us-eu.fastlylb.net. |
|                   |    151.101.1.5 |                   151.101.61.5 |
|                   |  151.101.129.5 |                                |
'-------------------+----------------+--------------------------------'
share|improve this answer
    
Wow. That's awesome! That's exactly what I was looking for. I already figured out a solution in bash, but this one is way better. Thank you! – Matt Backslash Sep 21 '16 at 12:52

I tried a lot of variations and done some researches. Following code is working great for me:

for DOMAIN in "${DOMAINS[@]}"; do
    printf "%-8s\n" "${DOMAIN}"
done | sed -e 's/|/_|g' | column -t -s '_' | awk '1;! (NR%1){print "---------";}'

Just looping through my array. I'm using sed because I have a delimiter in my array-values to split them up. But column was that what I was searching for.

share|improve this answer
1  
! (NR%1) is a strange obfuscating way of saying true... – JJoao Sep 21 '16 at 15:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.