Skip to content

Symbol listed twice crashes ticker.sh script #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
PSLLSP opened this issue Oct 26, 2019 · 1 comment
Closed

Symbol listed twice crashes ticker.sh script #14

PSLLSP opened this issue Oct 26, 2019 · 1 comment

Comments

@PSLLSP
Copy link

PSLLSP commented Oct 26, 2019

Symbol listed twice crashes ticker.sh script. Example:

$ ./ticker.sh GOOG GOOG
./ticker.sh: line 43: [: POSTPOST: binary operator expected
./ticker.sh: line 51: [: too many arguments
./ticker.sh: line 58: [: too many arguments
GOOG       1265.131265.13       0.00./ticker.sh: line 81: printf: (0.33%)(0.33%): invalid number
      4.14   4.1400146      0.00

Problem is that once symbol is listed more than once, "jq" returns not a string but an array.

The fix:

$ git diff
diff --git a/ticker.sh b/ticker.sh
index 6bceda6..acf2435 100755
--- a/ticker.sh
+++ b/ticker.sh
@@ -34,13 +34,13 @@ results=$(curl --silent "$API_ENDPOINT&fields=$fields&symbols=$symbols" \
   | jq '.quoteResponse .result')
 
 query () {
-  echo $results | jq -r ".[] | select(.symbol == \"$1\") | .$2"
+  echo $results | jq -r "[.[] | select(.symbol == \"$1\") | .$2][0]"
 }
 
 for symbol in $(IFS=' '; echo "${SYMBOLS[*]}"); do
   marketState="$(query $symbol 'marketState')"
 
-  if [ -z $marketState ]; then
+  if [ "$marketState" = "null" ]; then
     printf 'No results for symbol "%s"\n' $symbol
     continue
   fi

Example after the fix:

$ ./ticker.sh GOOG GOOG
GOOG       1270.00      4.87     (0.38%) *
GOOG       1270.00      4.87     (0.38%) *

There is one more problem with some symbols those are not active anymore. I am not sure, for example "YHOO" or "NV". printf command crashes because it cannot print "null" as number. I suggest following fix but I am not sure if it is the right solution; maybe some message should be written:

@@ -69,6 +69,12 @@ for symbol in $(IFS=' '; echo "${SYMBOLS[*]}"); do
     percent=$(query $symbol 'regularMarketChangePercent')
   fi
 
+  if [ "$price" = "null" ]; then
+    price="0"
+    diff="0"
+    percent="0"
+  fi
+
   if [ "$diff" == "0" ]; then
     color=
   elif ( echo "$diff" | grep -q ^- ); then
@pstadler
Copy link
Owner

Why would you list the same ticker twice?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants