Showing posts with label Jürgen Gulbins. Show all posts
Showing posts with label Jürgen Gulbins. Show all posts

Friday, July 9, 2010

best practices in shell script programming – double quotes


You do know the result of this:
a=A; echo $a
But are you just as sure here?
a=A; b=B; echo $a.$b
That depends on the shell, you are using. So I suggest you better write it this way:

a=A; b=B; echo ${a}.${b}
Enclosing variable names in curly braces is quite often a good idea.

Look at the following piece of code:
case $var in
  x*)
    echo var starts with x
    ;;
  *)
    echo var starts with something else
    ;;
esac
Looks alright, doesn't it?
No, it's does not. If that variable had not been assigned a value before or is of zero length, you will see an ugly syntax error occur.
Therefore enclose the variable in double quotes like here:
case "$var" in
  x*)
    echo var starts with x
    ;;
  *)
    echo var starts with something else
    ;;
esac
There is no good excuse for not doing it anywhere. It may look ugly and unnecessary, but it will help. That's the way shell scripting is. I learned this from Jürgen Gulbins around 1987, when I enjoyed working for him. This series of articles is dedicated to him. I owe him a lot.

Friday, December 18, 2009

editing files with VI and enforcing certain editor settings

You do know, that you can request VI (or VIM)  to make using the tab key equivalent to going e.g. 4 positions to the right (":set tabstop=4"). But what if your colleague opens that file and has a global setting like ":set tabstop=8"?
As long as you don't "globally" agree on this, it will at least help to add a setting with a "file-wide" scope. VI's feature supporting such options is called modeline. There are 2 related settings, that make the use of that feature possible resp. impossible: "[no]modeline", and "modelines=n". For further reading do a ":help modeline" in VIM! (I guess, there are not that many original UCB VI-s around nowadays any more) That shows you samples and the exact syntax for how to add modelines.
I saw Jürgen Gulbins using this feature like 25 years ago, found it useful then, but never applied it myself in the meantime. But now in my current project with mixed VIM and Eclipse usage it seems to make very good sense, but I wasn't able to recall the details. The article, that my Google enquiry pointed me to, was posted in December 2005 on the linux-il list: "vim inline :set". Thanks to Meir Kriheli for answering the question there!