1

Here is my full code:

   #!/bin/bash
wget -O /tmp/crex24.txt --no-check-certificate "https://api.crex24.com/CryptoExchangeService/BotPublic/ReturnTicker?request=[NamePairs=BTC_LTC,BTC_ETH,BTC_XMR]"
echo "++++++++++++++++ CREX 24 ++++++++++++++++"
number_of_pairs=`cat /tmp/crex24.txt | grep PairId | wc -l`
count=1
while [ $count -le $number_of_pairs ]
do
pairname=`cat /tmp/crex24.txt | grep -e "PairName" | sed -n "${count}p" | cut -d: -f2 | tr -d '", '  | cut -c1-10`
highprice=`cat /tmp/crex24.txt | grep -e "HighPrice" | sed -n "${count}p" | cut -d: -f2 | tr -d '", ' | cut -c1-10`
lowprice=`cat /tmp/crex24.txt | grep -e "LowPrice" | sed -n "${count}p" | cut -d: -f2 | tr -d '", ' | cut -c1-10`
echo "$highprice $lowprice $pairname"
echo "$pairname $highprice $lowprice"
let "count++"
done

Output:

++++++++++++++++ CREX 24 ++++++++++++++++
0.01663970 0.01574956 BTC_LTC
 0.01663970 0.01574956
0.07105730 0.06700000 BTC_ETH
 0.07105730 0.06700000
0.03130300 0.02700000 BTC_XMR
 0.03130300 0.02700000

My question is why wont $pairname echo out when in the beginning ? What am i doing wrong ?

Thanks.

Miracle
  • 387
  • 5
  • 31
huz_akh
  • 93
  • 1
  • 9
  • What is in the /tmp/crex24.txt file? Looks like ``$pairname`` is just empty. – Kokogino Apr 26 '18 at 06:46
  • The problem is that pairname contains the \r symbol at the end so anything after it will replace it on the line. – PTRK Apr 26 '18 at 06:48
  • @huz_akh what do you want the output to actually look like? – ffledgling Apr 26 '18 at 06:49
  • 1
    Also, don't use backticks for subshell, see https://stackoverflow.com/questions/9449778/what-is-the-benefit-of-using-instead-of-backticks-in-shell-scripts – hellow Apr 26 '18 at 06:50
  • Kokogino : No its not empty as you can see in the above live PTRK : I am not aware of the \r , I will look more into it ffledgling : I want $pairname in the beginning – huz_akh Apr 26 '18 at 06:57

1 Answers1

1

The problem may be that pairname contains the character \r as shown with bash -x script.sh

+ pairname=$'BTC_XMR\r'

Since you used echo without -e, this should not be a problem but i dont know why, echo process this escape character and so anything after that is returned to the first character of the line and covers pairname.

My answer is to use cut -d instead of cut -c to get pairname

pairname=$(cat /tmp/crex24.txt | grep -e "PairName" | sed -n "${count}p" | cut -d: -f2 | cut -d\" -f2)

gives you

++++++++++++++++ CREX 24 ++++++++++++++++
0.01663970 0.01574956 BTC_LTC
BTC_LTC 0.01663970 0.01574956
0.07105730 0.06700000 BTC_ETH
BTC_ETH 0.07105730 0.06700000
0.03130300 0.02700000 BTC_XMR
BTC_XMR 0.03130300 0.02700000

Now we have pairname in the beginning

PTRK
  • 899
  • 1
  • 5
  • 18