DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 27th December 2009
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 3,140
Thanked 182 Times in 149 Posts
Default ksh quote or no quote whitespace madness

The program:
Code:
#!/bin/sh
    
THIS='   aa
bb
    cc
dd
ee
ff'
    
THAT="   zz
yy
    xx
ww
"

echo \"THIS: [\${THIS}]\"    
echo -------------------
echo "THIS: [${THIS}]"

echo

echo THIS: [\${THIS}]
echo -------------------
echo THIS: [${THIS}]

echo

echo \"THAT: [\${THAT}]\"
echo -------------------
echo "THAT: [${THAT}]"

echo

echo THAT: [\${THAT}]
echo -------------------
echo THAT: [${THAT}]
A run and the output
Code:
$ sh this-that
"THIS: [${THIS}]"
-------------------
THIS: [   aa
bb
    cc
dd
ee
ff]

THIS: [${THIS}]
-------------------
THIS: [ aa bb cc dd ee ff]

"THAT: [${THAT}]"
-------------------
THAT: [   zz
yy
    xx
ww
]

THAT: [${THAT}]
-------------------
THAT: [ zz yy xx ww ]
Who can explain?
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #2   (View Single Post)  
Old 27th December 2009
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

What were you expecting? Looks perfectly normal to me J65nko but I'm a bit drowsy....

Code:
Terry@vectra$ echo `cat this-that`
#!/bin/sh THIS=' aa bb cc dd ee ff' THAT=" zz yy xx ww " echo \"THIS: [\${THIS}]\" echo ------------------- echo "THIS: [${THIS}]" echo echo THIS: [\${THIS}] echo ------------------- echo THIS: [${THIS}] echo echo \"THAT: [\${THAT}]\" echo ------------------- echo "THAT: [${THAT}]" echo echo THAT: [\${THAT}] echo ------------------- echo THAT: [${THAT}]

Quote:
Originally Posted by 2.6.3 Command Substitution
The shell shall expand the command substitution by executing command in a subshell environment (see Shell Execution Environment) and replacing the command substitution (the text of command plus the enclosing "$()" or backquotes) with the standard output of the command, removing sequences of one or more <newline>s at the end of the substitution. Embedded <newline>s before the end of the output shall not be removed; however, they may be treated as field delimiters and eliminated during field splitting, depending on the value of IFS and quoting that is in effect.

Quote:
Originally Posted by 2.2.3 Double Quotes
Enclosing characters in double-quotes ( "" ) shall preserve the literal value of all characters within the double-quotes, with the exception of the characters dollar sign, backquote, and backslash, as follows:
There might be more HERE, but I'm to sleepy to parse it right now.



echo "$THIS" preserves things just as you would expect, that's the double quotes job.

echo $THIS substitutes $THIS with the contents of the THIS variable, which I assume is subject to field splitting.

Unless you screw with IFS, echo $THIS should be equivalent to this:

Code:
echo    aa \
bb \
    cc \
dd \
ee \
ff'
Which contains the `words` aa, bb, cc, dd, ee, and ff, plus a new line; because of how the shell does input field splitting!



Maybe I'm just to sleepy or perhaps I missed some trick about the question?
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
  #3   (View Single Post)  
Old 27th December 2009
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Thanked 49 Times in 43 Posts
Default

Quote:
Originally Posted by J65nko View Post
...
THAT=" zz
yy
xx
ww
"

...

echo THAT: [${THAT}][/code]

...

THAT: [ zz yy xx ww ][/code]Who can explain?
[, ], zz, yy, xx, ww become individual arguments (default IFS is whitespace) to the echo command & echo prints its arguments separated by a space.
You have used single/double quotes to define $THIS & $THAT respectively but it makes no difference as the quotes are not preserved in the shell variable.
Was that the question?

Last edited by ephemera; 27th December 2009 at 07:09 AM.
Reply With Quote
  #4   (View Single Post)  
Old 28th December 2009
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 3,140
Thanked 182 Times in 149 Posts
Default

I found in this in a on old dump(8) I restored 2 days ago. What else has a man to do on the second day of Christmas?

I cannot recall why I wrote it, probably to show somebody on bsdforums.org who had a problem with this, at least for most beginning sh programmers, whitespace madness.

Indeed it has like both of you mentioned something to do with 'IFS' (inter field separator), a shell variable holding the characters, the shell uses to break up a string into words.
Code:
$ X='x      y    z'

$ echo "$X"
x      y    z

$ echo $X
x y z

$ IFS_ORIG=$IFS
$ IFS=''

$ echo $X       
x      y    z

$ IFS=$IFS_ORIG
$ echo $X
x y z
As shown here, after setting IFS to nothing, the "echo $X" shows the original whitespace.
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #5   (View Single Post)  
Old 28th December 2009
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

Quote:
Originally Posted by J65nko View Post
I found in this in a on old dump(8) I restored 2 days ago. What else has a man to do on the second day of Christmas?
On the second day of Christmas, my dump restored to me two geeks and a shell script in a pear tree.



Sorry, couldn't resist
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
Reply

Tags
echo, quotes, sh whitespace

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
interrupt storm and irq madness siffland FreeBSD General 5 23rd October 2009 05:16 AM
escape single quote in sed gosha Programming 5 9th March 2009 10:22 AM


All times are GMT. The time now is 09:00 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick