Results 1 to 7 of 7

Thread: Looping Concept

  1. #1
    Join Date
    Dec 2003
    Location
    Ogden Utah
    Posts
    34

    Talking Unanswered: Looping Concept

    Hello:

    I am a greenie @ unix scripting and looking for some input as to how to solve the problem I have below.


    Below is representation of what is found in file em.data

    VenNu Sts controlnu typ email
    111111 LATE 321321321 997 xxxxx@xxxx.com
    222222 LATE 321321321 997 xxxxx@xxxx.com

    The objective is to write a script that can read each line in the em.data file and send a corresponding email notification for each respective email account. Here vendor number 111111 will have different email account than vendor number 222222 and their line information will also differ.

    What I have so far is the script below but it does differentiate between email addresses and sends all data to each respective email address. What would be some of your suggestions.


    ############################
    file=em.data

    COLA=`cut -f1 $file`
    COLB=`cut -f2 $file`
    COLC=`cut -f3 $file`
    COLD=`cut -f4 $file`
    Email=`cut -f3 $file`


    for i in $email ; do
    echo $COLA $COLAB $COLAC $COLAD | mailx -s "Notifications" -r edi@flyingj.com $i
    done
    mvilla

  2. #2
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Looking at your script, presumably what you meant was `cut -f1 -d" "` to get your lists of values. This would still however be the wrong approach because you would end up with 5 variables, each containing ALL of the respective column data in your file.

    You then say, for every element in $email (i.e. for each email address), mail them the full set of data held in $COLA, $COLB, $COLC and $COLD.

    Do you see why this is wrong?

    You need to take each line at a time. One way to do this is to use read:

    Code:
    while read COLA COLB COLC COLD EMAIL
    do
      mailx -s "Notifications" -r "$EMAIL edi@flyingj.com" <<- !!
            $COLA $COLAB $COLAC $COLAD
    !!
    done < em.data
    Note that the spaces in front of $COLA is actually a TAB (The use of <<- allows this formatting and will remove the TAB before passing the detail into mailx).

    HTH
    Last edited by Damian Ibbotson; 01-30-04 at 12:19.

  3. #3
    Join Date
    Dec 2003
    Location
    Ogden Utah
    Posts
    34

    Talking Looping Concept -THANK YOU!!!!!

    Originally posted by Damian Ibbotson

    WOW WHAT A GREAT SOLUTION!!!! Yes I did try and it did work.

    Looking at your script, presumably what you meant was `cut -f1 -d" "` to get your lists of values.

    >>>>Yes, Correct.

    This would still however be the wrong approach because you would end up with 5 variables, each containing ALL of the respective column data in your file.

    >>>>Yes, you are absolutely right!

    You then say, for every element in $email (i.e. for each email address), mail them the full set of data held in $COLA, $COLB, $COLC and $COLD.

    Do you see why this is wrong?

    >>>>I do now!!

    You need to take each line at a time. One way to do this is to use read:

    >>>>So UNIX know to read each line continuesly?

    Code:
    while read COLA COLB COLC COLD EMAIL
    do
      mailx -s "Notifications" -r "$EMAIL edi@flyingj.com" <<- !!
            $COLA $COLAB $COLAC $COLAD
    !!
    done < em.data
    Note that the spaces in front of $COLA is actually a TAB (The use of <<- allows this formatting and will remove the TAB before passing the detail into mailx).

    >>>>Could you explain why the data file em.data is placed after the while loop.

    Looking forward to your reply.

    Regards,
    Milenko

    HTH
    mvilla

  4. #4
    Join Date
    Dec 2003
    Location
    Ogden Utah
    Posts
    34

    Re: Looping Concept -THANK YOU!!!!!

    Originally posted by mvillan
    I got a little carried away here and forgot to correct my spelling and sentence construction.
    >>>>So UNIX knows how to read each line continuously when using read?
    mvilla

  5. #5
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    The file em.data is placed after the loop because the loop is taking its input from that file.
    Code:
    while read lineDataFromFile
    do
        ...
    done < fileName
    What this is saying is 'while' the exit status of the read command is 0 (i.e. the read command returns 'true') after attempting to read the next line from the file em.data, attempt to read another line. When there are no more lines to read, the final attempt to read a line will fail and return a non-zero exit status (which equates to a boolean false) and the loop will cease. It's a simple case of "while condition=true; do stuff".

  6. #6
    Join Date
    Feb 2004
    Location
    UK
    Posts
    43

    LOOPING CONCEPT

    Hi
    I was just going through solution which u hv suggested..
    I hv tried to run this but it gives some different result.

    it mails this line to abc.pqr@xxx.com

    222222 LATE 321321321 997 222222@xxxx.com
    this is the 2 line in em.data

    while read COLA COLB COLC COLD EMAIL
    do

    print "email is $EMAIL...."
    print "abcd is $COLA $COLAB $COLAC $COLAD..."

    mailx -s Notifications -r $EMAIL abc.pqr@xxx.com !! $COLA $COLAB $COLAC $COLAD !!

    done < em.data

    if u c the mail box in
    From : 111111@xxxx.com
    To : 111111@gtecompute2.eur.nsroot.net; abc.pqr@xxx.com

    and the content of mail ie body is

    222222 LATE 321321321 997 222222@xxxx.com

    (1)how it is happening as per my understanding mail body sould contain first line of em.data..
    (2)and abc.pqr@xxx.com should receive two mail or what??

  7. #7
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525

    Re: LOOPING CONCEPT

    mailx -s Notifications -r $EMAIL abc.pqr@xxx.com !! $COLA $COLAB $COLAC $COLAD !!
    The above line is incorrect. You have not read the post correctly. This looks to me like you are attempting to send the email to: $EMAIL, abc.pqr@xxx.com, !!, $COLA, $COLAB, etc.

    As a result, there is no std input into mailx, so it will take the input from the em.data file.

    The solution I gave used a HERE-DOCUMENT, with the syntax
    Code:
    command <<here-doc-string
    input data for command
    here-doc-string
    Of course, you could simply echo your values and pipe them into mail

    Code:
    echo $COLA $COLB $COLC $COLD | mailx -s theSubject theMailRecipients
    ... but my preference is not to use a command (i.e. echo) if you don't really need it.

    Damian
    Last edited by Damian Ibbotson; 02-11-04 at 14:18.

Posting Permissions

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