On Tue, Sep 16, 2008 at 9:34 AM, Christopher Sawtell <[EMAIL PROTECTED]> wrote: >> printf is nice, but over the top. > I'm curious. What makes you suggest that?
Well, a number of reasons. Number one is "simplicity", which in my mind says "echo is a good-enough command, just use it properly instead of learning another command instead". Number two *was* "POSIX says so", but apparently I'm out of date, and POSIX now recommend using printf ... http://www.opengroup.org/onlinepubs/009695399/utilities/echo.html ... so I'll keep quiet about that one. Number three is that echo is actually a shell builtin for bash, even though there is *also* a binary echo on your system. If you explicitly invoke /bin/echo, you'll get a fork(), otherwise you won't. Number four is stylistic -- I prefer using inline expansion of commands with $() to the printf example, which was to stash results into variables and then to expand them later. If the results are only going to be used in output ... well, that all depends on the particular script, doesn't it? Printf doesn't have to be used with multi-argument layout, it'll also take in-line substitution quite happily. > I am aware that in this particular and simplistic case it's nebulous, Of course :-) But these are the best things to argue over, because both sides are always right, even when they disagree :-) So, in my old-fashioned world, a simple script runs in bash (because I don't care about execution overhead -- if I did, I'd use dash, which is the default /bin/sh under Debian and understands most of the useful bash syntax), uses echo with in-line variable substitution, uses files in /tmp but tries to remember to delete them, and forgets to invoke trap() ... As an example, here's one from a Cygwin machine that tests lots of things to see which bit of the Internet has broken for me ... (to use on non-cygwin, check the location of bash and the name/options of the traceroute command) --------8<-----cut here----->8------- #!/usr/bin/bash echo "Testing Internet connectivity ..." docmd() { $1 > /tmp/$$ 2>&1 if [ $? == 0 ] then echo $2; rm /tmp/$$; return 0; else echo $3; cat /tmp/$$; rm /tmp/$$; return 1; fi } pingud() { echo -n "ping $1: " docmd "ping -n 2 $2" "$3" "$4" || exit 1; } trace() { echo -n "Traceroute $1 {$2 hops}: " docmd "tracert -d -h $2 $3" "$4" "$5" || exit 1; } connport() { echo -n "Connect to $1:$2: " docmd "nc -w 2 -z $1 $2" "$3" "$4" || exit 1; } pingud WRTv2 10.15.2.1 up down pingud "DNS WRTv2" wrtv2.lan up unresolved pingud Trango 120.89.80.94 up down pingud "Pine Hill" 120.89.80.93 up down pingud "ISP DNS primary" 202.20.97.19 ping down pingud "ISP VoIP" 161.29.135.4 ping down trace "OpenDNS IP" 3 208.67.222.222 OK Broken trace "google.com" 3 google.com OK Broken connport google.com 80 OK "No Google" connport www.bigevilcorp.com 443 OK "No Big Evil Corp" connport 10.10.10.10 80 OK "No VPN connection" --------8<-----cut here----->8------- -jim
