trailing whitespace (Solved)

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

trailing whitespace (Solved)

#1 Post by don570 »

There is a strange behaviour I noticed with trailing whitespace.
The following isn't successful...

Code: Select all

# A="Note: ";[ $A = "Note: " ] &&  echo successful

If I assume that there is no trailing whitespace....

Code: Select all

# A="Note: ";[ $A = "Note:" ] &&  echo successful
successful
An extra layer of brackets also solves the problem....

Code: Select all

# A="Note: ";[[ $A = "Note: " ]] &&  echo successful
successful
 
______________________________________________________

I checked with a gtkdialog script and there doesn't appear to be any
problem with trailing whitespace.

Code: Select all

#!/bin/bash
# whitespace experiment

export DIALOG='<window>
 <vbox>
 

 <frame>
<hbox>  
  <entry>
        <default>"Note: "</default>

    <variable>MESSAGE</variable>
  </entry>
</hbox>
</frame>
<button ok>             
	   </button>	   

 </vbox>
 </window>
' 
 I=$IFS; IFS=""
     for STATEMENTS in  $(gtkdialog --geometry=+200+100 --program DIALOG); do
       eval $STATEMENTS        
     done
# check for trailing  whitespace     
    
A="$MESSAGE";[ $A = "Note: " ] &&  xmessage success

____________________________________

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

Re: trailing whitespace

#2 Post by MochiMoppel »

don570 wrote:The following isn't successful...

Code: Select all

# A="Note: ";[ $A = "Note: " ] &&  echo successful
A="Note: ";[ "$A" = "Note: " ] && echo successful
I checked with a gtkdialog script and there doesn't appear to be any problem with trailing whitespace.
...because you didn't change $IFS back to its default value.

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#3 Post by don570 »

you didn't change $IFS back to its default value
I hadn't thought about IFS. :oops:

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#4 Post by don570 »

I need to read more about IFS. It's mentioned in.
Advanced Bash Manual
_____________________________________________________
________________________________________________________

Code: Select all

$IFS defaults to whitespace (space, tab, and newline), but may be changed, for example, to parse a comma-separated data file. Note that $* uses the first character held in $IFS. See Example 5-1.

bash$ echo "$IFS"

(With $IFS set to default, a blank line displays.)
	      


bash$ echo "$IFS" | cat -vte
 ^I$
 $
(Show whitespace: here a single space, ^I [horizontal tab],
  and newline, and display "$" at end-of-line.)



bash$ bash -c 'set w x y z; IFS=":-;"; echo "$*"'
w:x:y:z
(Read commands from string and assign any arguments to pos params.)
	      

Set $IFS to eliminate whitespace in pathnames.

IFS="$(printf '\n\t')"   # Per David Wheeler.

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#5 Post by don570 »

I noticed that someone has put up a good page with examples to
explain gtkdialog
http://xpt.sourceforge.net/techdocs/lan ... gExamples/
____________________________________________

Post Reply