Posted: Mon 13 Jul 2009, 11:02
Chapter 49 - Eliminating trailing white space
I had a lengthy script, which ran very successfully. I modified it from time to time. Then one little change and it stopped running. I went over and over the change and couldn't find a thing.
I went over every aspect of the script and still could not find the trouble. I opened it with a hex editor to view things which a text editor wouldn't show and still didn't find the cause.
I eventually thought maybe if it wasn't anywhere in the printing characters, it was likely a problem caused by non printing characters.
In the vast majority of cases when your scripts don't do as intended the problem is in the printing characters. The problem I describe is very rare and maybe shouldn't even be mentioned . . .
. . .but I suppose if it happened to me, it might happen to you. Also, one thing we have very little use for in our scripts, text documents and web pages is trailing white space.
The only files we ever want to remove trailing white space on are text files. Scripts, and html files are text files.
For starters in building this script, let's see how to tell if the intended file is an actual text file. file will tell it if it is.
file index.html, says: HTML document text
file updatedb, says: Bourne-Again shell script text executable
we can use grep's return code to see if it is safe to remove trailing spaces
~~~~~~~~~~~~~~
line 1 : file $1 | grep " text" >/dev/null
line 2 : [ x$? = x0 ] && echo $1 is a text file
line 1 : file processes the file in $1, we pipe the output to grep, grep looks for this text string" file" and we redirect the output to the black hole >/dev/null
grep gave bash an invisible return code based on if it found the string " file", we use the return code in line 2
line 2 : we test the return code to see if it matches 0. Earlier I showed arithmetic comparison operators. The = is not for arithmetic, it's more for string comparisons. When making comparisons we never want to compare one string with a blank. By using the x, either side of the string comparison are assured of having some kind of value as a place holder.
~~~~~~~~~~~~~~
String comparison - not arithmetic
Does x0 == x0 ? If true it is a text file
Earlier I used quotes, like this:
[ "$?" = "0" ] && echo $1 is a text file
The quotes serve the same purpose as the x - you're developing you own style.
We will work more on our script in the next chapter
~
Chapter 49 - eliminating trailing white space
I had a lengthy script, which ran very successfully. I modified it from time to time. Then one little change and it stopped running. I went over and over the change and couldn't find a thing.
I went over every aspect of the script and still could not find the trouble. I opened it with a hex editor to view things which a text editor wouldn't show and still didn't find the cause.
I eventually thought maybe if it wasn't anywhere in the printing characters, it was likely a problem caused by non printing characters.
In the vast majority of cases when your scripts don't do as intended the problem is in the printing characters. The problem I describe is very rare and maybe shouldn't even be mentioned . . .
. . .but I suppose if it happened to me, it might happen to you. Also, one thing we have very little use for in our scripts, text documents and web pages is trailing white space.
The only files we ever want to remove trailing white space on are text files. Scripts, and html files are text files.
For starters in building this script, let's see how to tell if the intended file is an actual text file. file will tell it if it is.
file index.html, says: HTML document text
file updatedb, says: Bourne-Again shell script text executable
we can use grep's return code to see if it is safe to remove trailing spaces
~~~~~~~~~~~~~~
line 1 : file $1 | grep " text" >/dev/null
line 2 : [ x$? = x0 ] && echo $1 is a text file
line 1 : file processes the file in $1, we pipe the output to grep, grep looks for this text string" file" and we redirect the output to the black hole >/dev/null
grep gave bash an invisible return code based on if it found the string " file", we use the return code in line 2
line 2 : we test the return code to see if it matches 0. Earlier I showed arithmetic comparison operators. The = is not for arithmetic, it's more for string comparisons. When making comparisons we never want to compare one string with a blank. By using the x, either side of the string comparison are assured of having some kind of value as a place holder.
~~~~~~~~~~~~~~
String comparison - not arithmetic
Does x0 == x0 ? If true it is a text file
Earlier I used quotes, like this:
[ "$?" = "0" ] && echo $1 is a text file
The quotes serve the same purpose as the x - you're developing you own style.
We will work more on our script in the next chapter
~
Chapter 49 - eliminating trailing white space