[gradsusr] [EXTERNAL] Re: shell command within grads script

Adams, Jennifer M. (GSFC-610.2)[ADNET SYSTEMS INC] jennifer.m.adams at nasa.gov
Tue Oct 20 08:19:36 EDT 2020


Newer builds of GrADS also have an intrinsic function in the scripting language that will issue a shell command and capture the output.

sys (command)
This function was added in version 2.1.1.b0. It submits the specified command to the shell and returns the resulting text stream that gets sent to standard output (stdout). The command is passed to /bin/sh. There is no way to check if the command succeeded; the return code is not captured, and an empty result could mean that the command has no output or that the command failed. Any error notifications or text streams sent to standard error (stderr) will appear in the console window and not the returned text unless you explicitly include a capture of stderr in your command string.
Consider the following script test_sys.gs:
cmd1="ls ./foo"
cmd2="ls ./foo 2>&1"
cmd3='echo Hello, world!'
res1=sys(cmd1); say 'cmd1 returned ->'res1'<-'
res2=sys(cmd2); say 'cmd2 returned ->'res2'<-'
res3=sys(cmd3); say 'cmd3 returned ->'res3'<-'

The session in the GrADS command window when running test_sys.gs looks like this:
ga-> test_sys
ls: ./foo: No such file or directory
cmd1 returned -><-
cmd2 returned ->ls: ./foo: No such file or directory
<-
cmd3 returned ->Hello, world!
<-
ga->





--
Jennifer Miletta Adams
ADNET Systems, Inc.
NASA/GSFC, Code 610.2
Building 32, Room S159
(301) 614-6070


From: gradsusr <gradsusr-bounces at gradsusr.org> on behalf of Davide Sachetti <davide.sacchetti at arpal.liguria.it>
Reply-To: GrADS Users Forum <gradsusr at gradsusr.org>
Date: Tuesday, October 20, 2020 at 3:36 AM
To: "gradsusr at gradsusr.org" <gradsusr at gradsusr.org>
Subject: [EXTERNAL] Re: [gradsusr] shell command within grads script

the simplest way is to redirect shell output to a file (!date >/tmp/x),
then read it (http://cola.gmu.edu/grads/gadoc/script.html<https://urldefense.proofpoint.com/v2/url?u=http-3A__cola.gmu.edu_grads_gadoc_script.html&d=DwMFaQ&c=ApwzowJNAKKw3xye91w7BE1XMRKi2LN9kiMk5Csz9Zk&r=M2mFZsxo0ZoztoK2Lyoypu1kKkBVKyEoM9CXGWO42zk&m=YdeK14LAwOILDorq-HRrVxRz6zIYU2uymXt97Jz3S1E&s=nGpp4SbYO3yOtlYygMHXxVoR3mGl1YL6dmR89kgE8fw&e=>)

in opengrads there is at least one extension (I wrote one) that gets
the output of external commands, but if you don't know about extensions
the first solution may be faster to implement

Bye bye
Davide

On Mon, 2020-10-19 at 17:26 -0400, Muthuvel Chelliah - NOAA Federal
wrote:
> Semyon, Jeff,
>
> Thanks for the replies.
>
> a) I know that the date command with the options I had asked, will
> give the
> result at the grads command line with a ! before that command.
>
> b) I also know the second round about way my manipulating the output
> from the q time output (but how to do few days ago?).
>
> I was only curious if there was a way from a) above, of possibly
> storing
> that output in a variable as part of a grads script variable itself.
> I guess, it is difficult, and not doable!!
>
> Thanks again
> Muthu
>
> ------------------------------------------------
> Dr. Muthuvel Chelliah, eMail: Muthuvel.Chelliah at noaa.gov<mailto:Muthuvel.Chelliah at noaa.gov>
> Rm. 3008, Climate Prediction Center, Ph: (301) 683-3393
> NCEP/NWS/NOAA/U.S.Dept.of Commerce,
> 5830, University Research Court, NCWCP,
> College Park, MD 20740
>
>
> On Mon, Oct 19, 2020 at 4:54 PM Jeff Duda <jeffduda319 at gmail.com<mailto:jeffduda319 at gmail.com>>
> wrote:
> > You can try prompting the linux command with "!" and hopefully the
> > script recognizes the result and stores it properly.
> >
> > I see that you're attempting to do something that would require
> > lots of code in a grads script, but hopefully you know that you can
> > query the valid time of data using 'q time' and parse the data
> > using the 'sublin()', 'subwrd()', and 'substr()' functions internal
> > to GrADS' scripting language.
> >
> > Jeff Duda
> >
> > On Mon, Oct 19, 2020 at 1:56 PM Muthuvel Chelliah - NOAA Federal <
> > muthuvel.chelliah at noaa.gov<mailto:muthuvel.chelliah at noaa.gov>> wrote:
> > > Hello folks,
> > >
> > > How can I set the result of the following linux shell command
> > > to a variable (time_var) within a Cola grads script? which can be
> > > assigned
> > > to set time command, via 'set time 'time_var
> > >
> > > date -d "3 days ago" +%d%b%Y
> > >
> > > Thanks
> > > Muthu
> > > ------------------------------------------------
> > > Dr. Muthuvel Chelliah, eMail: Muthuvel.Chelliah at noaa.gov<mailto:Muthuvel.Chelliah at noaa.gov>
> > > Rm. 3008, Climate Prediction Center, Ph: (301) 683-3393
> > > NCEP/NWS/NOAA/U.S.Dept.of Commerce,
> > > 5830, University Research Court, NCWCP,
> > > College Park, MD 20740
> > > _______________________________________________
> > > gradsusr mailing list
> > > gradsusr at gradsusr.org<mailto:gradsusr at gradsusr.org>
> > > http://gradsusr.org/mailman/listinfo/gradsusr<https://urldefense.proofpoint.com/v2/url?u=http-3A__gradsusr.org_mailman_listinfo_gradsusr&d=DwMFaQ&c=ApwzowJNAKKw3xye91w7BE1XMRKi2LN9kiMk5Csz9Zk&r=M2mFZsxo0ZoztoK2Lyoypu1kKkBVKyEoM9CXGWO42zk&m=YdeK14LAwOILDorq-HRrVxRz6zIYU2uymXt97Jz3S1E&s=nAiCz20baQhtExK-bbkfiFgLOkGnAqo2LwZQAS1Kpt8&e=>
> >
> >
> > _______________________________________________
> > gradsusr mailing list
> > gradsusr at gradsusr.org<mailto:gradsusr at gradsusr.org>
> > http://gradsusr.org/mailman/listinfo/gradsusr<https://urldefense.proofpoint.com/v2/url?u=http-3A__gradsusr.org_mailman_listinfo_gradsusr&d=DwMFaQ&c=ApwzowJNAKKw3xye91w7BE1XMRKi2LN9kiMk5Csz9Zk&r=M2mFZsxo0ZoztoK2Lyoypu1kKkBVKyEoM9CXGWO42zk&m=YdeK14LAwOILDorq-HRrVxRz6zIYU2uymXt97Jz3S1E&s=nAiCz20baQhtExK-bbkfiFgLOkGnAqo2LwZQAS1Kpt8&e=>



Attenzione ARPAL ha cambiato indirizzi digitali, i nuovi indirizzi di e-mail sono composti dall'attuale nome.cognome seguito da @arpal.liguria.it, quindi nome.cognome at arpal.liguria.it

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://gradsusr.org/pipermail/gradsusr/attachments/20201020/ad8d7d57/attachment-0001.html>


More information about the gradsusr mailing list