user2824383
user2824383

Reputation: 13

In bash, how do we, in this case limit mail to just From:, To:, Date:, Subject:, and the message body.

In this program, I cat the contents of my mail into the program, and it should only display the fields mentioned (From: To: Date: Subject:) and the message body for each email in the inbox. I am still new to BASH so please explain any answers if you have them.

 #! /bin/bash
    IFS=" "
    read first rest
    while [ $? -eq 0 -a "$first" == "From" ] ; do
         echo $first" "$rest
         :
         read first rest
         ###################################
         #
         while [ ! -z $first ] ; do
              #
              case "$first" in
                  "Date:" )
                  echo $first$rest
                  :
                  ;;
                  "From:" )
                  echo $first$rest
                  :
                  ;;
                  "To:" )
                  echo $first$rest
                  :
                  ;;
                  "Subject:" )
                  echo $first$rest
                  :
                  ;;
                  * )
 :
              ;;
          esac
          read first rest
     done
     ###################################
     #
     ##
     ##
     :
     read first rest
     ###################################
     #
     while [ ! -z $first ] ; do
          :
          echo $first" "$rest
          read first rest
     done
     read first rest
done
:
exit

Upvotes: 0

Views: 118

Answers (1)

glenn jackman
glenn jackman

Reputation: 247122

in_headers=true
while IFS= read -r line; do
    if $in_headers; then
        if [[ $line == "" ]]; then
            in_headers=false
        else
            case $line in 
                From:* | To:* | Date:* | Subject:*) : ;;
                *) continue ;;
            esac
        fi
    fi
    echo "$line"
done

Upvotes: 0

Related Questions