[ncl-talk] Bash variables not being read via system() function
Dave Allured - NOAA Affiliate
dave.allured at noaa.gov
Sat Oct 8 07:31:38 MDT 2022
Tabish, I do not know what is wrong with the system() command the way you
wrote it. However, I suggest a simpler approach. It is easy to directly
pass simple string command line arguments to a shell command through
system(). Do not use asciiwrite or text files.
cd_string = tostring (cd)
emis_string = str_join (tostring (emis), " ")
system ("./myscript.sh " + cd_string + " " + emis_string)
Take care to include intervening spaces between all arguments to the shell
command, as shown here. You could also write "cd" directly, because NCL
automatically converts scalar numbers to strings when doing string
concatenation:
emis_string = str_join (tostring (emis), " ")
system ("./myscript.sh " + cd + " " + emis_string)
On Sat, Oct 8, 2022 at 6:34 AM Tabish Ansari via ncl-talk <
ncl-talk at mailman.ucar.edu> wrote:
> Hello,
>
> I have a small shell script *myscript.sh* which takes 13 arguments: start
> day, and 12 values of emission levels. For now, it just echoes them back on
> the screen.
>
> I'm supplying these 13 numbers via an NCL script by writing them out to a
> text file and reading them again in the shell. NCL is nicely writing these
> text files out and myscript.sh is able to pick these arguments when I write
> the command in bash. However, when I try to achieve the same from inside
> NCL using the system() function, the shell script isn't able to read these
> arguments.
>
> Here's *myscript.sh:*
> #!/bin/bash
> echo day=$1
> echo Ai=$2
> echo Ap=$3
> echo At=$4
> echo Ar=$5
> echo Bi=$6
> echo Bp=$7
> echo Bt=$8
> echo Br=$9
> echo Ci=${10}
> echo Cp=${11}
> echo Ct=${12}
> echo Cr=${13}
>
> And here's my NCL script:
> begin
>
> SD = 02 ; start day
> baseline = (/100,100,100,100,100,100,100,100,100,100,100,100/)
>
> emis = baseline
> print("PREPARING EMISSIONS: "+emis(0)+" "+emis(1)+" "+emis(2)+"
> "+emis(3)+" "+emis(4)+" "+emis(5)+" "+emis(6)+" "+emis(7)+" "+emis(8)+"
> "+emis(9)+" "+emis(10)+" "+emis(11))
>
> asciiwrite("emisstring.txt",emis)
> asciiwrite("SD.txt",SD)
>
> system("./myscript.sh $(<SD.txt) $(<emisstring.txt)")
>
> end
>
> These two scripts are self-sufficient and you can easily copy-paste and
> test them on your system. Please let me know what's wrong here.
>
> Thanks a lot.
>
> Tabish
> -------------------------------
> Dr Tabish Ansari
> Research Associate
> Air Quality Modelling Group
> IASS-Potsdam
> Germany
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20221008/cf142f9e/attachment.htm>
More information about the ncl-talk
mailing list