Executing q in shell script

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! 

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?

Hi, try this

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

works in bash

…missing ’ just before last `
sorry

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!!!:)

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.bukowinski> <br>wrote: <br>&gt; Hi, try this <br>&gt; <br>&gt; 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.





</p.bukowinski>