How do I send .png plot to stdout in gnuplot? - bash

Currently, I do something like the following:
set term png
set output 'file.png'
But hardcoding filename in the script is quite inflexible. Is there some way to tell gnuplot to output image file to stdout, so I will be able to redirect it's output where needed?

If you do want to send your .png to stdout, just don't set the output:
#!/usr/bin/env gnuplot
set term png
plot x
Then run the script
./plot.plt > mypng.png
I think the bash wrapper makes more sense for most purposes, but this is potentially useful as well.

Put your gnuplot script inside a script and use a here document:
#!/bin/bash
gnuplot << EOF
set term png
set output "$1.png"
plot "$1.dat"
EOF
Now invoke the script, say plot.sh, by specifying the prefix to output file and data as argument: bash plot.sh file.

Related

How to display (echo) a large json file in bash?

I have a json file and I know the location of it. let us say C:\desktop\file. I am adding a bash script in an existing application that would read this bash script take the output and use the json that it extracts for something else. How would I go about doing that.
output should be something like
echo Start
echo C:\desktop\file
echo End
The file is also large

How to use base64 encoded image as an argument?

I am trying to use the base64 encoding of an image as a flag when I run my program. Im getting back: Argument list too long
I am on a Ubuntu 16.04 Docker image on a mac.
$ ./myProgram -input "/9j/4AAQSkZJRgABA [...]"
Instead of accepting it as an argument, I would suggest you read it from standard in instead.
$ base64 someImage.jpg | ./myProgram
If this program is a shell script, you can save standard in into a variable with something like this:
#!/bin/sh
MY_BASE64_IMAGE_INPUT=$(cat -)
# do something with that info
echo $MY_BASE64_IMAGE_INPUT
There is a length limit of a single command. This is your program... It should be easier in every way to send the name of the file as the argument to your program which would then read the desired information from the file. You could cat the contents of the file into a variable (if you really want it as a variable). It may be more conventional if you passed the contents of the file to your program via the standard input stream.

Why 'pause -1' gnuplot command not working while executed in a while -r bash loop

I'm trying to make a bash script in combination with gnuplot.
I'm using an input file 'input.list' containing the single-column list of files to be analyzed with gnuplot.
I'm using a following bash script:
#!/bin/bash
while read -r line
do
...
#Other operations on files
...
gnupinp=$line
gnuplot -e "input='${gnupinp}'; plot input u 1:2; pause -1"
done < input.list
There are two issues probably connected:
When first file from the list is analyzed the plot is created but the pause -1 seems to be neglected while pause 1 works fine.
No matter if I use pause -1 or pause 1 the script fails starting from the #Other operations on files part when the second file from the list is executed.
The same behavior can be obtained when I use system 'sleep 1' command in gnuplot.
When I neglect pause command the gnuplot scripts are preformed properly for all listed files.
thanks in advance for any help
You are using the standard input in two competing ways: feeding the data to read, and to gnuplot's pause -1.
If you want the user to be able to interact with gnuplot by pressing return after the graph is plotted, you have to use some other way than stderr to feed your script with data, e.g. read from a file.

Bash shells script 101 - variable definition, pbcopy, etc

I am trying to create a (my first) bash script, but I need a little help. I have the following:
#!/bin/bash
echo "Write a LaTeX equation:"
read -e TeXFormula
URIEncoded = node -p "encodeURIComponent('$(sed "s/'/\\\'/g" <<<"$TeXFormula")')"
curl http://latex.codecogs.com/gif.latex?$URIEncoded -o /Users/casparjespersen/Desktop/notetex.gif | pbcopy
I want it to:
Require user input (LaTeX equation)
URIEncode user input (the top result in Google was using node.js, but anything will do..)
Perform a cURL call to this website converting equation to a GIF image
Copy the image to the placeholder, so I can paste it to a note taking app like OneNote, Word, etc.
My script is malfunctioning in the following:
URIEncoded is undefined, so there is something wrong with my variable definition.
When I copy using pbcopy the encrypted text content of the image is copied, and not the actual image. Is there a workaround for this? Otherwise, the script could automatically open the image and I could manually Cmd + C the content.
URIEncoded is undefined, so there is something wrong with my variable
definition.
The line should read
URIEncoded=$(node -p "encodeURIComponent('$(sed "s/'/\\\'/g" <<<"$TeXFormula")')")
without spaces around the = sign, and using the $() construct to actually perform the command, otherwise, the text of the command would be assigned to the variable.
When I copy using pbcopy the encrypted text content of the
image is copied, and not the actual image. Is there a workaround for
this? Otherwise, the script could automatically open the image and I
could manually Cmd + C the content.
pbcopy takes input from stdin but you are telling curl to write the output to a file rather than stdout. Try simply
curl http://latex.codecogs.com/gif.latex?$URIEncoded | pbcopy
or, for the second option you describe
curl http://latex.codecogs.com/gif.latex?$URIEncoded -o /Users/casparjespersen/Desktop/notetex.gif && open $_

Do an animation in 1 command in gnuplot

I'm trying to show an animation using gnuplot
I got the following script:
plot ”heat1d.txt ” using 1:2 every :::a::a
pause 0.01
a=a+1
if ( a<b ) reread
that I execute using
a = 0
b = 100
load "a.plot"
it works, but is there a way to execute all of this using only 1 command from a shell?
Alternatively is there a way to integrate the variable definitions into the .plot file so that I can simply execute it? I tried different things like echo 'a=0'|gnuplot etc but it doesn't seem to actually define the variable correctly
thanks
You can use a do for loop.
do for [a = 0:100] {
plot ”heat1d.txt ” using 1:2 every :::a::a
pause 0.01
}
The default terminal on linux is usually wxt, and it has the raise option, which will change the focus to the plot window at every iteration. This will make it difficult if not impossible to stop the animation.
I suggest to put noraise as the terminal option. For example, you can put the following line at the beginning of the script:
set term wxt noraise
Now, if you want to stop the animation halfway, press CtrlC on the gnuplot terminal.
You can pass -e as a commandline argument. For example, if you have the script:
#script test.gp
print foo,bar
Then you could run it with gnuplot using:
gnuplot -e "foo=1;bar=2" test.gp
In your case, it looks like you could accomplish nearly what you want by invoking your script as:
gnuplot -e "a=0;b=100" a.plot

Resources