下面是我的bash shell脚本,我正在从中执行两个运行良好的配置单元sql查询。我正在计算这个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 [email protected] [email protected]  <<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

问题陈述:
我将在Error Percentage中得到$QUERY2数字。我需要制作一个简单的饼图,可以使用Error Percentage中的数字来显示No Error Percentage$QUERY2,就像下面使用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

最佳答案

创建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输出文件。

关于linux - 使用Bash Shell脚本制作简单的饼图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11917818/

10-11 08:30