Friday, 10 September 2010

bash/sh/ksh syntax: function needs whitespace after initial {

# this is broken:
$ function cdt {cd $TTM_TESTS_DIR;pwd;}

# this is also broken:
$ function cdt { cd $TTM_TESTS_DIR;pwd }

# this is okay
$ function cdt { echo "tcl isn't the only language with whitespace-dependant syntax. though looking mostly like a tokenised parsed language.";cd $TTM_TESTS_DIR;pwd;}

So like tcl sh/bash not really parsed in a properly tokenised way.
Mmmm.
Specifically here:
1. needs whitespace after first { of function
2. needs ; after last command before closing }  (doesn't matter about whitespace here)

Aliases are bad. For many reasons.One reason being vars in aliases expanded at definition time.
So if the variable is redefined then the alias doesn't match. Alias must also be redefined when changing variable.

man bash explains it better and says: "For almost every purpose, aliases are superseded by shell functions."
 http://www.gnu.org/software/bash/manual/bashref.html#Aliases




Best answer ever to question on parsing html tags with regular expressions:
http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454
This is more fun as it has a lovely pic of Cthulhu for illustrative purposes.
http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html
Cthulhu  ach, Cthulhu isn’t in my spell-check dictionary!
What an oversight!
Tsk!




Checked functrion cdt on a few bash versions.
These are the errors seen:

GNU bash, version 3.2.49(23)-release (i686-pc-cygwin)
GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
sh on qnx is ksh 6.4.1

sh: syntax error near unexpected token `{cd'    (sh on this cygwin IS bash)
bash: syntax error near unexpected token `{cd'
-bash: syntax error near unexpected token `{cd'


qnx$ strings $(which sh) |grep -C8 -i version
The ksh is a public-domain version of the Korn shell. It's a command
interpreter that's intended for both interactive and shell
script use.
For more information please refer to a ksh or unix shell reference.
NAME=ksh
DESCRIPTION=Public Domain Korn Shell
DATE=2009/05/20-17:46:32-EDT
STATE=stable
HOST=cctrunk
USER=builder
TAGID=166
VERSION=6.4.1

No comments: