2

This is a gnuplot scripting question on unix like systems.
For shell executable gnuplot scripts starting like:-

#!/opt/local/bin/gnuplot

how do you switch to the gnuplot prompt in the starting terminal session, at the end of the script?

Adding load "/dev/stdin" at the end, switches the input, but gives no user prompt.

I would like to let the user replot their own data over the setup and background generated by the script, and/or enter other gnuplot commands. I am looking for an elegant solution within gnuplot. When using the #!/opt/local/bin/gnuplot -c in a gnuplot scriptfile (after a chmod +x), I would like ./script.gp to work the same way as call "script.gp" from gnuplot does. This is so we could subsequently replot "info.dat" at a gnuplot prompt in each case. I want to switch gnuplot from batch mode to interactive at the end of the script (probably like in the way a startup file would). I can't remember or find the command/trick for this (load "/dev/stdin" is close).

The plot window in this case is AquaTerm, gnuplot 5.0 patchlevel 3 (macports), and the terminal session is OS X "Terminal". --persist seems unhelpful in changing the experience.

2

1 Answer 1

2

You want to send a load of plot commands from a file to gnuplot, then send a load of commands from your user's terminal, which suggests something like this:

{ cat plot.gp; while read cmd; do echo "$cmd"; done; } | gnuplot

Or if I flesh that out a bit:

{ cat plot.gp; while :; do >&2 echo -n "gnuplot> "; read -re c; [ "$c" == "quit" ] && break; echo "$c"; done; } | gnuplot

I am using this plot.gp

set xrange [-5:5]
plot sin(x),cos(x),x*x

That basic functionality can be spruced up quite a lot, if you feel fancy:

#!/bin/bash
gpfile=$1

{
   # Show user gnuplot version - on stderr because stdout is going to gnuplot
   gnuplot -e "show version" >&2

   # Strip shebang (but not comments) from plot commands and send to gnuplot
   grep -v "^#!" "$gpfile" 

   # Show plot commands from file to user - on stderr and prefixed with pseudo-prompt
   grep -v "^#!" "$gpfile" | sed 's/^/gnuplot> /' >&2

   # Read user input and forward onto gnuplot
   while :; do
      >&2 echo -n "gnuplot> "
      read -re c
      [ "$c" == "quit" ] && break
      echo "$c"
   done
} | gnuplot

enter image description here

You would save the above in a file called plotandinteract in your HOME directory, then make it executable (just once) with:

chmod +x $HOME/plotandinteract

Then you can run it with:

$HOME/plotandinteract SomePlotFile

There's probably a much more elegant solution with Tcl/expect but I can't work that out - looks like I need @GlennJackman again :-)

Sign up to request clarification or add additional context in comments.

2 Comments

I am impressed! However, I am looking for a solution within gnuplot which might work with shebang on unix and guplot>call "script.gp" on other systems.
The enhanced last version of my script works with scripts containg a shebang.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.