How to use for loops in command prompt in csh shell — looking for decent one liners

拜拜、爱过 提交于 2020-01-03 18:54:36

问题


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


回答1:


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.




回答2:


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.




回答3:


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



回答4:


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:

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

The while loop:

    $> 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:

    $> 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.



来源:https://stackoverflow.com/questions/1544325/how-to-use-for-loops-in-command-prompt-in-csh-shell-looking-for-decent-one-li

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!