Sponsored Content
Top Forums Shell Programming and Scripting Managing output... echo or printf? Post 302914556 by maverick72 on Tuesday 26th of August 2014 03:07:49 PM
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?
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
GIT-SH-I18N(1)							    Git Manual							    GIT-SH-I18N(1)

NAME
git-sh-i18n - Git's i18n setup code for shell scripts SYNOPSIS
. "$(git --exec-path)/git-sh-i18n" DESCRIPTION
This is not a command the end user would want to run. Ever. This documentation is meant for people who are studying the Porcelain-ish scripts and/or are writing new ones. The 'git sh-i18n scriptlet is designed to be sourced (using .) by Git's porcelain programs implemented in shell script. It provides wrappers for the GNU gettext and eval_gettext functions accessible through the gettext.sh script, and provides pass-through fallbacks on systems without GNU gettext. FUNCTIONS
gettext Currently a dummy fall-through function implemented as a wrapper around printf(1). Will be replaced by a real gettext implementation in a later version. eval_gettext Currently a dummy fall-through function implemented as a wrapper around printf(1) with variables expanded by the git-sh- i18n--envsubst(1) helper. Will be replaced by a real gettext implementation in a later version. GIT
Part of the git(1) suite Git 2.17.1 10/05/2018 GIT-SH-I18N(1)
All times are GMT -4. The time now is 07:34 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy