Find the answer to your Linux question:
Results 1 to 4 of 4
hey there - pretty new to bash scripting... Now i have an instance where i need ot write to a file a predetermined # of times... in this case I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2004
    Posts
    10

    increment looping statements


    hey there - pretty new to bash scripting...

    Now i have an instance where i need ot write to a file a predetermined # of times... in this case I am just creating headers

    Now I have variables defined:
    Code:
    DOM1=domain.com
    DOM2=domain2.com
    DOM3=domain3.com
    
    # DOMCNT is number of total domains.
    DOMCNT=3
    
    PFPATH=/path/to/file
    For each one i want to create a unique header (or run the below commands to create the heads)

    Code:
    let "CNT=0"     
    until [ "$CNT" = "$DOMCNT" ] ; do
            echo "##############" >$pfpath/$HDR.head
            echo "# DOMAIN $HDR" >>$pfpath/$HDR.head
            echo "##############" >>$pfpath/$HDR.head
    let "CNT++" 
    done
    Now 1) is this sytax correct? and 2)what would be hte best way to assign the value for $HDR? Each time it cycles through I want it to change the $HDR value to the next domain... essentially cycling through all the defined DOM# variables.

    I hope this makes sense....

  2. #2
    Just Joined!
    Join Date
    May 2004
    Posts
    10

    Some progress - clean up code?

    Ok - I have since found a way to get this to work as desired, but I was wondering if I posted the code here if anyone might know of a betterway to accomplish this?

    I wrote it as a function

    Code:
    function canonicalhead {
    let "CNT=0"
    let "CNT++"
    while [ "$CNT" -le "$DOMCNT" ] ; do
            printf "%q" "HDR=" >header.tmp
            printf "$" >>header.tmp
            printf "%q" "DOM$CNT" >>header.tmp
            source header.tmp
            rm -f header.tmp
            echo "##############" >$PFPATH/$HDR.head
            echo "# DOMAIN $HDR" >>$PFPATH/$HDR.head
            echo "##############" >>$PFPATH/$HDR.head
            echo
            let "CNT++"
    done
    }
    The main way I suspect it can be cleaned up it a better way to set the $HDR variable. Right now I export it and source it back in.

  3. #3
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078
    why not increment it at every iteration of the loop? if you use a sequential domain naming scheme, that will work... or you could set up all of the domain names as $HDR1, $HDR2, $HDR3, etc and then set something in the loop to increment at the iterations to point to the correct HDR var
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    May 2004
    Posts
    10
    I don't quite follow (as i have been scripting for all of a day)

    I already have all my domains set with the variables $DOM1, $DOM2, etc.
    I can't use an incremental domain naming scheme as they are real domain names (ie domain.com and anotherdomain.com)

    And my original issue was exactly how to change the $HDR variable each time it ran through the loop. As in change $HDR to = $DOM1, then on next loop have $HDR = $DOM2, etc etc.

Posting Permissions

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