* noquote for quotes as in noglob for filename generation?
@ 2009-10-10 3:01 Nazri Ramliy
2009-10-10 3:30 ` Philippe Troin
0 siblings, 1 reply; 7+ messages in thread
From: Nazri Ramliy @ 2009-10-10 3:01 UTC (permalink / raw)
To: zsh-users
Hello list,
Is there an equivalent 'noquote' for quotes (as in noglob for filename
generation)?
What I'm trying todo:
I have a shell script named 'sql':
that does pretty much this:
$ cat ~/bin/sql
<uninteresting details omitted...>
sql_command=`noglob echo "$@"` # Note the noglob!
eval mysql -u $username -p$password $db "\"$sql\""
And in my .zshrc:
alias select='sql select'
alias insert='sql insert'
alias update='sql update'
This allows me to run sql queries directly from zsh command line prompt without
directly invoking mysql. The advantage is obvious: I can run sql commands
directly from zsh prompt, which gives me all the bells and whistles of command
line processing that comes with zsh (honorary mention: the _programmable_
completion).
The 'noglob' above allows me to do
$ select * from Artist
instead of
$ select \* from Artist
Now I want to be able to do
$ select * from Artist where name like '%Jackson'
instead of
$ select * from Artist where name like \'%Jackson\'
Is there an easy solution (like noglob) that I can use here?
Thanks in advance for any pointers and help.
Nazri.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: noquote for quotes as in noglob for filename generation?
2009-10-10 3:01 noquote for quotes as in noglob for filename generation? Nazri Ramliy
@ 2009-10-10 3:30 ` Philippe Troin
[not found] ` <544dda350910100053s499c0827yd54408b7f82b696d@mail.gmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: Philippe Troin @ 2009-10-10 3:30 UTC (permalink / raw)
To: Nazri Ramliy; +Cc: zsh-users
Nazri Ramliy <ayiehere@gmail.com> writes:
> What I'm trying todo:
>
> I have a shell script named 'sql':
> that does pretty much this:
>
> $ cat ~/bin/sql
> <uninteresting details omitted...>
> sql_command=`noglob echo "$@"` # Note the noglob!
> eval mysql -u $username -p$password $db "\"$sql\""
I don't see why you need eval or noglob here...
What's wrong with replacing:
sql_command=`noglob echo "$@"` # Note the noglob!
eval mysql -u $username -p$password $db "\"$sql\""
with simply:
mysql -u $username -p$password $db "$*"
?
The mechanism you want is:
${(q)variable} # Escaped
${(qq)variable} # Single quoted
${(qqq)variable} # Double quoted
(depending on which kind of quoting you want).
But again, if you remove the unnecessary intermediary variable and
eval, you shouldn't need it.
Phil.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: noquote for quotes as in noglob for filename generation?
[not found] ` <544dda350910100053s499c0827yd54408b7f82b696d@mail.gmail.com>
@ 2009-10-10 9:33 ` Nazri Ramliy
2009-10-11 11:07 ` Sebastian Stark
0 siblings, 1 reply; 7+ messages in thread
From: Nazri Ramliy @ 2009-10-10 9:33 UTC (permalink / raw)
To: zsh-users
On Sat, Oct 10, 2009 at 11:30 AM, Philippe Troin <phil@fifi.org> wrote:
> I don't see why you need eval or noglob here...
> What's wrong with replacing:
>
> sql_command=`noglob echo "$@"` # Note the noglob!
> eval mysql -u $username -p$password $db "\"$sql\""
>
> with simply:
>
> mysql -u $username -p$password $db "$*"
>
> ?
Nothing's wrong with it and it's actually simpler and better. The two lines
are just old artifacts.
> The mechanism you want is:
>
> ${(q)variable} # Escaped
> ${(qq)variable} # Single quoted
> ${(qqq)variable} # Double quoted
Thank you Philippe for the tip (I learned something new). But I don't
think that's what I want here (or maybe I'm just too stupid to see how I
can make use of it to do what I want :)
What I want is for the script to receive the quotes that I gave it from
the command line 'unscathed'.
For example, if I ran:
$ sql select * from Artist where first_name like 'Michael %'
I want the script to run this:
mysql -u $user -p$password $db -e "select * from Artist where
first_name like 'Michael %'"
but instead what the script run is:
mysql -u $user -p$password $db -e "select * from Artist where
first_name like Michael %"
which is wrong in SQL parlance because of the missing quote around "Michael %".
Right now I have to escape the single quotes:
$ sql select * from Artist where first_name link \'Michael %\'
which is quite cumbersome as it is distracting to my train of thought
when building the sql command.
I'm thinking of doing something in preexec() to achieve this but that
seems a bit overkill for
that single shell script.
While we're on this topic I wonder if it would also possible to do
something similar for
parenthesis as well but that might be asking for too much.
Nazri.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: noquote for quotes as in noglob for filename generation?
2009-10-10 9:33 ` Nazri Ramliy
@ 2009-10-11 11:07 ` Sebastian Stark
2009-10-11 14:58 ` Dražen Kačar
0 siblings, 1 reply; 7+ messages in thread
From: Sebastian Stark @ 2009-10-11 11:07 UTC (permalink / raw)
To: Nazri Ramliy; +Cc: zsh-users
Am 10.10.2009 um 11:33 schrieb Nazri Ramliy:
>> The mechanism you want is:
>>
>> ${(q)variable} # Escaped
>> ${(qq)variable} # Single quoted
>> ${(qqq)variable} # Double quoted
>
> Thank you Philippe for the tip (I learned something new). But I don't
> think that's what I want here (or maybe I'm just too stupid to see
> how I
> can make use of it to do what I want :)
You can’t prevent zsh from removing the quotes if you add them to the
command line of your sql script, as far as I know.
But using the (q..) expansion flag you can re-add the quotes later.
> What I want is for the script to receive the quotes that I gave it
> from
> the command line 'unscathed'.
>
> For example, if I ran:
>
> $ sql select * from Artist where first_name like 'Michael %‘
In the sql script the word ‚Michael %‘ will end up in some variable,
without the quotes. The script won’t even see that it was quoted
before. If you expand this variable, add the (qq) flag.
I guess your main problem in this is that you’re using $* to simply
pass all positional parameters of the script to the actual mysql
command.
What you need is a mechanism that finds out which of those parameters
should be single quoted (sql strings). Maybe it is enough if you quote
all words that have a space in them, very roughly:
for a in $*
do
if [[ -z ${a:#* *} ]]
then
print ${(qq)a}
else
print $a
fi
done
Sebastian
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: noquote for quotes as in noglob for filename generation?
2009-10-11 11:07 ` Sebastian Stark
@ 2009-10-11 14:58 ` Dražen Kačar
2009-10-11 15:05 ` Mikael Magnusson
0 siblings, 1 reply; 7+ messages in thread
From: Dražen Kačar @ 2009-10-11 14:58 UTC (permalink / raw)
To: Sebastian Stark; +Cc: Nazri Ramliy, zsh-users
Sebastian Stark wrote:
> Am 10.10.2009 um 11:33 schrieb Nazri Ramliy:
>>> The mechanism you want is:
>>>
>>> ${(q)variable} # Escaped
>>> ${(qq)variable} # Single quoted
>>> ${(qqq)variable} # Double quoted
>>
>> Thank you Philippe for the tip (I learned something new). But I don't
>> think that's what I want here (or maybe I'm just too stupid to see how
>> I
>> can make use of it to do what I want :)
>
> You can't prevent zsh from removing the quotes if you add them to the
> command line of your sql script, as far as I know.
>
> But using the (q..) expansion flag you can re-add the quotes later.
I think he wants Perl quoting operators:
Customary Generic Meaning Interpolates
'' q{} Literal no
"" qq{} Literal yes
`` qx{} Command yes*
qw{} Word list no
// m{} Pattern match yes*
qr{} Pattern yes*
s{}{} Substitution yes*
tr{}{} Transliteration no (but see below)
<<EOF here-doc yes*
* unless the delimiter is ''.
So one could write "ordinary" quoting characters for other programming
languages, without quoting those characters in the zsh script source. That
would look like this:
sql_execute q{select * from foo where id='bar';}
Since the pair 'q{', '}' encloses the string, one doesn't need to quote
apostrophes inside. Something like that would be useful, mostly because
"{}" can be replaced with any pair of delimiters, so one can pick
characters which are not used in the other language (SQL in this example).
The above example could also be written as:
sql_execute q(select * from foo where id='bar';)
The minimal quoting syntax I managed to find in zsh is:
setopt rc_quotes
sql_execute 'select * from foo where id=''bar'';'
That's a bit more readable than id=\'bar\' but still requires quoting.
--
.-. .-. Yes, I am an agent of Satan, but my duties are largely
(_ \ / _) ceremonial.
|
| dave@fly.srk.fer.hr
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: noquote for quotes as in noglob for filename generation?
2009-10-11 14:58 ` Dražen Kačar
@ 2009-10-11 15:05 ` Mikael Magnusson
2009-10-11 15:22 ` Dražen Kačar
0 siblings, 1 reply; 7+ messages in thread
From: Mikael Magnusson @ 2009-10-11 15:05 UTC (permalink / raw)
To: zsh-users
2009/10/11 Dražen Kačar <dave@fly.srk.fer.hr>:
> Sebastian Stark wrote:
>> Am 10.10.2009 um 11:33 schrieb Nazri Ramliy:
>>>> The mechanism you want is:
>>>>
>>>> ${(q)variable} # Escaped
>>>> ${(qq)variable} # Single quoted
>>>> ${(qqq)variable} # Double quoted
>>>
>>> Thank you Philippe for the tip (I learned something new). But I don't
>>> think that's what I want here (or maybe I'm just too stupid to see how
>>> I
>>> can make use of it to do what I want :)
>>
>> You can't prevent zsh from removing the quotes if you add them to the
>> command line of your sql script, as far as I know.
>>
>> But using the (q..) expansion flag you can re-add the quotes later.
>
> I think he wants Perl quoting operators:
>
>
> Customary Generic Meaning Interpolates
> '' q{} Literal no
> "" qq{} Literal yes
> `` qx{} Command yes*
> qw{} Word list no
> // m{} Pattern match yes*
> qr{} Pattern yes*
> s{}{} Substitution yes*
> tr{}{} Transliteration no (but see below)
> <<EOF here-doc yes*
>
> * unless the delimiter is ''.
>
> So one could write "ordinary" quoting characters for other programming
> languages, without quoting those characters in the zsh script source. That
> would look like this:
>
> sql_execute q{select * from foo where id='bar';}
>
> Since the pair 'q{', '}' encloses the string, one doesn't need to quote
> apostrophes inside. Something like that would be useful, mostly because
> "{}" can be replaced with any pair of delimiters, so one can pick
> characters which are not used in the other language (SQL in this example).
> The above example could also be written as:
>
> sql_execute q(select * from foo where id='bar';)
>
> The minimal quoting syntax I managed to find in zsh is:
>
> setopt rc_quotes
> sql_execute 'select * from foo where id=''bar'';'
>
> That's a bit more readable than id=\'bar\' but still requires quoting.
Or you can just use double quotes instead.
sql "select * from foo where id = 'bar';"
or you can make sql read a line of input and do
sql
select * from foo where id = 'bar';
but of course, the whole point of the exercise was to have 'select' be an alias,
and maybe
select "* from foo where id = 'bar';"
looks weird?
I think an accept-line widget is really the only thing that could
work, but of course it will only work in simple commands, ie not for
things like echo $(for i in 1 2; do select foo) or something. (Unless
you reimplement all of zsh's parsing in the widget (not recommended)).
--
Mikael Magnusson
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: noquote for quotes as in noglob for filename generation?
2009-10-11 15:05 ` Mikael Magnusson
@ 2009-10-11 15:22 ` Dražen Kačar
0 siblings, 0 replies; 7+ messages in thread
From: Dražen Kačar @ 2009-10-11 15:22 UTC (permalink / raw)
To: zsh-users
Mikael Magnusson wrote:
> 2009/10/11 Dražen Kačar <dave@fly.srk.fer.hr>:
> > The minimal quoting syntax I managed to find in zsh is:
> >
> > setopt rc_quotes
> > sql_execute 'select * from foo where id=''bar'';'
> >
> > That's a bit more readable than id=\'bar\' but still requires quoting.
>
> Or you can just use double quotes instead.
> sql "select * from foo where id = 'bar';"
That's a problem with some databases. Oracle, for example, has a bunch of
views whose names start with "v$" and double quotes would want to expand
that.
--
.-. .-. Yes, I am an agent of Satan, but my duties are largely
(_ \ / _) ceremonial.
|
| dave@fly.srk.fer.hr
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2009-10-11 15:22 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-10-10 3:01 noquote for quotes as in noglob for filename generation? Nazri Ramliy
2009-10-10 3:30 ` Philippe Troin
[not found] ` <544dda350910100053s499c0827yd54408b7f82b696d@mail.gmail.com>
2009-10-10 9:33 ` Nazri Ramliy
2009-10-11 11:07 ` Sebastian Stark
2009-10-11 14:58 ` Dražen Kačar
2009-10-11 15:05 ` Mikael Magnusson
2009-10-11 15:22 ` Dražen Kačar
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).