Managing output... echo or printf?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Managing output... echo or printf?
# 1  
Old 08-26-2014
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:

Code:
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:

Code:
        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?
# 2  
Old 08-26-2014
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:

Code:
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.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 08-26-2014
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 Smilie

Major Thanks!!!

P.S. Got ya for the stderr Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Availability: echo vs. printf?

Hello, For some reason i dont remember, i currently believe (but beeing unsure) that printf is available on more diffrent systems (unix, bsd, linux, ??) than echo is. Could someone please enlighten me, whether this is true or not? Thank you PS: I just found pages about the diffrences of... (3 Replies)
Discussion started by: sea
3 Replies

2. Shell Programming and Scripting

Grep behaves diffrent upon printf or echo output

Hello I want to check whether certain arguments were passed to the script, and when those are, not doing a log entry. If those arguments are not passed, always do a log entry (*new call*). What currently i have is this: echo "${@}"|grep -q \\- || \ tui-log -e "$LOG" "\r---- New call $$... (4 Replies)
Discussion started by: sea
4 Replies

3. Shell Programming and Scripting

Excel table like output with printf

Hi All; I try to create a excel like table with headers and some fields containing values, other long and complex mathematic formulas. I have some header like : Name Formula Value True/False Under name column, they are some formula names, formula column some long mathematic formulas... (9 Replies)
Discussion started by: reseki
9 Replies

4. Shell Programming and Scripting

managing output on a web page

Legends, I want to send the output to a webpage after execution of script. the webpage layout will be something like below HOSTNAME at <TIME> -------------------- ProcessName1 <some text> ProcessName2 <some text> ..... ..... Pls help how i will approach... (1 Reply)
Discussion started by: sdosanjh
1 Replies

5. Shell Programming and Scripting

printf/echo in a second script

This may be little confusing. I have Script1, which pulls data from the system and creates another script(lets say script2). While I run script1 I need to add printf/echo statements for script2, so that when I run script2 I see those statement. eg: script1 765 printf " display frame-$1 timeoffset... (2 Replies)
Discussion started by: miltonrods
2 Replies

6. Shell Programming and Scripting

printf Hexadecimal output

printf "%X\n" "A" 41 printf "%X\n" "2" 2 Expected 32 (not 2). Is there a "printf" which will output the hexadecimal value of a numeric character? (9 Replies)
Discussion started by: methyl
9 Replies

7. Shell Programming and Scripting

\n in ksh using echo & printf

#!/usr/bin/ksh var1="Hi World" var2="Morning" var3=$(echo "$var1" \n "$var2") echo $var3 var3=$(printf "$var1 \n $var2") echo $var3 Output Any way to get in my $var3 ? (7 Replies)
Discussion started by: dahlia84
7 Replies

8. Programming

capture the output of printf into another variable

Hi , I wonder if in java I can pipe the below output of the printf into a variable: System.out.printf(" This is a test %s\n", myVariable); I want to keep the output of the printf command to create my history array. Thanks. (2 Replies)
Discussion started by: arizah
2 Replies

9. Shell Programming and Scripting

advanced echo/printf

I want to print a colored line using bash. I want to print: Smtp status where "Smtp status" will be in yellow and will be in green. Thanks. (2 Replies)
Discussion started by: proactiveaditya
2 Replies

10. UNIX for Advanced & Expert Users

Help please...output problems with printf.

#include <stdio.h> #include <math.h> // this function calculates the volume of a Cylinder int main(void) { int r; // radius int h; // height double M_PI; // pi int pOne = pow (r, 2); // get user input of radius and height printf ("Enter your... (3 Replies)
Discussion started by: pwanda
3 Replies
Login or Register to Ask a Question