Unix: csh Shell Loop Example

Unix: csh Shell Loop Example

Here is an example of a loop in the csh shell:

refe‮l:ot r‬autturi.com
# Loop through the numbers 1 to 10
foreach i (1 2 3 4 5 6 7 8 9 10)
    # Print the current number
    echo $i
end

This loop will iterate through the numbers 1 to 10 and print each number to the terminal.

The foreach keyword is used to start the loop, and the end keyword is used to end the loop. The i variable is used to hold the current loop value, and the list of values (1 2 3 4 5 6 7 8 9 10) specifies the values that the loop will iterate through.

Inside the loop, you can use the $i variable to refer to the current loop value.

Here is another example of a loop that counts from 1 to 10 and prints the odd numbers:

# Loop through the numbers 1 to 10
foreach i (1 2 3 4 5 6 7 8 9 10)
    # Check if the current number is odd
    if ($i % 2 == 1) then
        # Print the current number
        echo $i
    endif
end

This loop uses the if statement to check if the current loop value is odd, and only prints the value if it is. The % operator is used to compute the remainder of a division, and the == operator is used to compare two values.

For more information about loops and other features of the csh shell, you can refer to the csh man page by running man csh in the terminal.

Created Time:2017-10-30 14:27:30  Author:lautturi