Managing output... echo or printf?

Hello script guru's

as i write more and more code i always block at managing output... either writing to standard out, writing to files via std out (log, temp file, etc). Don't get me wrong 99% of the time it DOES the job but maybe there is more efficient.

I'm writing a small script to help production use DNS dynamic updates (named.conf 400 lines with 300 zones so i can't fuckup) and main function goes like this:

nsupdate_ops() {
        echo "Start NSUPDATE File creation"
        local TEMPFILE=$(mktemp /tmp/nsupdate.XXXXXX)
        exec 7> ${TEMPFILE}
        #echo "server ${DNSSERVER}" >&7
        #echo "zone ${FWDZONE}" >&7
        case ${OPERATION} in
                add_rec|update_rec)
                        printf "server %s\nzone %s\nupdate delete %s A\nupdate add %s 3600 A %s\nsend\n", ${DNSSERVER} ${FWDZONE} ${CN} ${CN} ${ADDRESS} >&7                 
                        ;;
                remove_rec)
                        echo "update delete ${CN}. A" >&7
                        ;;
                add_cname)
                        echo "prereq nxrrset ${CN} A" >&7
                        echo "prereq nxrrset ${CN} CNAME" >&7
                        echo "update add ${CN}. 3600 CNAME ${CNAME}" >&7
                        ;;
                remove_cname)
                        echo "update delete ${CN}. 3600 CNAME ${CNAME}" >&7
                        ;;
        esac
#       echo "send" >&7

        exec 7>&-
        echo "END NSUPDATE File creation"

}

As you can see i use echo's to std out and fd7 and printf.

I could always use a variable to my long line to make my code tidier but my main question is... is there ANY advantages to using one or another?

Is it a question of background as printf users usually means they did C?

I didn't use any:

        cat >$TEMPFILE <<EOF
server $DNSSERVER
zone $FWDZONE
update delete ${CN}. A
update add ${CN}. 3600 A $ADDRESS
show
send
EOF

As i would have to do one block for each occurences.

I also didn't start to touch logging... is there a definate standard in the coding community for the use of /* insert best coding practice for script logging */ ?

Any ideas at making my code more efficient?

Well, echo sometimes does odd things on certain shells -- some expand escape sequences, some don't, some support options, some don't -- but printf "%s" "string" is pretty much guaranteed the same everywhere. If you know you'll always be using the "right" shell it doesn't really matter.

What goes wrong 1% of the time? 99% success rate is terrific for an exam, but horrible for a program.

I don't know if it'll be faster but it'll be simpler to let echo print to stdout than to redirect into &7 99 times:

exec 7>&1 # Save a copy of stdout into FD 7
exec 1>"${TEMPFILE}" # Redirect stdout into $TEMPFILE

        echo a
        echo b
        echo c
        echo d

exec 1>&7 # Restore stdout
exec 7>&-

As for logging, data belongs in stdout, human-readable errors belong in stderr, don't mix the two.

1 Like

That one percent would be something i would have missed and not the behavior of echo (i need more experience with my coding).

I forgot about that std out to fd and going on with std echo's.... See THAT is EXACTLY the type of info i knew i was forgeting :smiley:

Major Thanks!!!

P.S. Got ya for the stderr :slight_smile: