Executing q in shell script

781 views
Skip to first unread message

Anton Bondar

unread,
Aug 14, 2013, 8:26:24 PM8/14/13
to personal...@googlegroups.com
Hi,

Lets say I need to load some dictionary, give it a key and assign returned value to an environment variable inside a shell script. How would one do it?

Here is a simple example where I have marketconfig dictionary hardcoded inside marketconfig.q:

PARAM=` q /home/.../marketconfig.q ; show marketconfig[`US] `

The problem is in executing the second part... In a normal form it would be:

q) \l /home/.../marketconfig.q
q) show marketconfig[`US]
q) \\

but I need to assign the value to an environment variable. Also, there should be no changes in the file

Thanks! 

Rory O'Rorke

unread,
Aug 14, 2013, 10:05:09 PM8/14/13
to personal...@googlegroups.com
Hi Anton

You can add setenv (http://code.kx.com/wiki/Reference/setenv) to the q script you load to apply the value you return to an env variable in your shell.
Thanks Rory 


--
You received this message because you are subscribed to the Google Groups "Kdb+ Personal Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to personal-kdbpl...@googlegroups.com.
To post to this group, send email to personal...@googlegroups.com.
Visit this group at http://groups.google.com/group/personal-kdbplus.
For more options, visit https://groups.google.com/groups/opt_out.

Patryk Bukowinski

unread,
Aug 15, 2013, 6:32:37 AM8/15/13
to personal...@googlegroups.com

Hi, try this

R=`q <<< 'system "l dir/marketconfig.q"; show marketconfig[\`US]`

works in bash

Patryk Bukowinski

unread,
Aug 15, 2013, 6:35:14 AM8/15/13
to personal...@googlegroups.com

...missing ' just before last `
sorry

Anton Bondar

unread,
Aug 15, 2013, 9:55:37 PM8/15/13
to personal...@googlegroups.com
Thx Rory but my problem was in executing consecutive q commands from within shell script.


P.Buko thats exactly what I needed! Thanks a lot for the help!!!!!!:)

Aaron Davies

unread,
Aug 28, 2013, 12:29:24 PM8/28/13
to personal...@googlegroups.com
On Aug 15, 2013, at 6:35 AM, Patryk Bukowinski wrote:

> ...missing ' just before last `
> sorry
>
> On Aug 15, 2013 11:32 AM, "Patryk Bukowinski" <p.buko...@gmail.com> wrote:
> Hi, try this
>
> R=`q <<< 'system "l dir/marketconfig.q"; show marketconfig[\`US]`
>
> works in bash

the escaping gets simpler if you use the newer substitution syntax

R=$(q<<<'system"l dir/marketconfig.q";marketconfig`US')

for more complex examples, something like this might be better:

R=$(cat<<-EOF|q
\\l dir/marketconfig.q
marketconfig\`US
EOF
)

note that something like this is also possible:

m=US
R=$(cat<<-EOF|q
\\l dir/marketconfig.q
marketconfig\`$m
EOF
)

finally, note that the details of things like this vary widely between shells, so test carefully with your specific shell -- i tested the last two above in zsh, not sure if they work in bash, ksh, etc.

Reply all
Reply to author
Forward
0 new messages