回答:
スイッチを介して変数を入力できます -e
$ gnuplot -e "filename='foo.data'" foo.plg
foo.plgでは、その変数を使用できます
$ cat foo.plg 
plot filename
pause -1
「foo.plg」をもう少し汎用的にするには、条件を使用します。
if (!exists("filename")) filename='default.dat'
plot filename
pause -1
これ-eはファイル名の前に置かなければならないことに注意して-eください。そうでなければ、ファイルはステートメントの前に実行されます。特に、CLI引数でシバンgnuplot #!/usr/bin/env gnuplotを実行./foo.plg -e ...すると、提供された引数の使用が無視されます。
if (!exists("filename")代わりに必要ですif ! exists("filename")。
                    -e引数、例えば-e "filename='default.data'; foo='bar'"か-e "filename='default.data'" -e "foo='bar"'。
                    -e ...オプションは、ファイル名の前に持っています。確かにman gnuplot我々は読むことができます:-e「コマンドリストは、」要求されたコマンドを実行し、次の入力ファイルをロードする前に。。
                    フラグを使用して、バージョン5.0以降のgnuplotスクリプトに引数を渡すことができます-c。これらの引数は変数を介してアクセスされるARG0とARG9、ARG0スクリプトであること、およびARG1にARG9文字列変数。引数の数はによって与えられARGCます。
たとえば、次のスクリプト( "script.gp")
#!/usr/local/bin/gnuplot --persist
THIRD=ARG3
print "script name        : ", ARG0
print "first argument     : ", ARG1
print "third argument     : ", THIRD 
print "number of arguments: ", ARGC 
次のように呼び出すことができます:
$ gnuplot -c script.gp one two three four five
script name        : script.gp
first argument     : one
third argument     : three
number of arguments: 5
またはgnuplot内で
gnuplot> call 'script.gp' one two three four five
script name        : script.gp
first argument     : one
third argument     : three
number of arguments: 5
gnuplot 4.6.6以前でcallは、異なる(現在は非推奨)構文を持つメカニズムが存在します。引数を介してアクセスされます$#、$0、...、 $9。たとえば、上記と同じスクリプトは次のようになります。
#!/usr/bin/gnuplot --persist
THIRD="$2"
print "first argument     : ", "$0"
print "second argument    : ", "$1"
print "third argument     : ", THIRD
print "number of arguments: ", "$#"
そして、それはgnuplot内で(バージョン<4.6.6を思い出して)呼び出されます
gnuplot> call 'script4.gp' one two three four five
first argument     : one
second argument    : two
third argument     : three
number of arguments: 5
スクリプト名には変数がないため$0、最初の引数も変数であり、変数は引用符で囲まれていることに注意してください。これをコマンドラインから直接使用する方法はありません。@ con-fu-seによって提案されたトリックを介してのみ使用できます。
-cなどの行を含むスクリプトでスイッチを使用しようとしましたplot 'data' using 0:($1/100)。gnuplotが消えるため、エラー無効な式で死亡します$1。どこが間違っているのですか?なし-cでは、スクリプトは正常に実行されます。
                    gnuplot 5.0これらの例にのような行を追加してテストしましたplot 'data' using 0:($1/100)が、あなたの言うことがわかりませんでした。このバージョンでは変数を定義しているため、まれですARG0- ARG9ではなく$1- $9。-cフラグは以前のバージョンではサポートされていないため、バージョン5.0もあると思います(そうですか?)実際の問題がどこにあるかを確認するには、最小限のスクリプトを確認する必要があります:/
                    ここで提案されているように、環境を通じて情報を渡すこともできます。Ismail Aminの例をここで繰り返します。
シェルで:
export name=plot_data_file
Gnuplotスクリプトで:
#! /usr/bin/gnuplot
name=system("echo $name")
set title name
plot name using ($16 * 8):20 with linespoints notitle
pause -1
              name=plot_data_file ./the_gnuplot_script。
                    name=value commandです。その環境で特定の変数を使用してコマンドを実行するようにシェルに指示します。私はbash 4.3.11を使用していますが、これは非常に一般的なシェル機能だと思います。
                    Jari Laamanenの答えが最良の解決策です。シェル変数で複数の入力パラメーターを使用する方法を説明したいだけです。
output=test1.png
data=foo.data
gnuplot -e "datafile='${data}'; outputname='${output}'" foo.plg
とfoo.plg:
set terminal png
set outputname 
f(x) = sin(x)
plot datafile
ご覧のとおり、より多くのパラメーターがセミコロンで渡されます(bashスクリプトのように)が、文字列変数は ''でカプセル化する必要があります(gnuplot構文、Bash構文ではない)
'かどうかは関係ありません"。-e引数を囲む外側の引用符"は、bash変数を置き換える必要があります。また、以下では正常に動作します:OUTPUT=foobar; gnuplot -e "output=\"$OUTPUT\"; print output"
                    この質問には十分に回答していますが、私が行ったようにこれをググる人の作業負荷を減らすためにさえ、私はここで埋めるべきニッチを見つけることができると思います。vagobertoからの回答により、このバージョンの問題を解決するために必要なものが得られたので、ここで解決策を共有します。
私は最新の環境でプロットスクリプトを開発しました。
#!/usr/bin/gnuplot -c 
set terminal png truecolor transparent crop
set output ARG1
set size 1, 0.2
rrLower = ARG2
rrUpper = ARG3
rrSD = ARG4
resultx = ARG5+0 # Type coercion required for data series
resulty = 0.02 # fixed
# etc.
これは最近のgnuplot(私の場合は5.0.3)の環境でコマンドラインから完全に実行されます。
$ ./plotStuff.gp 'output.png' 2.3 6.7 4.3 7
サーバーにアップロードして実行すると、サーバーのバージョンが4.6.4(Ubuntu 14.04 LTSの現在のバージョン)だったために失敗しました。以下のシムは、元のスクリプトを変更せずにこの問題を解決しました。
#!/bin/bash
# GPlot v<4.6.6 doesn't support direct command line arguments.
#This script backfills the functionality transparently.
SCRIPT="plotStuff.gp"
ARG1=$1
ARG2=$2
ARG3=$3
ARG4=$4
ARG5=$5
ARG6=$6
gnuplot -e "ARG1='${ARG1}'; ARG2='${ARG2}'; ARG3='${ARG3}'; ARG4='${ARG4}'; ARG5='${ARG5}'; ARG6='${ARG6}'" $SCRIPT
これら2つのスクリプトを組み合わせると、gnuplotのバージョンや基本的には* nixに関係なく、パラメーターをbashスクリプトからgnuplotスクリプトに渡すことができます。
たとえば、次のようにシェルマジックを実行することもできます。
#!/bin/bash
inputfile="${1}" #you could even do some getopt magic here...
################################################################################
## generate a gnuplotscript, strip off bash header
gnuplotscript=$(mktemp /tmp/gnuplot_cmd_$(basename "${0}").XXXXXX.gnuplot)
firstline=$(grep -m 1 -n "^#!/usr/bin/gnuplot" "${0}")
firstline=${firstline%%:*} #remove everything after the colon
sed -e "1,${firstline}d" < "${0}" > "${gnuplotscript}"
################################################################################
## run gnuplot
/usr/bin/gnuplot -e "inputfile=\"${inputfile}\"" "${gnuplotscript}"
status=$?
if [[ ${status} -ne 0 ]] ; then
  echo "ERROR: gnuplot returned with exit status $?"
fi
################################################################################
## cleanup and exit
rm -f "${gnuplotscript}"
exit ${status}
#!/usr/bin/gnuplot
plot inputfile using 1:4 with linespoints
#... or whatever you want
私の実装はもう少し複雑です(たとえば、sed呼び出しでマジックトークンを置き換えていますが、すでにそれを行っています...)が、理解を深めるためにこの例を簡略化しました。また、もっと簡単にすることもできます... YMMV。
シェルで書く
gnuplot -persist -e "plot filename1.dat,filename2.dat"
連続して必要なファイル。-persistは、ユーザーが手動で終了しない限り、gnuplot画面を維持するために使用されます。
gnuplot -persist -c "myscript.plt" "mydata.csv" "myoutput.png"、このコマンドは正常に機能し、期待どおりにファイル「myoutput.png」を取得しますが、gnuplot画面は表示されません(exitmyscript.pltのコマンドはありません)。どうして?そして、私の例でgnuplot画面を表示させる方法は?
                    @vagobertoの答えは、位置引数が必要な場合に最適なIMHOと思われます。追加する小さな改善点があります。
vagobertoの提案:
#!/usr/local/bin/gnuplot --persist
THIRD=ARG3
print "script name        : ", ARG0
print "first argument     : ", ARG1
print "third argument     : ", THIRD 
print "number of arguments: ", ARGC 
これは以下によって呼び出されます:
$ gnuplot -c script.gp one two three four five
script name        : script.gp
first argument     : one
third argument     : three
number of arguments: 5
私のような怠惰なタイパーの場合、スクリプトを実行可能にすることができます(chmod 755 script.gp)
次に、以下を使用します。
#!/usr/bin/env gnuplot -c
THIRD=ARG3
print "script name        : ", ARG0
print "first argument     : ", ARG1
print "third argument     : ", THIRD
print "number of arguments: ", ARGC
そしてそれを次のように実行します:
$ ./imb.plot a b c d
script name        : ./imb.plot
first argument     : a
third argument     : c
number of arguments: 4
              
ifデフォルトを提供するためにも使用できます。if ! exists("filename") filename='default.data'