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
# 15  
Old 07-24-2014
Guys I love the parser! It is not 100% clear to me how it handles multiple spaces in a row. As far as multiple carraige returns, the behavior of read is to include blank lines as output and the behavior or for with /n as the delimiter is to skip them. Both however, treat multiple unquoted spaces or tabs as if the were one.

I assume we can add another inString variable for single quotes (') and add $'\t' (tab) to the second case condition in Chubler_XL's version.

Hmm . . . I had kind of assumed that ' was "superior" to ". Actually whichever one comes first is "superior"
Code:
$ echo "'" "''" "'''"
' '' '''

$ echo '"' '""' '"""'
" "" """

$ echo '"'"'" "'"'"'
"' '"

So inStringSingle if active would have not not count " and inStringDouble if active would not count '.

Meanwhile, I have the mailer to the point that I can use it for the purpose I wrote it. I will return to it in the future to make it much nicer. I've decided that my \n workaround when I loop through the command list is actually a feature since so many e-mail strings and other things SMTP have spaces and will pass them one per line with spaces on STDIN. When I add options and $@, I will just add /n characters to the command string between $1, $2, etc. as BASH already parses quotes in arguments neatly.

Anyway, here is the script in working (but not complete form).
Code:
#! /bin/bash
##################################################################################
#  tmail: Send an Email with Telnet v0.5                                         #
##################################################################################
#  Michael E. Stora, July 2014
##################################################################################
#  Declarations                                                                  #
##################################################################################
header="%%%%%"
boundary="$(read -r -n 512 line </dev/urandom; echo $line | sha256sum)"
warning="This is a multipart message in MIME format."
host="$HOSTNAME"
sender="$USER""@""$HOSTNAME"
mailDate="$(date +%a", "%d" "%b" "%Y" "%T" "%z)"
##################################################################################
# Standard User Editable Defaults
# move this to a .conf file later               <<<<<<<<<<<< To Do
subject="no subject"
server="smtp.xxxxx.com"; port="25"; delay="1"
##################################################################################
#
# Add usage function and help <<<<<<<<<<<< To Do
#
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.   <<<<<<<<<<<< To Do
#
# grab remaining arguments (parse remaining $@ into newline delimited commands list) <<<<<<<<<<<< To Do
#
# read in commands from stdin between "$header" lines, if any
read -r line
if [[ "$line" == "$header" ]]; then
    while read line && [[ "$line" != "$header" ]]; do commands+="$line"$'\n'; done
else
    firstLine="$line" # Oops! Not a header afterall.  Manaully add it to top of body later.
fi
#
# Process commands from whatever source (options, arguments, body header)
# usage: command:value1{:value2:value3}
numA=-1 # attachment numbering starts at 0
oIFS="$IFS"; IFS=$'\n'; for var in $commands; do
    if [[ "$var" == *:* ]]; then # does it look like a command or a naked email?
        IFS=":" read -r comm val1 val2 val3 < <(echo "$var") # using named FIFO instead of pipe to
                                                             # keep read variables in the current shell.
        case "${comm,,}" in # usage: match lowercase version of comm
            "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") # usage: attach:att_path:att_MIME_type(optional):att_name(optional)
                attachment[$((++numA))]="$val1"
                attachmentType[numA]="$val2"
                [[ "$val3" ]] && attachmentName[numA]="$val3" || attachmentName[numA]="${val1##*/}"
                # if no name given, default to short filename from path
                # add code for guessing MIME type from file extension if blank  <<<<<<<<<<<< To Do
                ;;
            "server") # usage: server(optional):server:port(optional):delay(optional)
                [[ "$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"; exit 1 ;;  #add usage/help here. <<<<<<<<<<<< To Do
        esac
    else recipients["$var"]=1; [[ ! "$blind" ]] && toList["$var"]=1
    # naked email addresses are To: unless blind is set (then BCC:)
    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" # comma+space delimited
    echo "${toString%, }" # remove last comma+spave
    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
    for i in `seq 0 "$numA"`; do
        echo "--""$boundary"
        echo "Content-Type: ""{$attachmentType[$i]}""; name=\"""${attachmentName[$i]}""\""
        echo "Content-Transfer-Encoding: base64";
        echo "Content-Disposition: attachment; filename=\"""${attachmentName[$i]}""\""
        echo
        base64 <"${attachment[$i]}"
        echo
    done
    echo "--""$boundary""--"
    echo "."
    sleep "$delay"
    echo "QUIT"; } | telnet "$server" "$port"
#
exit 0


Last edited by Michael Stora; 07-24-2014 at 02:12 PM..
# 16  
Old 07-24-2014
Quote:
Originally Posted by Michael Stora
Guys I love the parser! It is not 100% clear to me how it handles multiple spaces in a row. As far as multiple carraige returns, the behavior of read is to include blank lines as output and the behavior or for with /n as the delimiter is to skip them. Both however, treat multiple unquoted spaces or tabs as if the were one.

I assume we can add another inString variable for single quotes (') and add $'\t' (tab) to the second case condition in Chubler_XL's version.
Yes I did consider single quotes but thought the implementation added nothing interesting and detracted from the elegance of the example so consider that an "exercise for the reader". Backslash support is quite interesting, and as read only consumes single characters, it can be achieved with a single line case clause.

I didn't consider multiple white space, and this requires another state variable:

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

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

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

  export $1="$chBuffer"
  shift

  # Blank any vars not assigned
  while [ $# -gt 0 ]
  do
      export $1=""
      shift
  done
  IFS=$OIFS
  [ "$chAct" == $'\n' ]
}

In relation to random string generation: if you have openssl you could also use:

Code:
boundary="$(openssl rand -hex 32)"


Last edited by Chubler_XL; 07-25-2014 at 02:18 AM.. Reason: Tweaking to match functionality with read built-in
# 17  
Old 07-24-2014
Quote:
Originally Posted by Michael Stora
Guys I love the parser! It is not 100% clear to me how it handles multiple spaces in a row.
The way i wrote it the (unquoted) blank is a "terminal" (an expression ending the current parsing entity) - several consecutive terminals produce empty parsing entities, unless explicitly suppressed. You can do that in the " " part of the case-construct by inserting something like:

Code:
      " ")
          if [ -n "$chBuffer" ] ; then
               # output/process the buffer which just ended
          else
               # do nothing
          fi
          ;;

As for a little theory behind all that: i suggest the "Dragon Book": "Principles of Compiler Design"; Sethi, Aho, Ullmann. IMHO a must for the library of any programmer. It should be the third book apprentices buy, after the TAOCP by Knuth and "The C Programming Language" by Kernighan/Ritchie.

Quote:
Originally Posted by sreyan32
I assume we can add another inString variable for single quotes (') and add $'\t' (tab) to the second case condition in Chubler_XL's version.

Hmm . . . I had kind of assumed that ' was "superior" to ". Actually whichever one comes first is "superior"
It is a - rather common - misconception that quoting can in any way be nested. I can not! In fact the shell works the same way as my parser: it maintains a flag "inside/outside quoted string" which is switched upon every occurrence of a quote (bar escaping, etc.). So "...."...."....." is read: inside quotes after first, outside after second, inside after third and outside again after fourth quote char.

A single quote inside a double-quoted string (and vice versa) is treated as a normal character without any special meaning.

I hope this helps.

bakunin
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