Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

shell

csh

tcsh

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

like image 596
vehomzzz Avatar asked Dec 13 '22 02:12

vehomzzz


2 Answers

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.

like image 96
Dennis Williamson Avatar answered Apr 29 '23 10:04

Dennis Williamson


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.

like image 43
DigitalRoss Avatar answered Apr 29 '23 10:04

DigitalRoss