使用 Bash Shell 脚本制作简单的饼图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11917818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:59:25  来源:igfitidea点击:

Make a Simple Pie Graph using Bash Shell Script

linuxbashshellsolarissunos

提问by arsenal

Below is my Bash Shell Script from which I am executing my two Hive SQL Queries which is working fine. And I am calculating Error Percentagein this Bash Shell Script.

下面是我的 Bash Shell 脚本,我从中执行了两个运行良好的 Hive SQL 查询。我正在Error Percentage这个 Bash Shell 脚本中进行计算。

#!/bin/bash

QUERY1=`hive -e "
set mapred.job.queue.name=hdmi-technology;
SELECT SUM(total_items_purchased), SUM(total_items_missingormismatch) from lip_data_quality where dt='$DATE_YEST_FORMAT2';"`

QUERY2=`hive -e "
set mapred.job.queue.name=hdmi-technology;
SELECT 100 * SUM(total_items_missingormismatch*1.0) / SUM(total_items_purchased) FROM lip_data_quality where dt='$DATE_YEST_FORMAT2';"`


mailx -s "LIP Data Quality Report for $DATE_YEST_FORMAT1" -r [email protected] [email protected]  <<EOF
Data Successfully loaded into LIP_DATA_QUALITY table

Total Items Purchased: `echo $QUERY1 | awk '{print }'`

Total Items MissingorMismatch: `echo $QUERY1 | awk '{print }'`

Error Percentage: $QUERY2
EOF

Problem Statement:-

问题陈述:-

I will be getting Error Percentagenumber in $QUERY2. I need to make a Simple Pie Graph that can show Error Percentageand No Error Percentageby using the number from $QUERY2just like below graph using Bash Shell Script.

我会得到Error Percentage号码$QUERY2。我需要制作一个简单的饼图,它可以使用 Bash Shell 脚本显示Error PercentageNo Error Percentage使用$QUERY2下图中的数字。

enter image description here

在此处输入图片说明

I am running SunOS. Is this possible to do in Bash Shell script? Any thoughts will be appreciated.

我在跑步SunOS。这可以在 Bash Shell 脚本中实现吗?任何想法将不胜感激。

Update:-

更新:-

Below is the Shell Script that I am using, that I created using vi editor.

下面是我正在使用的 Shell 脚本,我使用vi editor.

 1  #! /bin/bash
 2
 3  TEMP=$(mktemp -t chart)
 4  QUERY1=36
 5  QUERY2=64
 6  cat > $TEMP <<EOF
 7      <html>
 8        <head>
 9          <!--Load the AJAX API-->
10          <script type="text/javascript" src="https://www.google.com/jsapi"></script>
11          <script type="text/javascript">
12
13            // Load the Visualization API and the piechart package.
14            google.load('visualization', '1.0', {'packages':['corechart']});
15
16            // Set a callback to run when the Google Visualization API is loaded.
17            google.setOnLoadCallback(drawChart);
18
19            // Callback that creates and populates a data table,
20            // instantiates the pie chart, passes in the data and
21            // draws it.
22            function drawChart() {
23
24              // Create the data table.
25              var data = new google.visualization.DataTable();
26              data.addColumn('string', 'Title');
27              data.addColumn('number', 'Value');
28              data.addRows([
29                ['Error Percentage', $QUERY1],
30                ['No Error Percentage', $QUERY2]
31              ]);
32
33              // Set chart options
34              var options = {'title':'Errors',
35                             'width':400,
36                             'height':300};
37
38              // Instantiate and draw our chart, passing in some options.
39              var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
40              chart.draw(data, options);
41            }
42          </script>
43        </head>
44
45        <body>
46          <!--Div that will hold the pie chart-->
47          <div id="chart_div"></div>
48        </body>
49      </html>
50      EOF
51
52      # open browser
53      case $(uname) in
54         Darwin)
55            open -a /Applications/Google\ Chrome.app $TEMP
56            ;;
57
58         Linux|SunOS)
59            firefox $TEMP
60            ;;
61       esac
62

Error that I got after running the above shell script as sh -x chart.sh-

运行上述 shell 脚本后出现的错误sh -x chart.sh-

bash-3.00$ sh -x chart.sh
chart.sh: syntax error at line 3: `TEMP=$' unexpected

Any thoughts will be appreciated.

任何想法将不胜感激。

Another Update:-

另一个更新:-

After the below suggestions, when I tried something like this-I got another error.

在以下建议之后,当我尝试这样的事情时 - 我又遇到了另一个错误。

bash-3.00$ bash -x chart.sh
++ mktemp -t chart
mktemp: failed to create file: /tmp/chart
+ TEMP=
+ QUERY1=36
+ QUERY2=64
+ cat
chart.sh: line 6: $TEMP: ambiguous redirect

Another Update: Made Some Progress I guess. Not sure where the output files will be going? Or it will open into a browser?

另一个更新:我猜取得了一些进展。不确定输出文件的去向?或者它会在浏览器中打开?

bash-3.00$ bash -x chart.sh
++ mktemp -t chart
+ TEMP=/tmp/chart
+ QUERY1=36
+ QUERY2=64
+ cat
++ uname

回答by Diego Torres Milano

A very simple way of creating a Google Chart:

创建Google Chart 的一种非常简单的方法:

#! /bin/bash

TEMP=$(mktemp -t chart.XXXXX)
QUERY1=36
QUERY2=64
cat > $TEMP <<EOF
<html>
  <head>
    <!--Load the AJAX API-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">

      // Load the Visualization API and the piechart package.
      google.load('visualization', '1.0', {'packages':['corechart']});

      // Set a callback to run when the Google Visualization API is loaded.
      google.setOnLoadCallback(drawChart);

      // Callback that creates and populates a data table,
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawChart() {

        // Create the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Title');
        data.addColumn('number', 'Value');
        data.addRows([
          ['Error Percentage', $QUERY1],
          ['No Error Percentage', $QUERY2]
        ]);

        // Set chart options
        var options = {'title':'Errors',
                       'width':400,
                       'height':300};

        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>

  <body>
    <!--Div that will hold the pie chart-->
    <div id="chart_div"></div>
  </body>
</html>
EOF

# open browser
case $(uname) in
   Darwin)
      open -a /Applications/Google\ Chrome.app $TEMP
      ;;

   Linux|SunOS)
      firefox $TEMP
      ;;
 esac

if you save it as chart.shthen run it as

如果你将它保存为chart.sh然后运行它

$ bash -x chart.sh

or

或者

$ chmod +x chart.sh
$ ./chart.sh

which gives you something like

这给了你类似的东西

enter image description here

在此处输入图片说明

Notice that you only need bashand an Internet connection, there's nothing to install.

请注意,您只需要bash一个 Internet 连接,无需安装任何东西。

mktempshould be available in Solaris (http://docs.oracle.com/cd/E23824_01/html/821-1461/mktemp-1.html). If you don't have it just set TEMP to whatever file you want the HTML output to be.

mktemp应该在 Solaris ( http://docs.oracle.com/cd/E23824_01/html/821-1461/mktemp-1.html) 中可用。如果您没有它,只需将 TEMP 设置为您希望 HTML 输出的任何文件。

回答by technosaurus

A simple way to create a pie chart in only shell, is to generate an svg image.

仅在 shell 中创建饼图的一种简单方法是生成 svg 图像。

A simple/small program to generate other image types is Ploticus. http://ploticus.sourceforge.net/doc/welcome.html

生成其他图像类型的简单/小程序是 Ploticus。 http://ploticus.sourceforge.net/doc/welcome.html

回答by AnBisw

There is probably no native way of doing it, so I would assume that your best bet is to get a third party tool installed, like-

可能没有这样做的本地方式,所以我认为您最好的选择是安装第三方工具,例如-

PLOTRIX

PLOTRIX

Documentation

文档