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

StackOverflow https://stackoverflow.com/questions/1544325

  •  20-09-2019
  •  | 
  •  

Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top