Sponsored Content
Top Forums Shell Programming and Scripting For loop respects quotes but not in a variable Post 302910349 by Michael Stora on Wednesday 23rd of July 2014 06:05:59 PM
Old 07-23-2014
I am trying to make a stand-along mailer along the lines of the minimal one I put together Monday but with everything configurable and a lot more features. The body of the message comes from STDIN but I also want to be able to pass arguments to manipulate headers at the top of STDIN since the arguments line could get very crowded very fast!

The darned thing works. In fact it works great. I am just tying up loose ends. (multiple attachments, implementing getopts, etc.) Right now commands on STDIN need to be one per line and I want to get around that although I am beginning to think the lack of need for quoting of extended e-mail addresses might be a feature: "Public, John Q" JQPublic@domain.com

Draft of Man:
Code:
tmail - lean send-only mailer for scripts and minimal embedded systems
tmail [OPTION] [COMMANDs]
    also accepts COMMANDs form STDIN if first line is "%%%%%".
    Use "%%%%%" to signify the end of OPTION section (remainder is mail body).
    Normally the end of the mail body is EOF but "%%%%%" can be used as well.
    This is useful when calling tmail from the terminal.
    Execution order is [OPTIONs] [COMMANDs] [COMMANDs from STDIN].
Any COMMAND without the ":" character "naked email" is assumed to be a email recipient.
    See -b below.
Options without arguments:
-?      Help            Show usage and exit
-b      Blind Mode      Assume naked e-mails are BCC: instead of To:
-l      Loud            show the telnet session.
-x      Debug Mode      echo what would have gone to telnet to stdout
                        (also sets delay=0)
 
Options with arguments:
-d  'date/time'         Date/Time override
                          Replaces calculated Date/Time with string.
                          (same as command date:'date/time')
-h  'host'              Host override
                          Replaces how the computer introduces itself to the SMTP server.
                          (same as command host:'host')
-m  'boundary'          MIME Boundary override
                          Replaces default MIME boundary
                          (same as command boundary:'boundary')
-v  server:port:delay   Server Override
                          Replaces the default SMTP Sever and Port number.
                          Delay is the delay after issuing commands to SMTP server.
                          See 'man sleep' for units.
                          Any field left blank does not override default.
                          ':18:' or '::2" changes only the Port or Delay, respectively.
                          (same as command server:)
 -a path:type:name      Attach File
                          Add an attachment to the email.  If not included name will be
                          the short name of the path and type will be guessed from the
                          file extension, if any.
                          (same as command attach: or a:)
 -f filepath            Use file for body of message instead of stdin.
 -h 'header'            Replaces "%%%%%" header with another string.
 -s 'subject'           Sets the message subject.
 
Commands:
from:email@domain           Set Sender (also f:)
to:email@domain             Add a "To:" recipient (also t:)
cc:email@domain             Add a "CC:" recipient (also c:)
bcc:email@domain            Add a recipient (also b:)
subject:'subject'           Sets the message subject (see -s above) (also s:)
attach:path:type:name       Add an attachment (see -a above) (also a:)
server:server:port:delay    Set Server, Port, and Delay (see -v above)
port:port                   Set Port (does not change server or dealy)
delay:delay                 Set Delay (does not change server or port)
host:host                   Set Host (see -h)
boundary:'boundary'         Set MIME boundary (see -m)
other                       Naked Email.  Any command without a ":" is assumed to be a "naked email".
                              Treated as a "To:" recipient unless blind mode (-b) is set.

Code:
#! /bin/bash
##################################################################################
#  tmail: Send an Email with Telnet                                              #
##################################################################################
#
##################################################################################
#  Declarations                                                                  #
##################################################################################
header="%%%%%"
boundary="XOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXO"
warning="This is a multipart message in MIME format."
mailDate="$(date +%a", "%d" "%b" "%Y" "%T" "%z)"
host="$HOSTNAME"
sender="$USER""@""$HOSTNAME"
##################################################################################
#Standard User Editable Defaults
#move this to a .conf file later
subject="no subject"
server="smtp.abcde.com"; port="25"; delay="1"
##################################################################################
declare -A recipients   # We are going to use associative array keys (and not values)
declare -A toList       # to keep track of mailing lists ${!array[@]} as a builtin
declare -A ccList       # way of preventing duplicates without sorting.
# add getopts options to handle "-x" type arguments.
# grab remaining arguments
#commands+=" ""$@"" "
# read in commands from stdin between "$header" lines, if any
read -r line
if [[ "$line" == "$header" ]]; then
    firstLine=""
    while read line && [[ "$line" != "$header" ]]; do commands+=$"$line"$'\n'; done
else
    firstLine="$line"
fi
# Process commands from whatever source
# echo "$commands" #debug
oIFS="$IFS"; IFS=$'\n'; for var in $commands; do # For does not seem to like temporary assignment
    if [[ "$var" == *:* ]]; then
        IFS=":" read -r comm val1 val2 val3 < <(echo "$var")
        case "${comm,,}" in
            "subject"|"s") subject="$val1" ;;
            "from"|"f") sender="$val1" ;;
            "to"|"t") recipients["$val1"]=1; toList["$val1"]=1 ;;
            "cc"|"c") recipients["$val1"]=1; ccList["$val1"]=1 ;;
            "bcc"|"b") recipients["$val1"]=1 ;;
            "attach"|"a") # add this
                ;;
            "server")
                [[ "$val1" ]] && server="$val1"
                [[ "$val2" ]] && port="$val2"
                [[ "$val3" ]] && delay="$val3"
                ;;
            "port") port="$val1" ;;
            "delay") delay="$val1" ;;
            "host") host="$val1" ;;
            "boundary") boundary="$val1" ;;
            "warning") warning="$val1" ;;
            "date") mailDate="$val1" ;;
            *) echo "bad command"; echo "var: ""$var"; echo "1: ""$val1"; echo "2: ""$val2"; echo "3: ""$val3";echo "4: ""$val4"; exit 1 ;;  #add usage and exist here.
        esac
    else recipients["$var"]=1; [[ ! "$blind" ]] && toList["$var"]=1
    fi
done; IFS="$oIFS"
# Telnet Session
{   echo "HELO ""$host"
    sleep "$delay"
    echo "MAIL FROM: ""$sender"
    sleep "$delay"
    oIFS="$IFS"; IFS=$'\n'; for i in "${!recipients[@]}"; do
        echo "RCPT TO: ""$i"
        sleep "$delay"
    done; IFS="$oIFS"
    echo "DATA"
    sleep "$delay"
    echo "From: ""$sender"
    toString="To: "
    oIFS="$IFS"; IFS=$'\n'; for i in "${!toList[@]}"; do
        toString+="$i"", "; done; IFS="$oIFS"
    echo "${toString%", "}"
    ccString="CC: "
    oIFS="$IFS"; IFS=$'\n'; for i in "${!ccList[@]}"; do ccString+="$i"", "; done; IFS="$oIFS"
    echo "${ccString%", "}"
    echo "Date: ""$mailDate"
    echo "Subject: ""$subject"
    echo "Content-Type: multipart/mixed; boundary=\"""$boundary""\""
    echo "MIME-Version: 1.0"
    echo    
    echo "$warning"
    echo
    echo "--"""$boundary""
    echo "Content-Type: text/plain"
    echo
    if [[ "$firstLine" ]]; then echo "$firstLine"; fi
        while read line && [[ "$line" != "$header" ]]; do echo "$line"; done
    echo
    #add a loop for multiple attachments
    if [[ "$attachment" ]]; then # add an attachment if not blank
        echo "--"""$boundary""
        echo "Content-Type: ""$attachmentType""; name=\""$attachmentName"\""
        echo "Content-Transfer-Encoding: base64";
        echo "Content-Disposition: attachment; filename=\""$attachmentName"\""
        echo
        base64 <"$attachment" | head
        echo
    fi
    echo "--"""$boundary"""--"
    echo "."
    sleep "$delay"
    echo "QUIT"; } | telnet "$server" "$port"
    exit 0

STDIN (or file when I implement that):
At this point I am taking advantage of the lack of a need to quote spaces by using the \n workaround.
Code:
%%%%%
subject:This is the mail subject
from:mike
to:bob
cc:frank
bcc:diane
%%%%%
Hello guys.
Here is an email
 
Mike

Mike
This User Gave Thanks to Michael Stora For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Double Quotes within a variable

This is a totally dumb newbie question, but I have not been able to find t he answer in the BASH book or online. I am trying pass a double quoted variable to the command line. variable = "-b \"dc=example,dc=com\"" When I run sh -x the variable comes out as '-b "dc=example,dc=com"' is... (4 Replies)
Discussion started by: burton_1080
4 Replies

2. Shell Programming and Scripting

Using echo to print double quotes along with variable substitution

Hi, I am generating html code using cshell, but i am having one problem while printing double quotes, I need to write following code in file. where $var contains list of web address <a href="$var">$var</a> So i am using echo "<a href="$var">$var</a>" > file.html But with this " in... (4 Replies)
Discussion started by: sarbjit
4 Replies

3. Shell Programming and Scripting

sed replace spaces between quotes with a variable

I have lines with: elseif (req.http.host ~ "^(www.)?edificationtube.com$|www.edificationtube.org www.edificationtube.net edificationtube.org www.edificationtube.com edificationtube.net") { elseif (req.http.host ~ "^(www.)?collegecontender.com$|www.collegecontender.com collegecontenders.com... (3 Replies)
Discussion started by: EXT3FSCK
3 Replies

4. UNIX for Dummies Questions & Answers

awk for inserting a variable containing single and double quotes

Hi i have to insert the below line into a specific line number of another file export MBR_CNT_PRCP_TYPE_CODES_DEL="'01','02','04','05','49','55','UNK'" I have passed the above line to a variable say ins_line. I have used below command to perform the insert awk 'NR==3{print "'"${ins_line}"'"}1'... (1 Reply)
Discussion started by: sathishteradata
1 Replies

5. Shell Programming and Scripting

Interpolate a variable with single quotes

I need to interpolate a shell variable in a code, i cannot share the exact code so this is an example i made up to describe the situation What I am trying to do here is try to wrap up the value of a variable in single quotes. This value needs to be passed to another program which would only... (4 Replies)
Discussion started by: sam05121988
4 Replies

6. Shell Programming and Scripting

Double quotes and variable proble in echo

HI Guys, I want to echo below line in my file :- lpd | grep AL | nawk '{print "1dLA - " $0} How can i echo same Output (4 Replies)
Discussion started by: pareshkp
4 Replies

7. UNIX for Dummies Questions & Answers

How to grep exact string with quotes and variable?

As the title says I'm running a korn script in attempts to find an exact match in named.conf finddomain.ksh #!/bin/ksh # echo "********** named.conf ************" file=/var/named/named.conf for domain in `cat $1` do grep -n '"\$domain "' $file done echo "********** thezah.inc... (1 Reply)
Discussion started by: djzah
1 Replies

8. Shell Programming and Scripting

How to pass two words within double quotes as variable?

Hi All, OS - Suse 10 ksh --version version sh (AT&T Research) 93s+ 2008-01-31 I am passing two words within double quotes ("Application Developer") to script as variable, but script is adding two single quotes between two words like ("Application' 'Developer"). below is simple test... (4 Replies)
Discussion started by: srimitta
4 Replies

9. Shell Programming and Scripting

Expansion of variable inside Single Quotes Fails!!

I am unable to expand the value of entry variable inside the nawk command. I tried three different nawk command as below but none of them substitute the value of entry variable. ls *.txt | while IFS='' read -r entry; do #nawk '/<name>/{A=1;++i} A{print >> ("cmd"i"_"$entry)}... (9 Replies)
Discussion started by: mohtashims
9 Replies

10. Shell Programming and Scripting

Spaces in double quotes in variable ?

Hi got this issue and was wondering if someone could please help out ? var='." "' echo $var ." " I 'll get ." " and not ." with 10 spaces in between " Thanks (3 Replies)
Discussion started by: stinkefisch
3 Replies
All times are GMT -4. The time now is 08:48 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy