4

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

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

请单行,而不是多行 thx

4

4 回答 4

10

csh手册页指出:

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

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

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

while 和 end 必须单独出现在它们的输入行上。

于 2009-10-10T16:14:41.950 回答
2

哇,我好几年没写csh剧本了。但比尔·乔伊确实是自己写的,我想这值得一些怀旧的努力......

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

要不就foreach i (*)

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

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

foreach循环巧妙地遍历这个数据结构。

于 2009-10-09T15:00:40.000 回答
1

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

printf "while ( 1 ) \n ps -aux|grep httpd \n echo 'just a new row' \n  sleep 2 \n end" | csh -f
于 2015-03-21T18:24:28.697 回答
0

我努力让 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'

示例输出:

    $> for_n_cmd 3 echo hi
    =================
    Iteration [1]
    =================
    hi
    =================
    Iteration [2]
    =================
    hi
    =================
    Iteration [3]
    =================
    hi

while 循环:

    $> while_cmd_w_sleep 2s echo hello there
    hello there
    =================
    Sleeping for 2s.
    =================
    hello there
    =================
    Sleeping for 2s.
    =================
    hello there
    =================
    Sleeping for 2s.
    ^C

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

    $> 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 hi,sleep 2secho done waiting正在循环中运行。

于 2017-09-04T05:56:49.330 回答