Juhász Péter <peter.juhasz83_gmail.com> said (on 2012/01/31):
> On Tue, 2012-01-31 at 18:18 +0100, plotter_piments.com wrote:
> > I have a number of gnuplot scripts that have various options set. I
> > would like to set a default value in the script and optionally determine
> > the value from the calling script or command.
> >
> > This does not seem to be possible.
> >
> > eg.
> >
> > if (doPNG) { set terminal png; set output pngfile}
> > else { set terminal wxt }
> >
> > plot ....
> >
> > called as :
> >
> > (echo 'doPNG=1;' && cat cos_fit.gnu) | gnuplot
> >
> > Some kind of isdefined() function would be a solution or simply if the
> > interpreter could return some kind of NaN value that could then be
> > tested instead of triggering an error and breaking out.
> >
> > Alternatively kind of error trapping mechanism but that may be going
> > beyond what is needed for this simple test.
> >
> > Do you think this would be a useful feature?
> >
> > Regards,. Peter.
>
> It's such a useful feature that it has been implemented already:
>
> if (exists("doPNG")) { # whatever...
>
> Look at "help exists". You'll see that, well, help exists. (Sorry, I
> couldn't resist...)
>
> There is also "defined", but that's deprecated.
>
> Péter Juhász
For your use case, it might make more sense to use environment
variables. For handling the output terminal, GNUTERM can already be
set to the desired terminal type.
In the more general case...
!#/bin/sh
GP_MYOUTPUT=png
export GP_MYOUTPUT
echo << __GP_EOF__ | gnuplot
print "This part is gnuplot..."
set xrange [-10:10]
set yrange [-10:10]
# etc etc
myoutput=`echo $GP_MYOUTPUT`
# do something with the gnuplot variable, myoutput
print myoutput
plot x, x**2
print "Leaving gnuplot now... bye"
__GP_EOF__
Above is an example of a shell script setting an environment variable,
GP_MYOUTPUT, that is imported into a gnuplot variable (myoutput) via a
system call (`echo $GP_MYOUTPUT`). As a gnuplot string variable, you
can do things like substr(), word(), value(), and so on.
|