[ncl-talk] returning value to the shell

Rick Brownrigg brownrig at ucar.edu
Mon Oct 17 20:18:12 MDT 2022


HI Jayant,

I don't think that is going to work. There's a hierarchy of system
processes going on there:  your outer shell process, which invokes ncl as a
process, which in turn invokes an "inner" shell process with the system()
command. The environment variable gets set in that inner process, which
promptly terminates. There's no way to pass environment settings back up
the hierarchy that I am aware of.

I'm not a bash wizard, but what comes to mind is to write a file from ncl,
and have your outer shell read that. So for example, print() the values you
need and invoke ncl as:

  ncl -nQ myscript.ncl >myvars.txt.    (the -n keeps ncl from enumerating
its output lines, and the -Q keeps it from printing the copyright stuff)

Then do something in your bash script similar to that described with this
link:

    https://www.cyberciti.biz/faq/unix-howto-read-line-by-line-from-file/

I hope that helps...
Rick




On Mon, Oct 17, 2022 at 6:40 PM Jayant via ncl-talk <
ncl-talk at mailman.ucar.edu> wrote:

> Hi!
> I have a shell script to start with where some variables (year,
> month,tstep,variable name) are defined and passed to a ncl script. Next,
> the ncl script performs further analysis (reading variables, analyzing).
> The dimensionality (2-d or 3-d or 4-d) of the variable is determined inside
> the ncl script, but I need this information back in the shell script. Is it
> possible to get it? Roughly, I tried this
> ----------------------
> myscript.ncl
> yr=toint(getenv("YEAR"))
> mn=toint(getenv("MON"))
> varin=getenv("VNAME")
> ...
> finp=addfile("modelout_"+yr+"_"+mn+".bin","r")
> vinp=finp->varin
> ...
> vdimin=dimsizes(varin)
>
> *system("export VDIMS="+vdimin)  ; is it the correct way?*
> ...
> ----------------------
> run_ncl.sh
> #!/bin/sh
> export YEAR=2019
> export MON=8
> export TSTEP=6
> export VNAME="PREC"
>
> ncl myscript.ncl
>
> *echo $VDIMS    ; returning empty ???*
> -----------------------------
>
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at mailman.ucar.edu
> List instructions, subscriber options, unsubscribe:
> https://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20221017/9d346421/attachment.htm>


More information about the ncl-talk mailing list