0

Attempting to write my first shell script to get the bitcoin price only when I'm not idle. After adding the if statement I receive:

/home/a/.scripts/bitcoin: line 3: [: -ge: unary operator expected

Code:

#! /usr/bin/bash

if [ $xprintidle -ge 600000 ]; then
  URL="https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=GBP"

    response=$(curl -s -w "%{http_code}" $URL)

    http_code=$(tail -n1 <<< "$response")  # get the last line
    content=$(sed '$ d' <<< "$response")   # get all but the last line which contains the status code

    echo "$response" | sed -e 's/{"GBP":\(.*\)}200/\1/'
else 
    echo "Idle"
fi
AAA
  • 361
  • 1
  • 5
  • 19
  • 1
    Hint: If you quote `"$xprintidle"` you'll get a more useful error message. – Charles Duffy May 25 '22 at 17:53
  • if you don't quote your expansions, how many words your variables will expand into depends on their contents. It may be zero (typically meaning the variable is empty), in which case you get the problem here; it may be more than you expect, exposing you to completely different syntax errors from `[`. Unless you know *exactly* why you're choosing not to in a specific context, **always** quote your expanisons. – Charles Duffy May 25 '22 at 17:54
  • 2
    Also, think about running your code through http://shellcheck.net/ before asking questions here. – Charles Duffy May 25 '22 at 17:55

0 Answers0