关于“为了” KornShell 中的循环

发布于 2024-10-28 06:57:24 字数 290 浏览 6 评论 0原文

有没有办法在 KornShell (ksh) 中使用“for”来实现以下内容? 这是 C 的等价物:

for(i=1;i<20;i++)
{
    printf("%d",i);
}

我想知道是否可以仅使用“for”而不是“while”来实现,

我尝试了以下方法,但它似乎不起作用。

for i in [1-20]
    do

    print $i
    done

请让我知道您的想法和解决方案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不羁少年 2024-11-04 06:57:24

这并不是一个真正的答案,而是给临时 ksh 用户的一个参考。

编辑于 2019 年 5 月 12 日(下面粗体进行了少量修改,其他信息现已受攻击)。

为了澄清这里的一些评论,典型供应商安装中有 2 个 ksh 可用(非 Linux(也许也是?))。

Solaris 和 AIX 有 ksh 和 ksh93(对于其他供应商来说可能也是如此)。基本 ksh 也称为 ksh88。 ,1995 中描述了 Ksh93

新的 Kornshell 命令和编程语言 真正的ksh(不是pdksh),主要使用名为ksh的ksh93。

最后,为了进一步混淆事情,不要让 1995 年的发布日期欺骗了您,ksh 继续 正在由 AT&T 的 David Korn 和 Glen Fowler 积极开发,直到2012年? 版本每年发布 2-3 次。某些 Linux 版本会选择较新的版本。

这些较新的版本具有非常先进的功能
(大部分内容取自 AT&T 研究 UWIN 页面。搜索链接 '注释和更改'(死链接))

  • 像c结构一样组成的复合变量(没有c数据类型,只是排版decls)(一个用户声称有500兆内存结构)
  • 双精度浮点算术完整的 C99 算术..数字 Inf 和 NaN 可以在算术表达式中使用。
  • TAB-TAB 补全生成补全的编号列表...
  • 支持处理/处理多字节语言环境(例如,en_US.UTF-8、hi_IN.UTF-8、ja_JP.eucJP、zh_CN.GB18030、zh_TW.BIG5 等)。 ..
  • /dev/(tcp|udp|sctp)/host/sevrice 现在处理 IPv6 地址 ...
  • ... 使用新的重定向运算符按偏移量或内容查找文件。
  • 新的 --showme 选项允许脚本的某些部分表现得就像指定了 -x 一样,而其他部分照常执行。 ...
  • 添加了 [[...]] 运算符 =~,它将字符串与扩展正则表达式进行比较 ....
  • printf(1) 内置函数已扩展为支持用于使字段居中的 = 标志 .. .(和其他)...
  • 视图路径
  • “大多数实用程序都是由 AT&T 开发的,并且符合 POSIX.2 和 X/Open。​​”

(请注意,上面的...,通常表示删除了一些限定信息)

Korn 和 Fowler 还为使用 Mingw 或 Cygwin 等系统的用户制作了一个高级环境 UWIN(Unix for Windows),这值得单独写一篇文章。 UWIN 的缺点是,它

  • 的实用程序集与您在喜爱的 Linux 中找到的实用程序集不同。
  • 另一个文件编译环境,几乎必须使用 MS Visual C(据说正在通过 Mingw 提供 gcc 支持),
  • 一个非常小的支持社区,
  • AT&T Common Public License V 1.0 Eclipse 公共许可证* 不是 GNU。

请参阅UWin主页(死链接) :不幸的是已经过时了,最好在上面的 dnld 链接中查找。嗯,这好多了 Glenn Fowler 的 UWin 常见问题解答< /a> (也死了,时间机器有人吗?)。

我希望这有帮助!

编辑于2019年5月12日。死链接的原因是什么?
大卫Korn 和 Glen Fowler 被解雇(AT&T,2012 年?

后来有消息称他们在 Google 工作。我无法证实这一点,所以将其视为旧谣言。

并参见 Ksh93 死了吗?

ast git-hub 站点ast 是包含 ksh93 的总体包。您可以在那里获取新的源代码并编译它。


这是项目描述的文本(README.md 中有更多信息)。

KSH93

此存储库包含 AT&T 软件技术 (AST) 工具包
来自 AT&T 研究部。截至 2017 年 11 月,开发重点为
转移到 ksh(或 ksh93)命令和所需的支持代码
构建它。

AST项目的非ksh代码不再活跃
维持。如果您对非 ksh 代码感兴趣,请参阅下面的内容
有关哪些分支包含完整 AST 代码库的详细信息。

该项目仅支持编译器和底层的系统
硬件兼容 ASCII。这包括 IBM zSeries 上的 Linux,但是
不是 z/OS。对 EBCDIC 的新生的、不完整的支持
已删除。请参阅问题#742。

*
EPL 取代了 AT&T 原来的 CPL。

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.

狼性发作 2024-11-04 06:57:24

ksh93 支持类似 C 的 (( ...;...; ...))

for ((i=1;i<20;i+=1)); do
    printf "%d " $i
done && print

这将产生:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

哎呀,即使是旧的语法(使用 '{' ... '}' 而不是 'do ... done' 也可以工作):

for((i=1;i<20;i+=1))
{
   printf "%d " $i
} && print

在旧的 shell 中,您仍然可以获得相同的效果

i=1 && while ((i<20)); do
    printf "%d " $i
    ((i+=1))
done && print

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
离不开的别离 2024-11-04 06:57:24

不幸的是,看起来 ksh 不支持基于范围的大括号扩展或支持 (( )) 构造,因此要紧凑地执行此操作,您需要像这样调用外部二进制seq

for i in $(seq 1 20); do
  echo $i
done

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
月亮坠入山谷 2024-11-04 06:57:24

如果“braceexpand”为“on”,ksh93 也提供大括号扩展。检查“set -o”,然后使用花括号 {}

for i in {1..20}
do
  print $i
done

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文