[OT] Re: how to start in "overwrite-mode"

2009-10-28 Thread Stephane CHAZELAS
2009-10-27, 16:29(-04), Jim Lawson:
[...]
> I have a user we're trying to encourage to migrate from tcsh to bash,
> who is used to his shell starting up in overwrite mode (as opposed to
> the default Insert mode.)
>
> Long story short, while I can easily bind a key to the "overwrite-mode"
> readline function, I can't figure out how to make bind start up that way
> as I can't find an "overwrite" readline variable which I can manipulate.
>  And the user doesn't want to hit "Ins" every time he starts a shell.
>
> Anyone know how to do this?
[...]

Sorry, I don't know how to do it with bash, but here's a
solution for zsh (switching from tcsh to zsh might reveal
easier btw):

in ~/.zshrc, add:

zle-line-init() zle overwrite-mode
zle -N zle-line-init

To get back on topic, bash is the only Bourne-like shell that I
know that doesn't allow that function definition syntax above,
I've always wondered why.

-- 
Stéphane


OT: Getting MySQL fields with embedded spaces into array

2009-10-28 Thread Gerard
I apologize for asking this here; however, I cannot seem to locate an
answer anywhere else.

I am writing a script that will use MySQL for a back end. I access MySQL
and store the results in arrays like this:

//snippet//

database: MyDataBase
table: MyTable
field: defaults

Now, I have populated the 'defaults' fields with the declare
statements that I will use in the script. They are entered similar to
this:
 
declare -a MSRBL_LIST

Now, I issue this from my bash script:

SQL_USER=user   # MySQL user
SQL_PASSWORD=secret # MySQL password
DB=MyDataBase   # MySQL data base name
HOST=127.0.0.1  # Server to connect to
NO_COLUMN_NAME="--skip-column-names"
COM_LINE="-u${SQL_USER} -p${SQL_PASSWORD} -h ${HOST} ${NO_COLUMN_NAME}"

table=MyTable

DECLARE_STATEMENTS=($(mysql ${COM_LINE} -i -e"use ${DB}; SELECT defaults FROM 
"${table}" WHERE 1;"))

for (( i=0;i<${#DECLARE_STATEMENTS[*]};i++)); do
echo  ${DECLARE_STATEMENTS[i]}
done

//end snippet//
 
This output is produced:

declare
-a
MSRBL_LIST

Obviously, that is not what I want. I have tried setting:

IFS=$( echo )

Prior to the invocation of the "DECLARE_STATEMENTS" statement; however,
that produced another error message and MySQL would not start.

I have been exploring different hacks to make this work. Perhaps
writing to a file and then using 'READ' to put the data into an array.
I was hoping that someone might have a working solution that I could
try. This problem only happens when the data stored in a MySQL field
contains embedded spaces or tabs. At least that is all that I am aware
of.

Thanks!

-- 
Gerard
ger...@seibercom.net

|===
|===
|===
|===
|

It isn't easy being the parent of a six-year-old.  However, it's a
pretty small price to pay for having somebody around the house who
understands computers.





Re: OT: Getting MySQL fields with embedded spaces into array

2009-10-28 Thread Greg Wooledge
On Wed, Oct 28, 2009 at 06:04:00AM -0400, Gerard wrote:
> COM_LINE="-u${SQL_USER} -p${SQL_PASSWORD} -h ${HOST} ${NO_COLUMN_NAME}"
> table=MyTable
> DECLARE_STATEMENTS=($(mysql ${COM_LINE} -i -e"use ${DB}; SELECT defaults FROM 
> "${table}" WHERE 1;"))

You're populating an array with each word (not each line) of the output
of your mysql command.

> This output is produced:
> 
> declare
> -a
> MSRBL_LIST
> 
> Obviously, that is not what I want.

(Not obvious to us.  Maybe so to you. ;-) )

> I have tried setting:
>   IFS=$( echo )

$() removes all trailing newlines from the output of the command that
it executes.  You're setting IFS to an empty string.  If you want to
set IFS to a newline, use this:

IFS=$'\n'

Or this:

IFS='
'

> I have been exploring different hacks to make this work. Perhaps
> writing to a file and then using 'READ' to put the data into an array.

'read' is the most flexible way, though you don't need a temporary file
to do this.  I have some more documentation on this approach here:
http://mywiki.wooledge.org/BashFAQ/005




Re: [OT] Re: how to start in "overwrite-mode"

2009-10-28 Thread Chet Ramey
> zle-line-init() zle overwrite-mode
> zle -N zle-line-init
> 
> To get back on topic, bash is the only Bourne-like shell that I
> know that doesn't allow that function definition syntax above,
> I've always wondered why.

The Posix grammar has never allowed it (a `function_body' must be a
compound command, and that's what bash implements), and there has
never been sufficient demand to add it as an extension. 

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/




Re: [OT] Re: how to start in "overwrite-mode"

2009-10-28 Thread Stephane CHAZELAS
2009-10-28 09:00:59 -0400, Chet Ramey:
> > zle-line-init() zle overwrite-mode
> > zle -N zle-line-init
> > 
> > To get back on topic, bash is the only Bourne-like shell that I
> > know that doesn't allow that function definition syntax above,
> > I've always wondered why.
> 
> The Posix grammar has never allowed it (a `function_body' must be a
> compound command, and that's what bash implements), and there has
> never been sufficient demand to add it as an extension. 
[...]

It's never allowed it to POSIX _scripts_, but it won't make a
shell non-conformant to support it (and ksh, pdksh, ash, zsh
even posh all do support it), just like most POSIX conformant
shells support arrays for instance even though that's not a
POSIX feature.

bash not supporting it helps for writing POSIX compliant scripts
(as it will return an error if one tries to use that
non-standard feature), but as bash is the only POSIX shell that
doesn't support it, it feels a bit silly.

I wonder what's the rationale for POSIX not supporting it (is it
because of bash?).

-- 
Stephane




Re: [OT] Re: how to start in "overwrite-mode"

2009-10-28 Thread Chet Ramey
Stephane CHAZELAS wrote:
> 2009-10-28 09:00:59 -0400, Chet Ramey:
>>> zle-line-init() zle overwrite-mode
>>> zle -N zle-line-init
>>>
>>> To get back on topic, bash is the only Bourne-like shell that I
>>> know that doesn't allow that function definition syntax above,
>>> I've always wondered why.
>> The Posix grammar has never allowed it (a `function_body' must be a
>> compound command, and that's what bash implements), and there has
>> never been sufficient demand to add it as an extension. 
> [...]
> 
> It's never allowed it to POSIX _scripts_, but it won't make a
> shell non-conformant to support it (and ksh, pdksh, ash, zsh
> even posh all do support it), just like most POSIX conformant
> shells support arrays for instance even though that's not a
> POSIX feature.

That's why I said it would be added as an extension.

> bash not supporting it helps for writing POSIX compliant scripts
> (as it will return an error if one tries to use that
> non-standard feature), but as bash is the only POSIX shell that
> doesn't support it, it feels a bit silly.

"Silly" it may be, but there's not really the demand for it.

> I wonder what's the rationale for POSIX not supporting it (is it
> because of bash?).

That would be the tail wagging the dog, wouldn't it?  It's never been
in the grammar.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/




Re: [OT] Re: how to start in "overwrite-mode"

2009-10-28 Thread Stephane CHAZELAS
2009-10-28 09:46:49 -0400, Chet Ramey:
[...]
> >> The Posix grammar has never allowed it (a `function_body' must be a
> >> compound command, and that's what bash implements), and there has
> >> never been sufficient demand to add it as an extension. 
> > [...]
> > 
> > It's never allowed it to POSIX _scripts_, but it won't make a
> > shell non-conformant to support it (and ksh, pdksh, ash, zsh
> > even posh all do support it), just like most POSIX conformant
> > shells support arrays for instance even though that's not a
> > POSIX feature.
> 
> That's why I said it would be added as an extension.

Sorry, I overlooked that bit.

> > bash not supporting it helps for writing POSIX compliant scripts
> > (as it will return an error if one tries to use that
> > non-standard feature), but as bash is the only POSIX shell that
> > doesn't support it, it feels a bit silly.
> 
> "Silly" it may be, but there's not really the demand for it.

I can understand it. I was more curious about the origins. After
all, that breaks Bourne backward compatibility (in a shell
called Bourne-again shell)) and it seems more effort to exclude
one form of command rather than include any form of command.

f() for i in 1; do echo test; done
f() { echo test; } > xx

are allowed (and I doubt there has been a lot of demand for
that), and can be confusing all the same (the second one for
instance is not interpreted POSIXly by zsh).

> > I wonder what's the rationale for POSIX not supporting it (is it
> > because of bash?).
> 
> That would be the tail wagging the dog, wouldn't it?  It's never been
> in the grammar.

OK, but again, why exclude simple commands here? Would there be
some implication? Has there be historical versions of sh that
didn't support it?

I guess I should ask the question on the POSIX list.

Cheers,
Stephane




Re: [OT] Re: how to start in "overwrite-mode"

2009-10-28 Thread Greg Wooledge
On Wed, Oct 28, 2009 at 02:00:53PM +, Stephane CHAZELAS wrote:
> I can understand it. I was more curious about the origins. After
> all, that breaks Bourne backward compatibility (in a shell
> called Bourne-again shell)

Bourne shell has no functions at all.

> Has there be historical versions of sh that
> didn't support it?

Bourne shell on Ultrix and (I believe still to this day) Sun Solaris.
No functions at all.  None.




Re: [OT] Re: how to start in "overwrite-mode"

2009-10-28 Thread Chris F.A. Johnson
On Wed, 28 Oct 2009, Greg Wooledge wrote:

> On Wed, Oct 28, 2009 at 02:00:53PM +, Stephane CHAZELAS wrote:
> > I can understand it. I was more curious about the origins. After
> > all, that breaks Bourne backward compatibility (in a shell
> > called Bourne-again shell)
> 
> Bourne shell has no functions at all.

   Had. Only before 1984. Since then it has had functions.

   The first shell I used, the Bourne shell on AT&T SVR3.2, had
   functions.

-- 
   Chris F.A. Johnson, webmaster 
   ===
   Author:
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)




Re: [OT] Re: how to start in "overwrite-mode"

2009-10-28 Thread Stephane CHAZELAS
2009-10-28 11:06:09 -0400, Chris F.A. Johnson:
> On Wed, 28 Oct 2009, Greg Wooledge wrote:
> 
> > On Wed, Oct 28, 2009 at 02:00:53PM +, Stephane CHAZELAS wrote:
> > > I can understand it. I was more curious about the origins. After
> > > all, that breaks Bourne backward compatibility (in a shell
> > > called Bourne-again shell)
> > 
> > Bourne shell has no functions at all.
> 
>Had. Only before 1984. Since then it has had functions.
> 
>The first shell I used, the Bourne shell on AT&T SVR3.2, had
>functions.
[...]

Yes,

The best place for information on the Bourne shell IMO is
http://www.in-ulm.de/~mascheck/bourne/index.html

Where we learn that Solaris (well SunOS at the time) sh has had
functions since SunOS 3, which according to
http://www.levenez.com/unix/ (another great site about Unix
history) takes us back to early 1986.

-- 
Stephane




Re: [OT] Re: how to start in "overwrite-mode"

2009-10-28 Thread Chet Ramey
Stephane CHAZELAS wrote:
> 2009-10-28 11:06:09 -0400, Chris F.A. Johnson:
>> On Wed, 28 Oct 2009, Greg Wooledge wrote:
>>
>>> On Wed, Oct 28, 2009 at 02:00:53PM +, Stephane CHAZELAS wrote:
 I can understand it. I was more curious about the origins. After
 all, that breaks Bourne backward compatibility (in a shell
 called Bourne-again shell)
>>> Bourne shell has no functions at all.
>>Had. Only before 1984. Since then it has had functions.
>>
>>The first shell I used, the Bourne shell on AT&T SVR3.2, had
>>functions.
> [...]
> 
> Yes,
> 
> The best place for information on the Bourne shell IMO is
> http://www.in-ulm.de/~mascheck/bourne/index.html
> 
> Where we learn that Solaris (well SunOS at the time) sh has had
> functions since SunOS 3, which according to
> http://www.levenez.com/unix/ (another great site about Unix
> history) takes us back to early 1986.

Functions were the last thing added to sh by Steve Bourne before he
left Bell Labs (he finished in 1983) and first appeared in the SVR2
shell (1984).

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/




Re: how to start in "overwrite-mode"

2009-10-28 Thread Dave Rutherford
On Tue, Oct 27, 2009 at 16:29, Jim Lawson  wrote:
> I have a user we're trying to encourage to migrate from tcsh to bash,
> who is used to his shell starting up in overwrite mode (as opposed to
> the default Insert mode.)
>
> Long story short, while I can easily bind a key to the "overwrite-mode"
> readline function, I can't figure out how to make bind start up that way
> as I can't find an "overwrite" readline variable which I can manipulate.
>  And the user doesn't want to hit "Ins" every time he starts a shell.
>
> Anyone know how to do this?

With the caveats that this is an awful solution, and it requires
that the user be willing to commit to one or two terminal types
(say, Xterm), I found a horrible way to do this.

The control characters below are literal in the real files.
Assuming the insert key on your keyboard is ^[[2~ :

Add to .Xdefaults: XTerm*answerbackString: ^[[2~
Add to .bashrc (and .profile if it doesn't source .bashrc):
bind '"^[[2~": overwrite-mode'
PROMPT_COMMAND='echo -n ^E'

This causes xterm to respond by simulating a pressing
of the insert key before each PS1 prompt, putting that
line into overwrite. If the user drops down to a PS2, etc.
prompt, that line will be in insert mode. I didn't solve that;
putting a ^E into PS* didn't work for some reason. (Why?)

Dave