Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何使用Shell脚本自动保存绘图_Bash_Shell - Fatal编程技术网

Bash 如何使用Shell脚本自动保存绘图

Bash 如何使用Shell脚本自动保存绘图,bash,shell,Bash,Shell,我有10个.txt文件,其中包含我的数据,如output1.txt,output2.txt,output3.txt等等。我使用以下命令绘制此数据: #!/bin/bash gnuplot set o "output1.png" p "output1.txt" u 1:2 with lines set o "output2.png" p "output2.txt" u 1:2 with lines set o "output3.png" p "output3.txt" u 1:2 with lin

我有10个
.txt
文件,其中包含我的数据,如
output1.txt
output2.txt
output3.txt
等等。我使用以下命令绘制此数据:

#!/bin/bash
gnuplot
set o "output1.png"
p "output1.txt" u 1:2 with lines
set o "output2.png"
p "output2.txt" u 1:2 with lines
set o "output3.png"
p "output3.txt" u 1:2 with lines
等等


显然,当我有大约100个数据文件时,这种方法不是很有用。我如何编写一个shell脚本,在脚本中使用
for
循环将数据自动保存到文件中?我是否有可能或者应该转向其他地方,比如python脚本?

我想您需要这样的东西:

for each in output*.txt
do
  BASE_NAME=$(basename $each .txt)
  echo ${BASE_NAME}
  gnuplot <<- EOF
    set o "${BASE_NAME}.png"
    p "$each" u 1:2 with lines
  EOF
done
用于输出*.txt中的每个
做
基本名称=$(基本名称$each.txt)
echo${BASE_NAME}
gnuplot完全未经测试:

您可以将其直接导入gnuplot:

./abovescript.sh | gnuplot
或者重定向到脚本文件,然后使用gnuplot调用

./abovescript.sh > myscript
gnuplot myscript

根据Mark Setchell的建议更新:

for fn in output*.txt; do
    # Extract the file "base"
    fid=${fn%%.txt}
    # Build output png
    out=${fid}.png
    # Echo the commands
    echo set o \"${out}\"
    echo p \"${fn}\" u 1:2 with lines
done | gnuplot
这还假设您的“输入”文件的名称与
outputX.txt的名称完全一致,其中
X
不包含任何空格

由马克·塞切尔添加。。。只是为了好玩,如果你给情节加上一点标签,那么它看起来更专业一些,方法是添加以下内容:

...
out=...
# Echo the commands
echo set title \"${out} - plotted with gnuplot\"
echo set ylabel \"y axis\"
echo set xlabel \"x axis\"

一些建议。。。将
$(ls..)
替换为
output*.txt
,以避免解析
ls
的输出。使用
fid=${fn%%.txt}
剥离扩展。在
完成后添加
|gnuplot
。哦,在每个
前面添加一个反斜杠“
我可以问一下,从语法角度看%%sign-in fn%%的功能是什么吗?这会产生以下错误:第4行:意外令牌附近的语法错误
fid'第4行:
fid=${fn%%.txt}
${string%%substring}
表示从
字符串
的后面删除子字符串
的最长匹配项--see。它给出以下错误:cell2.sh:第6行:BASE\u NAME:未找到命令第0行:警告:跳过不可读文件“output*.txt”“第0行:绘图中无数据当前文件夹中是否有文件outputX.txt?是的,它会不断绘图,但不会保存数据。它仍然会出现以下错误:cell2.sh:第7行:未找到输出:命令cell2.sh:第7行:未找到基本名称:命令。”
...
out=...
# Echo the commands
echo set title \"${out} - plotted with gnuplot\"
echo set ylabel \"y axis\"
echo set xlabel \"x axis\"