Chinaunix首页 | 论坛 | 博客
  • 博客访问: 155186
  • 博文数量: 41
  • 博客积分: 2246
  • 博客等级: 大尉
  • 技术积分: 375
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-27 15:45
文章分类
文章存档

2012年(4)

2011年(1)

2010年(11)

2009年(25)

分类: Python/Ruby

2012-02-28 11:15:56

    偶然在网上看到sina有一个查询股票信息的api,随即在linux下用shell写了个小脚本查询股票信息,方便工作之余关心一下股票行情,而且在linux终端里面查看,不容易被boss发现哦(我还故意把字体颜色调成了深蓝色,不仔细看,看不出来的哦,boss同事还以为你在认真工作写代码呢,嘿嘿)。小脚本分两个部分,checkstock.sh和stock_num.dat,把想要查询的股票代码(格式为:sh601699 或者 sz000001)添加到stock_num.dat中去,运行checkstock.sh即可查询。
    顺便总结一下,写脚本时候碰到的一些问题和解决方法,以备日后查阅,也望各位大牛指点新招妙招:
1. 浮点数的比较:
    两种方法:1. ds=`echo "1.23" "2.33" | awk '{if($1>=$2) {print 1;} else {print 0;}}'`
                               2. ds=`echo "$1 >= $2" | bc`
    因为test只能比较整数,浮点数的比较借用了bc和awk,通过上述两句,ds的值为0或者1,再做判断。

2. 数组作为参数传给函数:
    见函数prase_info()中的第一句, 如下:
     eval $(echo $1 | awk '{split($0,array,","); for(i in array) print "hs_info_array["i"]="array[i]}')
通过这个语句,可以把参数$1(字符串数组)通过awk赋给shell数组hs_info_array 。

3. 传参数不改变字符数组的结构
    见函数print_array(),传值的时候用 print_array "${STOCK_NUM[@]}",这里用@,结构不变,用*,则数组中的字符串则会被拆分,比如,STOCK_NUM[1]="aaa   bbb" 如果传值时用*,则会被分成两个,一个是aaa,一个是bbb。而用@,则仍然是“aaa   bbb”

配置文件stock_num.dat如下:
  1. sh601699
  2. sh600600
  3. sz000002
shell脚本checkstock.sh如下:
  1. #!/bin/bash

  2. ##################################################################################
  3. # Read stock number data from ./stock_num_file.txt
  4. #
  5. #
  6. #
  7. ###################################################################################
  8. STOCK_NUM_FILE="./stock_num.dat"
  9. ITEM_COLOR="\033[34m"
  10. COLOR_RESET="\033[0m"
  11. COLOR_RED="\033[31m"
  12. COLOR_GREEN="\033[32m"

  13. function ERRTRAP()
  14. {
  15.         echo "[LINE:$1] Error: Comand or Function exited with status $?"

  16. }

  17. function print_array()
  18. {
  19.         i=0
  20.         for elem in "$@"
  21.         do
  22.                 echo $((i++)) $elem
  23.         done
  24. }

  25. function read_data_file()
  26. {
  27.         i=0
  28.         if [ ! -e "./stock_num.dat" ]
  29.         then
  30.                 echo "ERROR: The stock_num.dat isn't existed!"
  31.                 exit 0
  32.         fi

  33.         while read line
  34.         do
  35.                 STOCK_NUM[$i]=$line
  36.                 let i+=1
  37.         done < $STOCK_NUM_FILE
  38.         for ((i=0; i < ${#STOCK_NUM[@]}; i++ ))
  39.         do
  40.         #echo "${STOCK_NUM[$i]}"
  41.                 get_info ${STOCK_NUM[$i]}
  42.         done
  43. }

  44. ############################################################################
  45. #
  46. # funciton: get_info(arg1)
  47. #
  48. # result: Get stock information from Internet
  49. #
  50. ############################################################################
  51. function get_info()
  52. {
  53.         stock_info=`curl http://hq.sinajs.cn/list=$1 2>/dev/null | sed -n -e 's/var.*=\"//' -e 's/ //g' -e 's/\";$//p'`
  54. # echo "$stock_info"
  55. # s_name=`echo "$stock_info" | sed -n 's/,.*$//p'`
  56. # echo $s_name
  57. # stock_data=`echo "$stock_info" | sed -n 's/'$s_name',//p'`
  58. # echo $stock_data
  59.         s_num=$1
  60.         if [ "$stock_info" != "" ];then
  61.                 prase_info $stock_info
  62.                 display_info
  63.         else
  64.                 echo "Exception:Can't find any infomation about $s_num"
  65.         fi
  66. }

  67. function prase_info()
  68. {
  69.         eval $(echo $1 | awk '{split($0,array,","); for(i in array) print "stock_info_array["i"]="array[i]}')
  70.         s_name=${stock_info_array[1]}
  71.         s_today_open=${stock_info_array[2]}
  72.         s_lastday_close=${stock_info_array[3]}
  73.         s_current_price=${stock_info_array[4]}
  74.         s_today_highest=${stock_info_array[5]}
  75.         s_today_lowest=${stock_info_array[6]}
  76.         s_dealed_stock_num=${stock_info_array[9]}
  77.         s_dealed_stock_cash_num=${stock_info_array[10]}
  78.         s_date=${stock_info_array[31]}
  79.         s_time=${stock_info_array[32]}

  80.         #####################################
  81.         #${stock_info_array[11-20] buy1-buy5
  82.         #${stock_info_array[21-30] sale1-sale5
  83.         ######################################


  84. }

  85. ############################################################################
  86. #
  87. # funciton: print_pre_zero(arg1)
  88. #
  89. # result: add prefix zero for rate
  90. #
  91. ############################################################################
  92. function print_pre_zero()
  93. {
  94.         local cmp_value_1=`echo "$1 < 0 && $1 > -1" | bc`
  95.         local cmp_value_2=`echo "$1 >0 && $1 < 1" | bc`
  96.         if [ $cmp_value_1 -eq "1" ]
  97.         then
  98.                 local tmp_val=`echo "scale=2;$1 / -1" | bc -l`
  99.                 echo "-0$tmp_val"
  100.         elif [ $cmp_value_2 -eq "1" ]
  101.         then
  102.                 echo "0$1"
  103.         else
  104.                 echo "$1"
  105.         fi

  106. }

  107. ############################################################################
  108. #
  109. # funciton: cmp_display(arg1,arg2)
  110. #
  111. # result: print arg1 with color(red or green)
  112. #
  113. ############################################################################
  114. function cmp_display()
  115. {
  116. #############################################################################
  117. # Two methods to compare float:
  118. # 1. ds=`echo "0" "0" | awk '{if($1>=$2) {print 1;} else {print 0;}}'`
  119. # 2. ds=`echo "$1 >= $2" | bc`
  120. # echo $ds
  121. #############################################################################
  122.         local cmp_value=`echo "$1 >= $2" | bc -l`
  123.         if [ $cmp_value -eq "0" ]
  124.         then
  125.                 echo -e "$COLOR_GREEN $1 $COLOR_RESET"
  126.         else
  127.                 echo -e "$COLOR_RED $1 $COLOR_RESET"
  128.         fi
  129. }


  130. ############################################################################
  131. #
  132. # funciton: cmp_display_rate(arg1,arg2)
  133. #
  134. # result: print arg1 with color(red or green) and add prefix zero.
  135. #
  136. ############################################################################
  137. function cmp_display_rate()
  138. {
  139.         local tmp_rate=`echo "($1 - $2) / $2 * 100" | bc -l`
  140.         local cmp_rate=`echo "scale=2;$tmp_rate/1" | bc`

  141.         local cmp_value=`echo "$1 >= $2" | bc`

  142.         if [ $cmp_value -eq "0" ]
  143.         then
  144.                 echo -e "$COLOR_GREEN $1 ($(print_pre_zero $(echo $1 - $2 | bc -l)) $(print_pre_zero $cmp_rate)%)$COLOR_RESET"
  145.         else
  146.                 echo -e "$COLOR_RED $1 (+$(print_pre_zero $(echo $1 - $2 | bc -l)) $(print_pre_zero $cmp_rate)%)$COLOR_RESET"
  147.         fi


  148. }

  149. function display_info()
  150. {
  151.         echo -e "$s_date"
  152.         echo -e "$s_time"
  153.         echo -e "$ITEM_COLOR STOCK: $s_name No. $s_num $COLOR_RESET"
  154.         echo -e "$ITEM_COLOR Last Day Closed Price:$COLOR_RESET $s_lastday_close"
  155.         echo -e "$ITEM_COLOR Today Open Price:$COLOR_RESET $(cmp_display $s_today_open $s_lastday_close)"
  156.         echo -e "$ITEM_COLOR Curren Price:$COLOR_RESET $(cmp_display_rate $s_current_price $s_lastday_close)"
  157.         echo -e "$ITEM_COLOR Highest Price:$COLOR_RESET $(cmp_display $s_today_highest $s_lastday_close)"
  158.         echo -e "$ITEM_COLOR Lowest Price:$COLOR_RESET $(cmp_display $s_today_lowest $s_lastday_close)"
  159.         echo -e "$ITEM_COLOR Dealed Stock Num(per 100):$COLOR_RESET $(expr $s_dealed_stock_num / 100)"
  160.         echo -e "$ITEM_COLOR Dealed Stock Cash Num:$COLOR_RESET $s_dealed_stock_cash_num"
  161. }

  162. function display_hs_index()
  163. {
  164.         eval $(echo $1 | awk '{split($0,array,","); for(i in array) print "hs_info_array["i"]="array[i]}')
  165.         echo -e "$ITEM_COLOR Index:$COLOR_RESET ${hs_info_array[1]}$COLOR_RESET"
  166.         echo -e "$ITEM_COLOR Today Open:$COLOR_RESET $(cmp_display ${hs_info_array[2]} ${hs_info_array[3]})"
  167.         echo -e "$ITEM_COLOR Last CLosed:$COLOR_RESET ${hs_info_array[3]}"
  168.         echo -e "$ITEM_COLOR Curren :$COLOR_RESET $(cmp_display_rate ${hs_info_array[4]} ${hs_info_array[3]})"
  169.         echo -e "$ITEM_COLOR Highest:$COLOR_RESET $(cmp_display ${hs_info_array[5]} ${hs_info_array[3]})"
  170.         echo -e "$ITEM_COLOR Lowest:$COLOR_RESET $(cmp_display ${hs_info_array[6]} ${hs_info_array[3]})"



  171. }

  172. function view_hs_index()
  173. {
  174.         sh_info=`curl http://hq.sinajs.cn/list=sh000001 2>/dev/null | sed -n -e 's/var.*=\"//' -e 's/\";$//p'`
  175.         if [ "$sh_info" != "" ];then
  176.                 display_hs_index $sh_info
  177.         else
  178.                 echo "Exception:Can't find any infomation about $s_num"
  179.         fi
  180.         sz_info=`curl http://hq.sinajs.cn/list=sz399001 2>/dev/null | sed -n -e 's/var.*=\"//' -e 's/\";$//p'`
  181.         if [ "$sz_info" != "" ];then
  182.                 display_hs_index $sz_info
  183.         else
  184.                 echo "Exception:Can't find any infomation about $s_num"
  185.         fi

  186. }

  187. view_hs_index
  188. read_data_file
  189. #trap "ERRTRAP $LINENO" ERR
附件:
            check_stock.rar  
            

阅读(4846) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~