For loop respects quotes but not in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For loop respects quotes but not in a variable
# 8  
Old 07-23-2014
xargs with a here doc seems to work quite nicley:

Code:
# test="a 'b c'"
# xargs -n 1 << EOF | while read var; do echo $var ; done
$test
EOF
a
b c

# 9  
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  
Old 07-23-2014
I'd suggest using boundary=$(uuidgen -t) rather than that XOXO.. string as this will avoid issues when a email from this program is attached in another.
# 11  
Old 07-23-2014
Quote:
Originally Posted by Chubler_XL
I'd suggest using boundary=$(uuidgen -t) rather than that XOXO.. string as this will avoid issues when a email from this program is attached in another.
good idea. Did not think of email inside an email.
Not in the embeded environment I am designing for but I will grab a certain number of bytes from urandom and pass them into base64.

update: base64 ended up being very unpredictable length even with huge amount of binary date fed to it could be strangely short. I used 512 bytes into sha256sum for a reasonable length header.

Mike

Last edited by Michael Stora; 07-23-2014 at 08:00 PM..
# 12  
Old 07-23-2014
Quote:
For does not seem to like temporary assignment
It's not whether it "likes" it or not. It's because of the order shell processes a statement.

The temporary IFS assignment only becomes valid when the statement is executed -- but in order to execute the statement, it already did splitting on $VAR. read on the other hand doesn't need a pre-splitted variable.
This User Gave Thanks to Corona688 For This Post:
# 13  
Old 07-23-2014
Quote:
Originally Posted by Michael Stora
I have a file with argments in it. There can be multiple arguments per line. Arguments can contain whitespace (in which case they are quoted). I want to itterate through them and handle them (such as with a case statement). I want the itteration to separate on unqoted white space but not quoted whitespace.
What you describe is a (very simple) parser. The following sketch roughly does what you want, you will want to put it into a function in you script. I wrote it for ksh initially, so you will probably have to adapt it somewhat ("print" -> "echo", etc.).

Code:
function f_Parse
{
typeset fIn="$1"                                 # input file
typeset lInStr=0                                 # flag: inside (quoted) string
typeset chProfStr=""
typeset chAct=""                                 # current char
typeset chRest=""
typeset chBuffer=""

chProfStr="$(cat "$fIn")"
while [ -n "$chProfStr" ] ; do
     chRest="${chProfStr#?}"                     # split off first char
     chAct="${chProfStr%${chRest}}"
     chProfStr="$chRest"

     #print - "chAct: $chAct"
     #print - "read : $chBuffer"

     case "$chAct" in
          \")
               if [ $lInStr -eq 0 ] ; then
                    (( lInStr = 1 ))
               else
                    (( lInStr = 0 ))
               fi
               chBuffer="${chBuffer}${chAct}"
               ;;

          " ")
               if [ $lInStr -eq 0 ] ; then
                    print - "$chBuffer"          # write / flush buffer
                    chBuffer=""
               else
                    chBuffer="${chBuffer}${chAct}"
               fi
               ;;

          *)
               chBuffer="${chBuffer}${chAct}"
               ;;

     esac
done

return 0
}

I hope this helps.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
# 14  
Old 07-23-2014
Love the f_Parse function Bakunin. It got me thinking about a quoted_read function that works like read but supports quoted strings so I could call it like this

Code:
test='one two three four five
"this and" that
done'

while quoted_read p1 p2 rest
do
   printf "%s\n" "p1=$p1" "p2=$p2" "rest=$rest" ""
done <<EOF
$test
EOF

and get the output:

Code:
p1=one
p2=two
rest=three four five

p1=this and
p2=that
rest=

p1=done
p2=
rest=


And here is the function I came up with (based on f_Parse):

Code:
function quoted_read
{
  typeset lInStr=0
  typeset chBuffer
  typeset chAct
  typeset RET=1

  [ $# -eq 0 ] && set -- REPLY

  OIFS=$IFS
  IFS=
  while read -N 1 chAct
  do
     RET=0
     case "$chAct" in
        \") (( lInStr = (lInStr + 1) % 2 ))
            continue
            ;;
        " "|$'\n') if [ $lInStr -eq 0 ]
             then
                 [ "$chAct" == $'\n' ] && break
                 if [ $# -gt 1 ]
                 then
                    # More vars to assign -> not appending to current
                    export $1="$chBuffer"
                    shift
                    chBuffer=""
                    continue
                 fi
             fi
             ;;
     esac
     chBuffer="${chBuffer}${chAct}"
  done

  if [ ${#chBuffer} -gt 0 ]
  then
      export $1="$chBuffer"
      shift
  fi

  # Blank any vars not assigned
  while [ $# -gt 0 ]
  do
      export $1=""
      shift
  done
  IFS=$OIFS
  return $RET
}

This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question