2

I have the following simple script:

echo "-------------------------- SOA --------------------------------"
echo " "
echo -n "  ---------> "; dig soa "$1" +short | awk '{print $3}'

The output is something like this:

-------------------------- SOA --------------------------------

---------> 2019072905

Now my question is can I make an "echo" command after the dig and the output to be something like this:

-------------------------- SOA -----------------------------

            ---------> 2019072905 <-------------

I have tried to search for similar cases but was not able to find any related.

Would this be possible?

Thanks in advance.

2

I would do the whole thing in printf instead:

#!/bin/sh

header='-------------------------- SOA --------------------------'

headerLength=$(awk '{print length()}' <<<"$header")

value=$(dig soa "$1" +short | awk '{print $3}')

valueString="-----------> $value <-------------"

valueLength=$(awk '{print length()}' <<<"$valueString")

offset=$(((headerLength + valueLength)/2+1))

printf "%s\n\n%${offset}s\n" "$header" "$valueString"

This has the advantage of always appearing centered no matter what the length of your value is (using a slightly modified version that just sets value=$1 to illustrate):

$ foo.sh 2019072905
-------------------------- SOA --------------------------------

            -----------> 2019072905 <-------------
$ foo.sh "some random long string"
-------------------------- SOA --------------------------------

      -----------> some random long string <-------------
$ foo.sh "foo"
-------------------------- SOA --------------------------

              -----------> foo <-------------
  • Thanks for your advice. Will check this out with 'printf' also. – Miro Jul 30 at 12:20
4
cmd=$(dig soa "$1" +short | awk '{print $3}'; echo -n \<----------;)

echo "-------------------------- SOA --------------------------------"
echo " "
echo -n "      ---------> ";echo $cmd
  • Thanks. This is indeed something that I was looking for. – Miro Jul 30 at 12:11
2
#!/bin/sh

soa=$(dig soa "$1" +short | awk '{print $3}')

cat <<__EOF__
-------------------------- SOA -----------------------------

            ---------> $soa <-------------
__EOF__

BTW, I think the question is wrong-headed because printing all that header garbage around the SOA just makes it pointlessly harder to use the output of this script as input to another script...and even when you don't currently think you'll ever need to do that, in future you might. Verbosity in output is a "sin" in unix :) - when writing a script, you should always be thinking that your output could end up being someone else's (including yourself) input.

I would have ignored the question, but I disliked the other answer even more. If you're going to do something wrong, you may as well do it properly.

  • Thank you for your advice. Will have this in mind. Atm, this is only for personal use combining several commands in a small script. – Miro Jul 30 at 12:10
  • that's cool. i have no objection to you doing whatever you want for your own needs or education....but that answer combining the output of dig ... |awk ... with the echo statement just irked me. it's bad coding style and it's teaching bad habits. – cas Jul 30 at 12:27
-1

I don't have dig so used echo in it's place for this example:

$ echo '2019072905' | awk '
    BEGIN { d=sprintf("%15s",""); gsub(/ /,"-",d); print d d, "SOA", d d ORS }
    { printf "%*s> %s <%s\n", 2*length(d)-length($0)/2+1, d, $0, d }
'
------------------------------ SOA ------------------------------

           ---------------> 2019072905 <---------------

$ echo '201' | awk '
    BEGIN { d=sprintf("%15s",""); gsub(/ /,"-",d); print d d, "SOA", d d ORS }
    { printf "%*s> %s <%s\n", 2*length(d)-length($0)/2+1, d, $0, d }
'
------------------------------ SOA ------------------------------

              ---------------> 201 <---------------

$ echo '12345672019072905' | awk '
    BEGIN { d=sprintf("%15s",""); gsub(/ /,"-",d); print d d, "SOA", d d ORS }
    { printf "%*s> %s <%s\n", 2*length(d)-length($0)/2+1, d, $0, d }
'
------------------------------ SOA ------------------------------

       ---------------> 12345672019072905 <---------------

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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