steve <
shmarto...@ticnet.com> wrote:
> It's got to be something simple, but I just haven't found the way to
> do it.
Perhaps the previous responses will solve your problem. A portable,
robust solution is *not* so very simple, as evidenced by the following
wrapper script that I long ago scripted as part of a function library
to solve just such a situation.
Like all these kind of functions, it's designed to be designated
an "autoloadable" function (q.v. the manpage) -- and yes, I also
wrote an "autoload" emulation function for bourne shell as well!
It's probably too much for you, but the embedded documentation
gives valuable background information about why this became
such an issue in the first place.
I did not invent the technique which this function uses, but it *was*
so ubiquitous that I'm surprised that it has not already been
proffered, or is easily searched for.
Besides the specific code snippets and book which have already
been mentioned, the book that I have enjoyed the most for its vast
collection of non-trivial shell snippets, is:
Peek, Jerry & Tim O'Reilly & Mike Loukides.
"UNIX Power Tools". 2nd Ed. O'Reilly and Associates. <http://
www.oreilly.com/catalog/upt2/>.
=Brian
#! /bin/echo error: only source
#*TAG:40746-4:1974-12-29:0644:echon:
# Author: "Brian Hiles" <
b...@iname.com>
# License: copyright (c) 2001-2003
# Date: 2001-01-01
# Description: portable version of echo when no terminating newline is
desired
# Name: echon
# Project: @(#)echon.sh 1.3.365 199804
b...@iname.com (Brian Hiles)
# Requires: echo
# See-also:
ftp://ftp.uu.net/usenet/comp.sources.unix/volume9/printf
# See-also:
ftp://ftp.uu.net/usenet/comp.sources.unix/volume17/printf
# Usage: echon [string]...
# Version: 1.03365
#XXX should I really be caching results? How [in]efficient?
#XXX really should have a portable way of indicating -* output string
#XXX BSD "echo -n -n" prints "-n"!!
#XXX consider eliminating caching of ECHON
#01
echon() # [string]...
# portable echo without terminating newline
{ case ${ECHON:=`echo -n X`} in
-n*) # SysV echo emulation
echo "$@"\\c ;;
X) # BSD echo emulation
echo -n "$@" ;; # Bug: "echo -n -- X" prints "-- X" !
*) # cannot determine...
ECHON=
return 1 ;;
esac
return 0
}
#02 EMBEDDED MAN-PAGE FOR "src2man"
: '
!++
NAME
echon - portable echo when no terminating newline is desired
SYNOPSIS
echon [string]...
DESCRIPTION
Echon serves to print with no newline in a completely portable and
efficient fashion, regardless of the Unix implementation or whether
echo is an external command or a builtin.
One would think that because "echo" is a builtin in modern shells
would guarantee a consistent option syntax for printing with no
terminating newline. Unfortunately, because System V Unix and BSD
Unix have a version of /bin/echo that implement different syntax
to print with no terminating newline, scripts that desire to do
so may need to be manually configured.
SunOS has an "echo" builtin that will emulate either behavior
depending on whether /usr/5bin appears before /usr/bin in the PATH.
Again unfortunately, this is but a half-measure and a behavior of
only
that OS. The "print" builtin in ksh(1) has a consistent interface,
but scripts intended to be portable and distributable are generally
scripted in sh(1).
ENVIRONMENT
ECHON - caches result of test between System V echo and BSD echo.
SEE ALSO
eread(3S)
BUGS
If echon has been previously executed and variable PATH is then
changed, echon(3S) may erroneously use the wrong echo syntax. If
PATH is changed, unset variable ECHON at the same time. Do not
export variable ECHON as a sub-process may have a different PATH
value; let echon(3S) instead re-cache the value of variable ECHON.
!--
'
: <<\!!!!
From the ksh93 FAQ:
Q12. Why does ksh93 have "print" since "echo" already is widely used?
A12. The behavior of "echo" varies from system to system. The POSIX
standard does not define the behavior of "echo" when the first
argument
begins with a "-" or when any argument contains a "\" character. This
makes "echo" pretty useless for use in portable scripts.
# compute prompt strings
xx=`echo -n no-; echo nl`
case $xx in
no-nl) echon='-n'; echoc='' ;;
*) echon=''; echoc='\c' ;;
esac
# output without a newline
prompt() echo $echon "$1$echoc"
function printf #
{ [ $# -lt 1 ] && echo "usage: $0 <format> [arg1 ...]" && return
typeset a args fmt=${1%,}
shift
for a
do args="$args,\"${a%,}\""
done
nawk "BEGIN { printf \"$fmt\" $args }" /dev/null
}
!!!!