Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Problem with arrays -- "unknown file attribute"



On Mon, Jan 05, 2009 at 10:42:09AM -0800, Webb Sprague wrote:
> Thanks Stephane for all your comments on zsh scripting.  I just have
> one question below...
> 
> > Or you could keep the default value of IFS which in zsh is
> > $' \t\n\0' (in other shells it's $' \t\n' as they don't support
> > the NUL character anyway).
> 
> What does the dollar do in IFS?  I actually have to get rid of it in
> order to make the script work.


$'...' is another kind of quotes that come from ksh93. Shells
got it wrong from the start in not expanding the \t, \n, \a...
in their double quotes. Instead it is tools like echo, printf,
awk... that expand them each one in its own way and causing all
sorts of problems as they are then expanded even when we don't
want them to. Fixing that was impossible as that would have
broken scripts, so Korn introduced a new kind of quote that
is used to expand those sequences. '\t' is the sequence of the 2
characters, \ and t, so in IFS that means both \ and t are
separators; and $'\t' is the tab character.

>I think it might be part of the reason
> all of my array conversions have been giving me hell, actually.
> 
> Here is a working function (so far -- the inserts might have something lurking):
> 
> pop-colnames () {
>     setopt localoptions errreturn
>     local IFS=' \t\n'

That means space, \, t and n are separators.

>     TABS=($(sqlite3 $DB .tables))
>     for TAB in $TABS; do
>         echo $TAB 2>&1

echo doesn't output anything on its standard error, so it makes
no sense to redirect it.

>         COLS=($(sqlite3 $DB "select * from $TAB where ROWID=1;"| sed
> 's/\"//g' | sed 's/|/\\t/g'))
[...]

You don't need 2 instances of sed and you don't need to escape "
as it is not special to sed (while there's no guarantee that \"
is special in some sed implementation).

sed 's/"//g;s/|/\\t/g'

-- 
Stéphane



Messages sorted by: Reverse Date, Date, Thread, Author