Is there a way to implement the following using 'for' in KornShell (ksh)?
Here is the C equivalent:
for(i=1;i<20;i++)
{
printf("%d",i);
}
I was wondering if this can be implemented using just 'for' and not 'while'
I tried the following, it does not seem to work.
for i in [1-20]
do
print $i
done
Please let me know your ideas and solutions.
Unfortunately, it looks as if ksh
does not have support for range-based brace expansion or support the (( ))
construct so to do this compactly, you'll need to call the external binary seq
like so:
for i in $(seq 1 20); do
echo $i
done
Not really an answer, but an FYI to casual ksh users.
Edit 2019-05-12 (Minor edits below in bold, other info is now stricken).
To clarify on several comments here, there are 2 ksh's available in typical vendor installations (non-Linux (maybe them too?)).
Solaris and AIX have a ksh and ksh93 (probably true for other vendors too). The base ksh is also known as ksh88. Ksh93 is described in The New Kornshell Command and Programming Language, 1995
Linux systems that have a true ksh (not pdksh), mostly use ksh93 named as ksh.
Finally, to further confuse things, don't let the 1995 pub date trick you, ksh continues under was under active development by David Korn and Glen Fowler at AT&T until 2012?. Versions were released 2-3X per year. Some Linux versions pick up the newer versions.
These newer versions have very advanced features
(most of this taken from AT&T research UWIN page. search for the link 'notes and changes' (dead link) )
- compound variables composed like c structs (no c datatypes, just typeset decls) (one user claims a 500 Meg in-memory struct)
- Double precision floating point arithmetic with full C99 arithmetic ..The numbers Inf and NaN can be used in arithmetic expressions.
- TAB-TAB completion generates a numbered list of completions ...
- Support for processing/handling multibyte locales (e.g., en_US.UTF-8, hi_IN.UTF-8, ja_JP.eucJP, zh_CN.GB18030, zh_TW.BIG5 etc.) ...
- /dev/(tcp|udp|sctp)/host/sevrice now handles IPv6 addresses ...
- ... seek on a file by offset or content with new redirection operators.
- A new --showme option which allows portions of a script to behave as if -x were specified while other parts execute as usual. ...
- The [[...]] operator =~ has been added which compares the string to an extended regular expression ....
- The printf(1) builtin has been extended to support the = flag for centering a field ... (and others) ...
- view-pathing
- "Most of the utilities were developed by AT&T and conform to POSIX.2 and X/Open."
(note that ...s in above, usually indicate some qualifying information removed)
Korn and Fowler had also produced an advanced environment, UWIN (Unix for Windows) for people that use systems like Mingw or Cygwin, that would be worthy of a separate post. The downside for UWIN is,
- not same set of utilities as you find in your favorite Linux.
- Another file compilation environment that pretty much has to use MS Visual C (gcc support via Mingw is said to be on-the-way),
- a very small support community,
- the
AT&T Common Public License V 1.0 Eclipse Public License* is not GNU.
See UWin main page (dead link) : unfortunately out of date, better to nose around in the dnld link above. Hmm, this is much better Glenn Fowler's FAQ for UWin (also dead, Time Machine anyone?).
I hope this helps!
Edit 2019-05-12 . The reason for the dead links?
David Korn and Glen Fowler Laid Off (at AT&T, 2012?
Information later surfaced that they are working at Google. I can't confirm this, so consider it as an old rumor.
AND see Is Ksh93 dead?
There still seems to be some activity at the ast git-hub site . ast
is the over-arching package that includes ksh93
. You can get the fresh source code there and compile it.
Here is the text of the project description. (There is considerably more information in the README.md
).
KSH93
This repository contains the AT&T Software Technology (AST) toolkit
from AT&T Research. As of November 2017 the development focus has been
shifted to the ksh (or ksh93) command and supporting code required to
build it.
The non-ksh code of the AST project is no longer being actively
maintained. If you are interested in the non-ksh code see below for
details on which branches contain the full AST code base.
The project only supports systems where the compiler and underlying
hardware is ASCII compatible. This includes Linux on IBM zSeries but
not z/OS. The nascent, incomplete, support for EBCDIC has been
removed. See issue #742.
*
The EPL replaced AT&T's original CPL.
ksh93 supports the C-like (( ...;...; ...))
:
for ((i=1;i<20;i+=1)); do
printf "%d " $i
done && print
This will produce:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Heck, even the old syntax (using '{' ... '}' instead of 'do ... done' will work):
for((i=1;i<20;i+=1))
{
printf "%d " $i
} && print
In older shells, you can still get the same effect with
i=1 && while ((i<20)); do
printf "%d " $i
((i+=1))
done && print
ksh93 offers braceexpansion also if "braceexpand" is "on". Check with "set -o" and then use curly braces {}
for i in {1..20}
do
print $i
done