Page 1 of 1

trailing whitespace (Solved)

Posted: Wed 27 Dec 2017, 22:19
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

____________________________________

Re: trailing whitespace

Posted: Thu 28 Dec 2017, 02:10
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.

Posted: Fri 29 Dec 2017, 20:18
by don570
you didn't change $IFS back to its default value
I hadn't thought about IFS. :oops:

Posted: Tue 02 Jan 2018, 17:42
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.

Posted: Tue 02 Jan 2018, 17:44
by don570
I noticed that someone has put up a good page with examples to
explain gtkdialog
http://xpt.sourceforge.net/techdocs/lan ... gExamples/
____________________________________________