Hugues Moretto-Viry dixit:
>I'm slowly switching from bash to mksh. So far, I like mksh and it's a real
>pleasure to use it.
Thanks!
>I sent this email because I need some informations about PS1 and printf().
>I came on IRC channel but it was kinda quiet.
I saw you, but when I got back to IRC you just had left.
On IRC, one normally sticks around a little longer, since
there are different timezones, people working or away from
keyboard, and all that.
But no problem, eMail is just fine as well.
>I write many shell scripts and I try to make them POSIX friendly. That's
>why I'm using printf() instead of echo. I saw mksh has no built-in printf
>command (I don't want to include printf.c before compilation).
That's somewhat commendable; printf is not really portable
either anyway.
On the other hand, if you’re going to use mksh features,
you should not try to make the scripts POSIX friendly any
more; for example, many invocations of sed, awk and cut
can be better (and quicker) expressed in the shell itself,
and, most importantly, [[ is not only faster than [ and
test (despite all of them being builtins) but also more
secure.
I work on mksh not because I like working on a shell, but
because I like to use this shell, both interactively and
for my scripts. At some point, I decided to not make the
scripts portable but the shell. I do not regret this at
all, rather the contrary, I stick to this decision.
Of course, if you still want/need to write POSIX compatible
scripts, there are other possibilities… something like this:
if test -n "$KSH_VERSION"; then
echon() {
print -nr -- "$@"
}
else
echon() {
printf '%s' "$*"
}
fi
This provides a function “echon” with the native shell
method to display a string without a trailing newline
(like “echo -n” which POSIX doesn’t guarantee); of course
you can also switch on e.g. $BASH_VERSION to use the
native GNU bash methods in it, too.
>I could use echo. Sometimes I need to add color but "echo -e" isn't
>supported by POSIX echo.
>So, I think I'm stuck. Any advices?
In the Korn Shell, “print” is the native feature to use.
Actually, mksh has no less than *three* implementations
of “echo”, some supporting “-e”, some not, some defaulting
to it, and which you get is highly dependent on some flags
and invocation magic, so using “echo” is generally wrong.
>Another question, I'm trying to export my bash PS1 to mksh. On bash, I can
>apply a color on the typed command and another color on the output.
>I use the following line: "trap "printf '\e[0m'" DEBUG". I tried to export
>it on mksh, but I failed.
>It seems trap DEBUG isn't implemented on mksh, do you know a hack for my
>need?
Normally, people add the respective colours to their PS1
directly… indeed there is no DEBUG trap. The manpage, if
you search for PS1, at the second occurrence of this word
has some examples for how to add colours to a PS1. If you
rather use a PS1 that works for you in GNU bash, which uses
only PS1 (with \[\e[0m\] and all that) and not the DEBUG
trap, I will convert it for you (I offer this PS1 conversion
service since some time).
Does that help?
bye,
//mirabilos
--
FWIW, I'm quite impressed with mksh interactively. I thought it was much
*much* more bare bones. But it turns out it beats the living hell out of
ksh93 in that respect. I'd even consider it for my daily use if I hadn't
wasted half my life on my zsh setup. :-) -- Frank Terbeck in #!/bin/mksh