如何在 csh shell 的命令提示符中使用 for 循环——寻找合适的衬里

发布于 2024-08-06 18:15:14 字数 137 浏览 6 评论 0原文

来自 bash shell,我错过了一个简单的循环滚动(for i in (...); do ... done;)

您会在 cshell 中发布典型的单行循环吗?

请使用单行,而不是多行 谢谢

coming from bash shell, I missed on an easy rolling of loops (for i in (...); do ... done;)

Would you post typical one-liners of loops in cshell?

ONE LINERS PLEASE, and not multiple-lines
thx

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

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

发布评论

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

评论(4

三寸金莲 2024-08-13 18:15:14

csh 手册页指出:

foreach、switch 和 while 语句以及 if 语句的 if-then-else 形式要求主要关键字出现在输入行的单个简单命令中,如下所示。

foreach 和 end 必须单独出现在不同的行上。

单词 else 和 endif 必须出现在输入行的开头; if 必须单独出现在其输入行上或出现在 else 之后。

while 和 end 必须单独出现在其输入行上。

The csh man page states:

The foreach, switch, and while statements, as well as the if-then-else form of the if statement require that the major keywords appear in a single simple command on an input line as shown below.

and

Both foreach and end must appear alone on separate lines.

and

The words else and endif must appear at the beginning of input lines; the if must appear alone on its input line or after an else.

and

The while and end must appear alone on their input lines.

唯憾梦倾城 2024-08-13 18:15:14

哇,我已经很多年没有写过 csh 脚本了。但 Bill Joy 确实自己写了它,我想它值得一些怀旧的努力...

set t=(*)
foreach i ($t)
  echo $i >> /tmp/z
end

或者只是 foreach i (*)

这个循环结构与 csh 具有的内置概念配合得很好 单词列表。这在 bash 中有点存在,但在普通的 posix shell 中不存在。

set q=(how now brown cow)
echo $q[2]

foreach 循环巧妙地迭代此数据结构。

Wow, I haven't written a csh script in years. But Bill Joy did write it himself, I suppose it's worth some nostalgia effort...

set t=(*)
foreach i ($t)
  echo $i >> /tmp/z
end

or just foreach i (*)

This loop structure works well with a built-in concept that csh has of a word list. This is sort-of present in bash but not in vanilla posix shells.

set q=(how now brown cow)
echo $q[2]

The foreach loop neatly iterates through this data structure.

伤痕我心 2024-08-13 18:15:14

我努力让 CSH shell 轻松循环并运行相同的命令。

正如这个答案所指出的: https://stackoverflow.com/a/1548355/1897481

让我放弃了一个制作一个单行作品。

最后决定使用以下别名来在循环中运行带有参数*的命令:

    # while_cmd_w_sleep <SLEEP-TIME> <CMD + ARGS>
    alias while_cmd_w_sleep '(echo '\''while (1)\n\!:2*\necho =================\necho Sleeping for \!:1.\nsleep \!:1\necho =================\nend'\'') | tcsh'

    # for_n_cmd <LOOP_COUNT> <CMD + ARGS>
    alias for_n_cmd         '(echo '\''foreach x (`seq \!:1`)\necho =================\necho Iteration \[$x]\necho =================\n\!:2*\nend'\'') | tcsh'

    # for_n_cmd_w_sleep <LOOP_COUNT> <SLEEP-TIME> <CMD + ARGS>
    alias for_n_cmd_w_sleep '(echo '\''foreach x (`seq \!:1`)\necho =================\necho Iteration \[$x]\necho =================\n\!:3*\necho =================\necho Sleeping for \!:2.\nsleep \!:2\nend'\'') | tcsh'

示例输出:

    
gt; for_n_cmd 3 echo hi
    =================
    Iteration [1]
    =================
    hi
    =================
    Iteration [2]
    =================
    hi
    =================
    Iteration [3]
    =================
    hi

while 循环:

    
gt; while_cmd_w_sleep 2s echo hello there
    hello there
    =================
    Sleeping for 2s.
    =================
    hello there
    =================
    Sleeping for 2s.
    =================
    hello there
    =================
    Sleeping for 2s.
    ^C

* 在循环中运行多个命令的示例:

    
gt; for_n_cmd 3 'echo hi\\nsleep 2s\\necho done waiting'
    =================
    Iteration [1]
    =================
    hi
    done waiting
    =================
    Iteration [2]
    =================
    hi
    done waiting
    =================
    Iteration [3]
    =================
    hi
    done waiting

在此示例中,三个命令 echo hisleep 2secho done waiting 正在循环中运行。

I struggled to get CSH shell to easily loop over and run the same command over.

As pointed out by this answer: https://stackoverflow.com/a/1548355/1897481

Made me give up one making a oneliner work.

Finally settled to having the following aliases to run commands with arguments* in a loop:

    # while_cmd_w_sleep <SLEEP-TIME> <CMD + ARGS>
    alias while_cmd_w_sleep '(echo '\''while (1)\n\!:2*\necho =================\necho Sleeping for \!:1.\nsleep \!:1\necho =================\nend'\'') | tcsh'

    # for_n_cmd <LOOP_COUNT> <CMD + ARGS>
    alias for_n_cmd         '(echo '\''foreach x (`seq \!:1`)\necho =================\necho Iteration \[$x]\necho =================\n\!:2*\nend'\'') | tcsh'

    # for_n_cmd_w_sleep <LOOP_COUNT> <SLEEP-TIME> <CMD + ARGS>
    alias for_n_cmd_w_sleep '(echo '\''foreach x (`seq \!:1`)\necho =================\necho Iteration \[$x]\necho =================\n\!:3*\necho =================\necho Sleeping for \!:2.\nsleep \!:2\nend'\'') | tcsh'

Sample outputs:

    
gt; for_n_cmd 3 echo hi
    =================
    Iteration [1]
    =================
    hi
    =================
    Iteration [2]
    =================
    hi
    =================
    Iteration [3]
    =================
    hi

The while loop:

    
gt; while_cmd_w_sleep 2s echo hello there
    hello there
    =================
    Sleeping for 2s.
    =================
    hello there
    =================
    Sleeping for 2s.
    =================
    hello there
    =================
    Sleeping for 2s.
    ^C

* Example where multiple commands are run in a loop:

    
gt; for_n_cmd 3 'echo hi\\nsleep 2s\\necho done waiting'
    =================
    Iteration [1]
    =================
    hi
    done waiting
    =================
    Iteration [2]
    =================
    hi
    done waiting
    =================
    Iteration [3]
    =================
    hi
    done waiting

In this example, three commands echo hi, sleep 2s and echo done waiting are being run in the loop.

我们只是彼此的过ke 2024-08-13 18:15:14

我想说,尽管有 csh 选项,我已经以某种方式修复了它,所以你可以这样做:

printf "while ( 1 ) \n ps -aux|grep httpd \n echo 'just a new row' \n  sleep 2 \n end" | csh -f

I would say that i've fixed it somehow, despite csh options, so you can do something like this:

printf "while ( 1 ) \n ps -aux|grep httpd \n echo 'just a new row' \n  sleep 2 \n end" | csh -f
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文