[ncl-talk] Passing a variable value from NCL to bash Script
Alan Brammer
abrammer at albany.edu
Wed Mar 1 08:38:46 MST 2017
Think Rick + Dennis answered the reversed question. To pass a value out of
NCL you probably need to write it to a file or some form.
When you call system or systemfunc NCL launches a sub shell, so environment
variables will disappear as soon as the call is finished.
To stick with the bash loop, writing the variables to a text file would be
the simplest solution. Then set the bash variables to the contents of the
text file(s).
asciiwrite("w1", w1 )
asciiwrite("dw1", dw1)
....
....
w1=$(cat w1)
dw1=$(cat dw1)
Currently though, you're appending the ncl script to opti.error.ncl on each
loop. Which doesn't seem right. Also it seems cleaner, given the
pseudocode, to just do the loop within NCL and define the main script as a
function with the w1, dw1 arguments.
On Wed, Mar 1, 2017 at 8:59 AM, Rick Brownrigg <brownrig at ucar.edu> wrote:
> Hi,
>
> If I understand correctly, you can pass in the values on the command-line
> invocation of NCL as:
>
> ncl w1=X dw1=Y
>
> where X and Y are the actual values. To pass in string arguments, we tell
> people to place quotes around the whole var=value pair, like:
>
> ncl 'myStringVar="foo"'
>
> You can find more info about passing in arguments at:
>
> http://ncl.ucar.edu/Document/Manuals/Ref_Manual/NclCLO.shtml
>
> Hope that helps...
> Rick
>
> On Wed, Mar 1, 2017 at 12:20 AM, Abheera Hazra <hazra.abheera at gmail.com>
> wrote:
>
>> Hi,
>>
>> I am using NCL to calculate a few terms for an optimization algorithm and
>> I am calling the NCL function from within a 'while loop' in a bash script.
>> The 'while loop' runs till a certain condition is met and for the condition
>> to be met, the bash script needs to accept a new value for an existing
>> variable from the NCL script. I have tried using system, systemfunc with
>> even the export command but nothing seems to work. The code looks as
>> follows:
>>
>> ------------------------------------------
>> #!/bin/bash
>>
>> w1=0.4
>> dw1=0.1
>> inc=0.001
>>
>> while awk 'BEGIN { if ('$dw1'<'$inc') {exit 1}}'; do
>>
>> cat <<'EOF' >> opti.error.ncl
>>
>> ;;;;Perform certain computations on w1 and update the w1 and dw1
>> w1=nw1
>> dw1=ndw1
>>
>> system("w1=" + w1)
>> system("dw1=" + dw1)
>>
>> ;;;;w1 and dw1 need to be passed to the bash script
>>
>> EOF
>>
>>
>> ncl k1=${w1} dw11=${dw1} opti.error.ncl
>>
>> echo $w1
>> echo $dw1
>>
>> done
>> ------------------------------------------
>>
>> But this does not update w1 and dw1 values. Any input would be greatly
>> appreciated.
>>
>> Thanks,
>> Abheera
>>
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk at ucar.edu
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>>
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20170301/f1e2926b/attachment.html
More information about the ncl-talk
mailing list