#!/bin/bash # # the format of your ~/.stocks file is one record per line, tab separated. # TICKER high low [quiet] # The ticker should be in at the very start of the line, and must be a Yahoo! # stock ticker. Comments begin with a # followed immediately by a whitespace. # Eg: # CRY.TO 33.00 30.00 # The optional 4th field may contain the word quiet. The stock will not be # displayed unless it goes outside the high and low bounds. This allows for # monitoring many stocks than will fit in a single terminal. # INPUT_FILE=~/.stocks if [ "$1" ] then if [ -f "$1" ] then INPUT_FILE="$1" else echo "Error: Bad stocks file $1" exit 1 fi fi debug=false # configure the sounds you want here alert_sound=${HOME}/public_html/sounds/austin_powers/austin/groovy_yeah.wav mp3dec="mpg321 -w -" # convert all decimal values into fixed-point integers of 1/10000 normalize () { local x if [ ${1%.[0-9][0-9][0-9][0-9]} != $1 ] then x=${1/.} elif [ ${1%.[0-9][0-9][0-9]} != $1 ] then x=${1/.}0 elif [ ${1%.[0-9][0-9]} != $1 ] then x=${1/.}00 elif [ ${1%.[0-9]} != $1 ] then x=${1/.}000 else x=${1/.}0000 fi echo $x } alert() { ps -fu $LOGNAME | grep -v grep | grep -q xmms && xmms -u if echo $alert_sound | grep -q 'mp3$' then $mpgdec $alert_sound | aplay -q -t wav - else aplay -q $alert_sound fi ps -fu $LOGNAME | grep -v grep | grep -q xmms && xmms -u } tmpfile=/tmp/stocks.$$ while read stock high low quiet do [ "${stock%#*}" != "$stock" ] && continue $debug && echo STOCK $stock $high - $low [ "$stocks" ] && stocks="$stocks,$stock" [ "$stocks" ] || stocks="$stock" done <${INPUT_FILE} $debug && echo list = $stocks url="http://finance.yahoo.com/d/quotes.csv?s=${stocks}&f=sl1d1t1c1oghv&e=.csv" wget -qO - "$url" | sed -e 's/"//g' -e 's/,/ /g' -e 's#N/A#0#g' >$tmpfile if [ ! -s $tmpfile ] then echo "No data" exit 1 fi year=$(date +%Y) doalert=false echo -e \ " Stock Price Last Trade Change Open Day Range Volume" # Stock Price Last Trade Change Open Day Range Volume while read stock high low quiet do [ "${stock%#*}" != "$stock" ] && continue current=$(awk "/^$stock\>/ {print \$2}" $tmpfile) high=$(normalize $high) low=$(normalize $low) current=$(normalize $current) $debug && echo DEBUG: $stock $high $current $low display=true log=false prefix= [ "$quiet" ] && display=false if [ $current -gt $high ] then doalert=true display=true #log=true prefix="^" elif [ $current -lt $low ] then doalert=true display=true #log=true prefix="v" else prefix=" " # $display && echo -n " " fi if echo $stock | grep -qe = then $display && grep "^$stock " $tmpfile | cut -d' ' -f1-4\ | xargs printf "${prefix}%-10s%6.4f %-10s %-7s\n" #$log && grep "^$stock " $tmpfile | cut -d' ' -f1-4\ # | xargs printf "${prefix}%-10s%6.4f %-10s %-7s\n" \ # >>~/.stock.log else change=$(grep "^$stock " $tmpfile | cut -d' ' -f5) if [ "X${change%[0-9].*}" = "X+" ] ; then color='0;32' ; elif [ "X${change%[0-9].*}" = "X-" ] ; then color='0;31' ; else color='0;30' ; fi if [ "${prefix}" = " " ] then declare -a info info=( $(grep "^$stock " $tmpfile) ) day_high=$(normalize ${info[7]}) day_low=$(normalize ${info[6]}) if [ $day_high -ge $high ] then prefix="^" elif [ $day_low -le $low ] then prefix="v" fi unset info fi fmt="${prefix}\\033[${color}m%-10s%6.2f %-10s %-7s " fmt="${fmt}%+6.3f %6.2f %6.2f-%-6.2f %d\\033[0m\n" $display && grep "^$stock " $tmpfile | xargs printf "$fmt" #fmt="${prefix}%-10s%6.2f %-10s %-7s " #fmt="${fmt}%+6.3f %6.2f %6.2f-%-6.2f %d\n" #$log && grep "^$stock " $tmpfile | xargs printf "$fmt" \ # >>~/.stock.log fi done <${INPUT_FILE} #$doalert && alert rm -f $tmpfile