From f86bbd0f412f5ec8c0f2b987a28821fa301e51a5 Mon Sep 17 00:00:00 2001 From: Llewellyn van der Merwe Date: Mon, 1 Jan 2018 23:14:26 +0200 Subject: [PATCH] Added dynamic percentage watcher options --- .gitignore | 3 +- dynamic.txt | 5 +++ functions.sh | 109 +++++++++++++++++++++++++++++---------------------- getPrice.sh | 30 +++++++++++++- 4 files changed, 98 insertions(+), 49 deletions(-) create mode 100644 dynamic.txt diff --git a/.gitignore b/.gitignore index e1fbcd6..296c50f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ notify sms smsto -factory \ No newline at end of file +factory +dynamic \ No newline at end of file diff --git a/dynamic.txt b/dynamic.txt new file mode 100644 index 0000000..1f869bf --- /dev/null +++ b/dynamic.txt @@ -0,0 +1,5 @@ +# here you can add many currency pairs you want to check in one run, one pair & target per line +# CurrencyToWatch TargetCurrecyToDisplay Percentage +# BTC USD 5 +# ETH USD 3 +# (remove this note and remove .txt from the file name ;) diff --git a/functions.sh b/functions.sh index 9c8505d..ad7912b 100644 --- a/functions.sh +++ b/functions.sh @@ -18,39 +18,6 @@ # FUNCTIONS #================================================================================================================================= -# workout the values based on percentage at which to send/show notice -function setPercentage () { - # get price if not already set - get_Price - # get the price value - local value="${CurrencyPair[${Currency}${Target}]}" - # check that we have a value - if [ "${value}" != "null" ]; then - # new value - COINnewValue="${Currency}${Target} $value" - # check if we have price set before - COINlineNr=$( awk "/${Currency}${Target}/{ print NR; exit }" "$COINvaluePath" ) - re='^[0-9]+$' - # Update coin value keeper - if ! [[ $COINlineNr =~ $re ]] ; then - # set the price for the first time and send notice - echo "${COINnewValue}" >> "$COINvaluePath" - else - # set updater - COINupdate=1 - # old price found - COINoldValue=$(sed -n "${COINlineNr}p" < "$COINvaluePath") - # get the keys - IFS=$' ' - local oldArray=( $COINoldValue ) - # set the value - value="${oldArray[1]}" - fi - # set the above below values - setAboveBelowValues "${value}" - fi -} - # run with the advance field options function runFactory () { # array of repos @@ -68,22 +35,39 @@ function runFactory () { for cpairs in "${currencypairs[@]}"; do # convert line to array local currencypair=($cpairs) - # check number of values - if [ ${#currencypair[@]} == 4 ]; then - # set globals - Currency="${currencypair[0]}" - Target="${currencypair[1]}" - TargetValue="${currencypair[2]}" - TargetAll=1 - if (( "${currencypair[3]}" == 1 )); then - AboveValue=1 + # if percentage + if (( "$PercentSwitch" == 1 )); then + # check number of values + if [ ${#currencypair[@]} == 3 ]; then + # set globals + Currency="${currencypair[0]}" + Target="${currencypair[1]}" + Percentage="${currencypair[2]}" + # set percentages + setPercentage + # run the main functions + runMain else - BelowValue=1 + echoTweak "Line missing values, see example dynamic.txt file for details" fi - # run the main functions - runMain else - echoTweak "Line missing values, see example factory.txt file for details" + # check number of values + if [ ${#currencypair[@]} == 4 ]; then + # set globals + Currency="${currencypair[0]}" + Target="${currencypair[1]}" + TargetValue="${currencypair[2]}" + TargetAll=1 + if (( "${currencypair[3]}" == 1 )); then + AboveValue=1 + else + BelowValue=1 + fi + # run the main functions + runMain + else + echoTweak "Line missing values, see example factory.txt file for details" + fi fi done # display @@ -138,6 +122,39 @@ function runMain () { getActiveCurrencyTarget } +# workout the values based on percentage at which to send/show notice +function setPercentage () { + # get price if not already set + get_Price + # get the price value + local value="${CurrencyPair[${Currency}${Target}]}" + # check that we have a value + if [ "${value}" != "null" ]; then + # new value + COINnewValue="${Currency}${Target} $value" + # check if we have price set before + COINlineNr=$( awk "/${Currency}${Target}/{ print NR; exit }" "$COINvaluePath" ) + re='^[0-9]+$' + # Update coin value keeper + if ! [[ $COINlineNr =~ $re ]] ; then + # set the price for the first time and send notice + echo "${COINnewValue}" >> "$COINvaluePath" + else + # set updater + COINupdate=1 + # old price found + COINoldValue=$(sed -n "${COINlineNr}p" < "$COINvaluePath") + # get the keys + IFS=$' ' + local oldArray=( $COINoldValue ) + # set the value + value="${oldArray[1]}" + fi + # set the above below values + setAboveBelowValues "${value}" + fi +} + # get active currency target function getActiveCurrencyTarget () { # get price if not already set diff --git a/getPrice.sh b/getPrice.sh index f1b1cb1..00bfefb 100755 --- a/getPrice.sh +++ b/getPrice.sh @@ -79,8 +79,10 @@ Getting Coin Value in Fiat Currency at set price Advance options (factory option) ====================================================== - -f Path to file with multiple currency pair options + -f Path to file with multiple currency pair options (Fixed values) (see example factory.txt file for details) + -P Path to file with multiple currency pair options (Percentages) + (see example dynamic.txt file for details) Message options ====================================================== @@ -106,7 +108,7 @@ exit 1 # http://mywiki.wooledge.org/BashFAQ/035 # http://wiki.bash-hackers.org/howto/getopts_tutorial -while getopts hc:C:o:v:B:A:baqtT:sS:M:lf:I:kp: opt; do +while getopts hc:C:o:v:B:A:baqtT:sS:M:lf:I:kp:P: opt; do case $opt in I) if (( "$OPTARG" == 2 )); then @@ -133,6 +135,30 @@ while getopts hc:C:o:v:B:A:baqtT:sS:M:lf:I:kp: opt; do Percentage=$OPTARG PercentSwitch=1 ;; + P) + FilePath=$OPTARG + # make sure we have a file + if [ ! -f "$FilePath" ] + then + echo "File path ($FilePath) does not exist, please add correct path" + show_help >&2 + exit 1 + fi + Factory=1 + # reset all basic settings + Currency="BTC" + Target="USD" + TargetValue=0 + TargetBelowValue=0 + TargetAboveValue=0 + TargetAll=0 + TargetBelow=0 + TargetAbove=0 + BelowValue=0 + AboveValue=0 + # percentage switch + PercentSwitch=1 + ;; v) TargetValue=$OPTARG TargetAll=1