How to execute commands in gnuplot using shell script?
Solution 1
From man gnuplot
or its online manpage:
-p, --persist lets plot windows survive after main gnuplot program
exits.
-e "command list" executes the requested commands before loading the
next input file.
So what you probably want to run is the following command:
gnuplot -e "plot sin(x); pause -1"
Other variants I proposed but which are not that useful were:
gnuplot -p -e "plot sin(x); pause -1"
gnuplot -e "plot sin(x)"
gnuplot -p -e "plot sin(x)"
Solution 2
One way is with -persist
:
#!/usr/bin/gnuplot -persist
set title "Walt pedometer" font ",14" textcolor rgbcolor "royalblue"
set timefmt "%y/%m/%d"
set xdata time
set pointsize 1
set terminal wxt enhanced title "Walt's steps " persist raise
plot "/home/walt/var/Pedometer" using 1:2 with linespoints
another way, if you need to preprocess data, is with a Bash Here Document
(see man bash
):
#!/bin/bash
minval=0 # the result of some (omitted) calculation
maxval=4219 # ditto
gnuplot -persist <<-EOFMarker
set title "Walt pedometer" font ",14" textcolor rgbcolor "royalblue"
set timefmt "%y/%m/%d"
set yrange $minval:$maxval
set xdata time
set pointsize 1
set terminal wxt enhanced title "Walt's steps " persist raise
plot "/home/walt/var/Pedometer" using 1:2 with linespoints
EOFMarker
# rest of script, after gnuplot exits
Solution 3
As explained in the man
pages, gnuplot
expects input from a command file in what is called an batch session. You can e.g. write the line plot sin(x)
to a file myplot
and then execute gnuplot myplot
.
If you omit the command file, as your script does, you will get an interactive session.
Solution 4
This might help
{#set terminal postfile
{#set output "d1_plot.ps"
set title "Energy vs. Time for Sample Data"
set xlabel "Time"
set ylabel "Energy"
plot "d1.dat" with lines
pause -1 "Hit Enter to continue"
Related videos on Youtube
Mihir Gadgil
Updated on September 18, 2022Comments
-
Mihir Gadgil over 1 year
What I want to do is write a script which first launches a program and then tells it to execute a bunch of commands and then quit. Lets go with an example.
I wrote this scriptmyscript.sh
and it doesn't work the way I want it to. What it does is just run gnuplot and wait for it to quit and then runs the other commands; which obviously produces errors.#!/bin/bash gnuplot plot sin(x) pause -1 quit
I guess it is clear what I'm trying to do; if not, then let me know in the comments.
-
Mihir Gadgil over 8 yearsOkay, I get it that its jumping into an interactive session, but is there no way to feed commands into that interactive session through the same sctipt? Also, could you give a more general answer (not specific to gnuplot)? Thanks!
-
Jos over 8 yearsNo, not all applications process input the same way. There is no way to generalize that.
-
Wilf over 8 years@MihirGadgil - not all programs work the same way... which other ones do you want to use?
-
Mihir Gadgil over 8 years@Jos Oh, I see, thanks! Wilf I have used linux in the past but not extensively, now trying to learn more. I don't have any such programs in mind; just trying to learn as much as I can from this one problem.
-
Rmano over 8 yearsThis (the "here-doc" shell method) is probably the generic answer the OP looked for. It will work for a lot of command-driven programs (and if not, you can escalate to
expect
... -
Mihir Gadgil over 8 yearsThe first 2 produce exactly the desired output. Although
-p
is not of much use in this example; if you press enter in the terminal, gnuplot exits and the plot window becomes completely non interactive, except for the quit command. Output of 3rd just comes and goes (not visible at all). The last one produces output, but since gnuplot closes immediately, the plot window is again non interactive (also, it shows a tiny 1square cm plot). Sopause -1
is necessary. -
Byte Commander over 8 years@MihirGadgil Thanks for the feedback. Edited my answer.
-
Hastur almost 8 yearsMoreover you can make executable the scripts with
chmod u+x myscript.gnu
and execute directly with./myscript.gnu
Just a note you forget the[]
in the yrange:set yrange [$minval:$maxval]
. -
Melebius about 6 yearsTo make your answer complete, could you add an example how to use the mentioned here-doc method?