Chad Clark's Open Journal : 2007-09-04

Historical Entries   Recent Entries   About The Author   RSS  

September 04, 2007 :
1) Bash tips: single quotes within single quotes and find the version number.

In BASH you are not allowed to use single quotes inside of a single quoted
string.  The man page even reads:

  Enclosing characters in single quotes preserves the literal value of each
  character within the quotes.  A single quote may not occur between single
  quotes, even when preceded by a backslash.

It turns out there is undocumented behaviour that will let you insert
backslash escaped single quotes into string delimited by single quotes.
Prefix the opening single quote with a dollar sign.

  [chad@lorax ~]$ echo 'asdf 123'
  asdf 123
  
  [chad@lorax ~]$ echo 'asdf\'123'
  >
  ---- I pressed ctrl-c here ----
  
  [chad@lorax ~]$ echo $'asdf\'123'
  asdf'123

This was tested with version:
  GNU bash, version 3.1.17(1)-release (i686-redhat-linux-gnu)

To get the version number at the shell prompt press Ctrl-X followed by
Ctrl-V ie: ^X^V

  [chad@lorax ~]$ 
  GNU bash, version 3.1.17(1)-release (i686-redhat-linux-gnu)
  [chad@lorax ~]$ 


2) Thoughts on mistakes and habits to avoid them.

Recently someone told me I make fewer mistakes than many people do.  My
first thought was we all make mistakes, myself included.

I wondered what he meant by other people make more mistakes until I
remembered a few times recently I have had to fix things someone else did.
I remembered being frustrated at having to fix stuff that never worked
simply because whoever did the work never bothered to test it.

I do not remember the last time someone told me of a mistake I made that
they had to fix.  It happens.  I just don't recall an example.

So maybe I happen to make fewer mistakes at work.  Why is that?

Testing everything you do before signing off on it is vital to not having
other people find mistakes you made.  The mistake is not so much that the
code has a bug but that you never ran the code before checking it in.

What is testing everything before you check it in?  It is a habit.  I know
that submitting untested code will bite me and others down the road.  It is
habit to test and to never think about skipping testing.

I run perl with "-w" and "use strict".  It has become habit for me.  "-w"
turns on warnings for some things and "use strict" disables some rather lax
rules.  Both of these cause perl to alert me to likely problems early on so
they don't cause weird program behaviour, bugs, and hours of debugging.

I usually walk around my car before getting in.  I check for objects like
bicycles, that my tires are not flat, and my license plate is attached.
Sometimes I only walk in front or behind if that is the direction I will be
driving.

I know a few people who have run over bicycles.  Kids leave toys in the
driveway and on the street.  Sometimes your kids sometimes other people's.

I knew someone who had their license plate stolen.  You can get a fine for
driving without a license plate even if someone stole it and you don't know
it's missing.

How many times have you heard to always make a copy of system config files
before editing them.  How often do you do it?  How many times have you
wished you had?

I create files with names like pf.conf.2007-09-04.  This gives me a file to
roll back to but it also gives me a file to diff against so I can be sure I
only changed the lines I wanted to.

When writing code with an "if" jump to the next line and add the closing
brace as soon as adding the opening brace.  Then go back and fill in the
loop.  I'm not sure how long it has been since I forgot to close a loop.

Always use braces with if.  That way you don't go back, add a new indented
line and your program breaks because C only reads the next statement after
an if when there are no braces.

You know you have a habit when someone is watching you type and they stop
you to ask why you did something and you think have to think a bit because
you always do it that way.

I'm not perfect but I try to get most of the way there.  Noticing a problem
and developing a habit to avoid that problem is part of the reason for not
making as many mistakes.  That and testing.  Always test!



Historical Entries   Recent Entries   About The Author   RSS