Here is my original script named ``plot_all.sh''
------------------------------------------------------------------------
#!/bin/bash
usage="\tVersion: 0.1
\tUsage: ./plot_all.sh file.plt [terminal]
\tfile.plt = file contains data to be plotted
\tterminal = the output type; if ignored, 'x11' is set
\t\tavailable values are: png, eps, pdf"
if [ $# -lt 1 ]; then
echo -e "$usage"
exit 0
fi
filename=`basename $1 .plt`
ext=$2
options=""
outputs="set output \"$filename.$ext\""
case "$ext" in
"png")
terminal="png"
options='font "/usr/share/fonts/truetype/freefont/FreeSans.ttf"';;
"eps" | "pdf")
terminal="post eps color"
ext="eps"
outputs="set output \"$filename.$ext\"";;
"svg")
terminal="svg";;
*)
terminal="x11"
outputs="";;
esac
gnuplot -persist << EOF
set term $terminal enhanced $options
$outputs
set xtics nomirror
set ytics nomirror
set title "Acceleration record [$filename]"
set xlabel "time (sec.)"
set ylabel "accleration (m/s^2)"
set yrange[-4:4]
plot "$filename.plt" u 1:2 t "a_x" w lines lt 1,\
"" u 1:3 t "a_y" w lines lt 2,\
"" u 1:4 t "a_z" w lines lt 3
EOF
if [ "$2" == "pdf" ]; then
epstopdf $filename.$ext
echo -e "The $filename.eps has been created\nand then be converted
to $filename.pdf\nby using epstopdf.\n"
fi
if [ "$terminal" == "x11" ]; then
echo -e "The terminal has been set as x11\nand no plot has been
outputed.\n"
fi
------------------------------------------------------------------------------------
And what I am trying is to make the part of gnuplot settings (that is,
lines between `gnuplot -persist << EOF' and `EOF' of the previous
script) ``replaceable.'' For example, I probably have several
different gnuplot settings for different data sets, and these settings
are saved as plain text files individually. If my script (plot_all.sh)
can read the different settings into itself to ``replace'' the
original setting part and execute these setting as they have been
written in the script, then I can use the same script repeatedly.
Is it possible for the shell script to perform this kind of work?
Because I am very new in writing shell script, it's likely I have
thought the whole problem in the wrong way. I have tried to find
commands in books but get no clear idea yet, so I post my question
here and hope someone could give me some advices. Thank you in advance.
> Hi, I am writing a shell script to work with gnuplot. So far, it works
> with some basic functions I expected, but I want to modified it to be
> more ``reusable.''
By the way - I have had great experience with the 2D plot program
called grace. (formerly Xmgr and Ace/gr)
I usually use the following
1) Create a data file
2) Run grace on that file. It displayes the graph on the screen.
3) Interactively add labels, axis, comments, titles, legends, color,
line style, plot style, etc. Grace is extremely flexible here, and
since it's interative, you see the results immediately..
4) Save the parameter file
5) generate one or more new data files
6) for each data file, run grace with the new data file, and the old
parameter file.
7) tweak the plot if necessary. Output each one in EPS, GIF, etc. format.
http://plasma-gate.weizmann.ac.il/Grace
The UI is a little clunky, but the flexibility is great. It's also scriptable.
Thank you for this useful information. The functions of Grace looks
great and I will give it a try.
(And of course, I still want to know whether there is solution for my
initial script problem.)
SettingsFile=<you set it to some default, cmd line arg, or ???>
gnuplot -persist << EOF
$(cat $SettingFile)
EOF
Hi, Jon LabBadie,
Thanks for your reply. I have tried your method but still have some
problems. I move all the settings for gnuplot to the file named
``plot_all.gp.'' Then did the following coding in my script:
SettingFile=plot_all.gp
gnuplot -persist << EOF
$(cat $SettingFile)
EOF
The content of SettingsFile has been loaded into the shell script, but
all the variables (e.g., $terminal, $option, ...) were treated as
plain text without corresponding values loaded. Therefore, when I
execute the script, the gnuplot complained with error messages like:
gnuplot> set term $terminal enhanced $options
^
line 0: unknown or ambiguous terminal type; type just 'set
terminal' for a list
I have tried quotes around these variables, but they didn't help. How,
or is it possible, to make the variables written in ``plot_all.gp'' be
replaced by the assigned values?
Untried, but how about something like the following variations?
cat "$SettingFile" \
| sed \
-e "s/\$terminal/$terminal/g" \
-e "s/\$options/$options/g" \
| gnuplot -persist
or:
gnuplot -persist <<EOF
$(sed -e "s/\$terminal/$terminal/g" -e "s/\$options/$options/g" <
"$SettingsFile")
EOF
or (not sure about syntax):
gnuplot -persist <(sed -e "s/\$terminal/$terminal/g" -e "s/\$options/
$options/g" < "$SettingsFile")
Ugly, someone else will probably have a better solution, but try:
gnuplot -persist << EOF
$(bash -c 'x=$(cat settings) ; eval echo "\"$x\""')
EOF
Yes, your suggestion works! Thank you.
I used the 2nd variation of your suggestions and added every single
variable for the sed to substitute. Very straightforward approach, but
I didn't know it could be done by doing this.
Thanks for your approach, but I have little trouble when I was try to
understand it. (Actually, I don't understand the `echo' part.)
I am not sure whether I understand the meaning of your suggestion. The
``settings'' means my own settings filename, am I right? I rewrite it
as the follows.
$(bash -c 'x=$(cat $settingsFile); eval echo "\"$x\""')
And then after executing the shell script, it outputted no gnuplot's
result and seemed to trapped in the gnuplot part. (I have to Ctrl+C to
terminate the script.)
My bad. Yes I did mean the name of your settings file.
It could be hard coded or supplied as a variable.
There is a capitalization difference in your line above (settingsFile)
compared to your earlier script (SettingsFile). That could cause
cat to get no argument and sit there waiting for input. I considered
a safetynet of ${SettingsFile:?No Filename}. It should print an error
and exit if there is no value in the variable.
Sorry to confuse you. I forgot to mention that I've always used
``settingsFile'' as the variable's name in my script.
The capitalization couldn't be the cause, or I can't apply Jim's
method successfully. Therefore, maybe some parts else make the script
just sit there and output nothing?
Just for you who might want to test or need to learn how to combine
gnuplot with shell script as me.
All these files have been uploaded to Google Docs, which I have almost
no experience. If you cannot view the files or have problems in
testing, let me know please.