【问题标题】:Make a Simple Pie Graph using Bash Shell Script使用 Bash Shell 脚本制作简单的饼图
【发布时间】:2012-08-11 21:29:29
【问题描述】:

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

#!/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 rj@host.com rj@host.com  <<EOF
Data Successfully loaded into LIP_DATA_QUALITY table

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

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

Error Percentage: $QUERY2
EOF

问题陈述:-

我将在$QUERY2 中获得Error Percentage 号码。我需要制作一个简单的饼图,可以使用来自$QUERY2 的数字显示Error PercentageNo Error Percentage,就像使用Bash Shell 脚本的下图一样。

我正在运行SunOS。这可以在 Bash Shell 脚本中执行吗?任何想法将不胜感激。

更新:-

下面是我使用的 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

sh -x chart.sh-运行上述shell脚本后我得到的错误-

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

任何想法将不胜感激。

另一个更新:-

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

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

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

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

【问题讨论】:

标签: linux bash shell solaris sunos


【解决方案1】:

创建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

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

$ bash -x chart.sh

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

这会给你类似的东西

请注意,您只需要bash 和 Internet 连接,无需安装任何内容。

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

【讨论】:

  • 感谢 dtmilano 的建议。我尝试在 bash shell 脚本中复制粘贴整个命令。但是我遇到了一些错误。我已经更新了我的问题,我正在运行什么 shell 脚本,以及我遇到了什么错误。任何建议将不胜感激。在运行这个 shell 脚本之前我还需要安装任何东西吗?
  • 感谢 dtmilano 的编辑。我在这方面取得了一些进展,但我遇到了另一个错误-bash-3.00$ bash -x chart.sh ++ mktemp -t chart mktemp: failed to create file: /tmp/chart + TEMP= + cat chart.sh: line 4: $TEMP: ambiguous redirect + open -a '/Applications/Google Chrome.app' chart.sh: line 51: open: command not found 。我也更新了问题。
  • 再次感谢 dtmilano 的编辑。我又取得了一些进展。这次我没有任何错误。我只是想知道,我将如何看到输出?它会打开浏览器吗?或者它将png文件保存在我机器上的某个位置。用运行 shell 脚本后得到的输出更新了我的问题。或者它会给我一个HTML文件,然后我需要查看该html文件才能看到实际结果?
  • uname 打印的输出是什么? HTML 内容在 $TEMP 中。
  • 如果我输入 uname,这就是我上面得到的。我可以将 tmp 文件夹中的 html 内容视为图表名称。但是怎样才能看到图形需要打开html文件才能看到图形呢?
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    可能没有本地方法可以做到这一点,所以我认为你最好的选择是安装第三方工具,比如-

    PLOTRIX

    Documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多