If you are stuck with a c-shell and you want to print (or execute a command) for an increment, say of 5, in a variable (for example, $x), then do either of the following:
#!/bin/csh # Exammple: 1 foreach x (`seq 1 5 20`) echo $x end #EOF
#!/bin/csh # Example: 2 @ x = 1 while ($x <= 20) echo $x @ x += 5 end #EOF
They both print the numbers 1 to 20 in steps of 5. If you want to use it in the command line, then you need to type each line (of course, except #!/bin/csh
and #EOF
) and hit enter .
If you know all the possible elements (numbers, characters or strings) of the array passed on to x
for each foreach
, then you may choose to use the following
#!/bin/csh # Example: 3 foreach x (1 2 9 40) echo $x end #EOF
when the elements are 1, 2, 9 and 40; or
#!/bin/csh # Example: 4 foreach x (*.cpp) echo $x end #EOF
when the elements are all the filenames ending with .cpp in the current directory.
Update: The second option (Example: 2) spits out some syntax error message when used in the command line (in the same way as mentioned above). However, it works fine in a script file.
Pingback: [svn] List subversion commits by a user for a given date range « *NIX Tricks
aetna doc find said:
Incredibly well written article!!!
Jack said:
Quite good indeed. Clueless was I until finding this article.
Anonymous said:
give more examples
Anonymous said:
I got a nice article here. and try to take some tricky examples.
Anonymous said:
Great! It is helpful to me. Thanks.
Anonymous said:
You may probably need a space after opening parenthesis and before closing parenthesis in foreach’s wordlist.