Re: [mu TECH] a 'printf' trick for scripts

From: Alfie Costa (agcosta@gis.net)
Date: Thu Jul 06 2000 - 20:28:07 CEST


On 5 Jul 2000, at 16:52, Michele Andreoli <mulinux@sunsite.auc.dk> wrote:

> There is a further method, using escape code in elvis:
>
> # cr="
>
> " (this is decimal 10, \n"
>
> To input escape-code as control-L in "vi":
>
> control-V control-L

That also works, but is not easy to read, (or edit, because invisible
characters aren't hard to accidentally delete), and keeping the code on the
left margin disorganizes '3D' code. After more testing, it seems that:

        CR=`echo`

...sadly does not work as expected -- I didn't test it before posting it! For
shame. But this works...

        CR=`echo -e "\n"`

...I think it is the quotes around the '\n' which help, somehow. I got this
trick from reading your 'rna' script, which uses it -- the '\n' is useful for
lots of things...

------

Here's a BEFORE/AFTER example from 'rna' with another use of $CR with 'printf'.
Multiple lines in a single format string...

BEFORE:

<snip>
fmt="%-15s: %s"

case $MODE in
Mailreader)
printf "$fmt" "From" "$From" > $TMP/hdr.cnt
echo >>$TMP/hdr.cnt
printf "$fmt" "To" "$To" >> $TMP/hdr.cnt
echo >>$TMP/hdr.cnt
printf "$fmt" "Cc" "$Cc" >> $TMP/hdr.cnt
echo >>$TMP/hdr.cnt
printf "$fmt" "Subject" "$Subject" >> $TMP/hdr.cnt
echo >>$TMP/hdr.cnt
;;
Newsreader)
printf "$fmt" "From" "$From" > $TMP/hdr.cnt
echo >>$TMP/hdr.cnt
printf "$fmt" "Newsgroups" "$Newsgroups" >> $TMP/hdr.cnt
echo >>$TMP/hdr.cnt
printf "$fmt" "To" "$To" >> $TMP/hdr.cnt
echo >>$TMP/hdr.cnt
printf "$fmt" "Subject" "$Subject" >> $TMP/hdr.cnt
echo >>$TMP/hdr.cnt
printf "$fmt" "Organization" "$Organization" >> $TMP/hdr.cnt
echo >>$TMP/hdr.cnt
;;
esac

(
printf "\
===============================================================================\
"
echo ) >>$TMP/hdr.cnt
<snip>

AFTER:

<snip>
fmt="%-15s: %s$CR"
HeaderBar="\
===============================================================================\
$CR"

case $MODE in
        Mailreader)
                printf "$fmt$fmt$fmt$fmt$HeaderBar" \
                        "From" "$From" "To" "$To" \
                        "Cc" "$Cc" "Subject" "$Subject" ;;
        Newsreader)
                printf "$fmt$fmt$fmt$fmt$fmt$HeaderBar" \
                        "From" "$From" "Newsgroups" "$Newsgroups" \
                        "To" "$To" "Subject" "$Subject" \
                        "Organization" "$Organization" ;;
esac > $TMP/hdr.cnt

<snip>

This "AFTER" code snippet shows how 'case...esac' is like a subshell; it allows
redirection and pipes. Because of this, just one '> $TMP/hdr.cnt' will do the
work of many. I'm still tweaking 'rna' though...

---------------------------------------------------------------------
To unsubscribe, e-mail: mulinux-unsubscribe@sunsite.auc.dk
For additional commands, e-mail: mulinux-help@sunsite.auc.dk



This archive was generated by hypermail 2.1.6 : Sat Feb 08 2003 - 15:27:15 CET