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.
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."

Best answer ever to question on parsing html tags with regular expressions:
This is more fun as it has a lovely pic of Cthulhu for illustrative purposes.
Cthulhu  ach, Cthulhu isn’t in my spell-check dictionary!
What an oversight!

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.
DESCRIPTION=Public Domain Korn Shell

No comments: