2012-08-11 84 views
1

以下是我的Bash Shell腳本,我正在執行我的兩個Hive SQL查詢工作正常。我正在計算這個Bash Shell腳本中的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 $1}'` 

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

Error Percentage: $QUERY2 
EOF 

問題陳述: -

我將在$QUERY2越來越Error Percentage號。我需要製作一個簡單的餅形圖,通過使用$QUERY2中的編號,可以顯示Error PercentageNo Error Percentage,就像下圖使用Bash Shell腳本一樣。

enter image description here

我正在SunOS。這是可能在Bash Shell腳本中做到的嗎?任何想法將不勝感激。

更新: -

下面是shell腳本,我使用,我用vi editor創建。我運行上面的外殼腳本sh -x chart.sh後得到

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 

錯誤 -

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 
+1

看看['gnuplot'](http://www.gnuplot.info)。 – chepner 2012-08-11 22:15:36

回答

4

的作成的非常簡單的方式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 

,讓你像

enter image description here

請注意,您只需要bash和互聯網連接,沒有什麼安裝。

mktemp應在Solaris(http://docs.oracle.com/cd/E23824_01/html/821-1461/mktemp-1.html)中可用。如果你沒有它,只需將TEMP設置爲你希望HTML輸出的文件即可。

+0

感謝dtmilano,建議。我試着在bash shell腳本中粘貼整個命令。但我有一些錯誤。我更新了我的問題,我正在運行的shell腳本以及我得到的錯誤。任何建議將不勝感激。在運行這個shell腳本之前,我還需要安裝任何東西嗎? – ferhan 2012-08-12 22:05:59

+0

感謝dtmilano的編輯。我做了一些進步,但我得到了另一個錯誤 - 「bash-3.00 $ bash -x chart.sh ++ mktemp -t圖表 mktemp:未能創建文件:/ tmp /圖表 + TEMP = + cat chart.sh:第4行:$ TEMP:模糊重定向 + open -a'/ Applications/Google Chrome.app' chart.sh:第51行:open:command not found '。我也更新了這個問題。 – ferhan 2012-08-13 00:43:50

+0

再次感謝dtmilano的編輯。我又取得了一些進展。這次我沒有任何錯誤。我只是想知道,我將如何看到輸出?它會打開瀏覽器?或者它會將png文件保存在我的機器上。用運行shell腳本後得到的輸出更新我的問題。或者它會給我一個HTML文件,那麼我需要查看HTML文件的實際結果來看? – ferhan 2012-08-13 04:52:31

0

有可能是這樣做的沒有原生的辦法,所以我認爲最好的辦法是讓安裝第三方工具,喜歡 -

PLOTRIX

Documentation