Find the answer to your Linux question:
Results 1 to 6 of 6
hi i am trying to run a for loop in csh, but i don't think that it is possible, the for loop is to loop round say for (i=0; i<=5; ...
  1. #1
    Just Joined!
    Join Date
    Jun 2007
    Posts
    4

    Unhappy for/while loop...awk

    hi i am trying to run a for loop in csh, but i don't think that it is possible, the for loop is to loop round say

    for (i=0; i<=5; i++){
    in here i want to run a file or a pipe to copy a line repeatedly, then save result in a file
    }

    i am relatively new here and am not sure the correct syntax
    i have been trying for hours
    do i need to use awk or am i ok in csh

    help!

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Welcome to the forum.

    Try this:

    Code:
    for i = 0 to 4 do
      # do your stuff here
    end
    Awk is not a shell but a text manipulation tool. I should begin with Bash instead of csh.

    Regards

  3. #3
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    Can I ask why csh? I thought bash was the default shell for just about every Linux distro under the sun. You may find it a lot easier to script with, too.

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    I agree with most other people: use bash, ksh, etc., any of the Bourne shell family is preferable to csh family for scripting.

    However, if you must, you may be able to use one of these:
    Code:
    #!/bin/tcsh
    
    # @(#) s3       Demonstrate loops in [t]csh.
    
    echo
    echo $version | fold -66
    echo
    
    echo " foreach loop:"
    foreach i ( `/usr/bin/seq 0 3` )
      echo " i is $i"
    end
    
    echo
    echo " while loop:"
    set j = 0
    while ( $j < 4 )
      echo " j is $j"
      @ j++
    end
    
    exit 0
    producing:
    Code:
    % ./s3
    
    tcsh 6.13.00 (Astron) 2004-05-19 (i386-intel-linux) options 8b,nls
    ,bye,al,ng,rh,nd,color,filec
    
     foreach loop:
     i is 0
     i is 1
     i is 2
     i is 3
    
     while loop:
     j is 0
     j is 1
     j is 2
     j is 3
    For a host of reasons not to use csh, see:
    Csh Programming Considered Harmful

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  5. #5
    Just Joined!
    Join Date
    Jun 2007
    Posts
    4

    Talking

    cheers guys helped, got it working now, can move along nicely

  6. #6
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by OOdle nOOdle View Post
    hi i am trying to run a for loop in csh, but i don't think that it is possible, the for loop is to loop round say

    for (i=0; i<=5; i++){
    in here i want to run a file or a pipe to copy a line repeatedly, then save result in a file
    }

    i am relatively new here and am not sure the correct syntax
    i have been trying for hours
    do i need to use awk or am i ok in csh

    help!
    depending on what you are doing, you can also use awk for loop
    Code:
    awk 'BEGIN {
            for (i=1;i<5;i++) {
                    #do something
            }
    } '

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...