corvi (7) [Avatar] Offline
#1
I want to run gnuplot using a bash script.
I summarize my problem in the following simple situation:
"test.bash", reported below, is my bash-script:
----------------------------------------------------------------
#!/bin/bash
fileout=$1
gnuplot << EOF
fileinp='mydata.dat'
set terminal postscript
set output "${fileout}.ps"
plot fileinp u 1smilie$3<5 ? $2 : 1/0)
EOF
-----------------------------------------------------
file "mydata.dat" read by gnuplot is simply this:
1 3 4
2 6 4
3 99 5
4 12 4
5 15 4
6 99 5
7 21 4
8 24 4
9 99 5
10 30 4

I exec the bash-script typing: "./test.bash pippo
(pippo.ps being the assigned output filename..)

The problem is that gnuplot reads $2 and $3 as (unexisting) second and third
bash script input values instead of column values of "fileinp" and obviously
crashes with the error:
------------------------------------------------------------
gnuplot> plot 'mydata.dat' u 1smilie<5 : 1/0)
^
line 0: invalid expression
------------------------------------------------------------
This is because (I presume..) the character "$" assumes different meaning in
bash-script and in gnuplot syntax.
Is there any way to overcome this problem?
philipp.janert (286) [Avatar] Offline
#2
Re: running gnuplot inside a bash script
Many thanks for providing this "streamlined" version of your problem. This makes it SOO much
easier for me!

You are on the right track, but not quite. The problem is not that $ has different meanings to
gnuplot and the shell. The problem is that the shell interprets all $ BEFORE gnuplot even runs.
In the current situation, the shell interprets all expressions beginning with a $ as shell variables
and tries to fill in the value of these variables. If the variable is not defined, it fills in an empty
string. This means that the column directives end up missing in your plot command.

The key is to PREVENT the shell from expanding those $ that need to be passed to gnuplot.
You do this by preceding them with a backslash. If you modify the plot command as follows:
plot fileinp u 1smilie$3<5 ? $2 : 1/0)
then things work as desired.
corvi (7) [Avatar] Offline
#3
Re: running gnuplot inside a bash script
many thanks for your clear answer: I did, and it perfectly works also in my more complicate original script!
480471 (1) [Avatar] Offline
#4
The problem is that your $ expand. you can make them stop expanding, by putting single quotes around your first EOF. Its a bash trick. Look in the here docs sections of bash to see it. I use it all the time. It stops all expansions. So you can write anything in there (anything besides the thing in your single quotes, which is EOF, which marks the end)
#!/bin/bash 
fileout=$1 
gnuplot << 'EOF'
fileinp='mydata.dat' 
set terminal postscript 
set output "${fileout}.ps" 
plot fileinp u 1$3<5 ? $2 : 1/0) 
EOF 


Examples with none-gnu plot

$ cat > test1 << EOF
asdf
asdffdas $1 $0 asdf asdfasdfasdfasdf
asdfasdf
EOF


$ cat > test2 << 'EOF'
234asdf
asdf23 $0 $3
asdf234
EOF


$ cat test1
asdf
asdffdas  -bash asdf asdfasdfasdfasdf
asdfasdf


$ cat test2
234asdf
asdf23 $0 $3
asdf234