Comparing Integers (I think)

Hi,

I can't figure out what I'm missing. I'm running a query to see if there are any streams recording on my DVR before starting a scripted update. I'm guessing that it is viewing $VIDEO as a string instead of an int. I've tried everything I saw on google but it still comes back as $VIDEO is greater than 0, then sends me an email saying that it was delayed because there are 0 stream(s) recording.

I removed one of the t's in http due to link restriction.

#!/bin/bash
#Check to make sure we aren't recording
DATA=$(curl -sq htps://172-16-99-170.88e7a0e0e4e44af5b00a87c5b0baea80.plex.direct:32400/media/subscriptions/scheduled?X-Plex-Token=XXXXXXXXXXXXXXX)
declare -i VIDEO=$(echo "$DATA" | grep '<MediaGrabOperation' | grep 'status="inprogress"'|wc -l)
if (( $Video > 0 ))
then
Path=$1
Version=${Path##*/}
echo $Path >> /home/rhysers/log.txt
ssh plex@172.16.99.170 -p 778 -o StrictHostKeyChecking=no "rm /tmp/plex*"
ssh plex@172.16.99.170 -p 778 "wget -P /tmp/ $Path" >> /home/rhysers/log.txt
ssh plex@172.16.99.170 -t -p 778 "sudo dpkg -i /tmp/$Version" >> /home/rhysers/log.txt
curl "htp://127.1.2.3:8181/tautulli/api/v2?apikey=XXXXXXXXXXXXXXXXXXXXX&cmd=notify&notifier_id=5&subject=Plex+Update+Completed&body=Completed+Update+Cycle+to+$Version+on+$(date +%Y-%m-%d@%H:%M:%S)"
else
curl "htp://127.1.2.3:8181/tautulli/api/v2?apikey=XXXXXXXXXXXXXXXXXXXXX&cmd=notify&notifier_id=5&subject=Plex+Update+DELAYED&body=Update+Delayed+because+there+is+$VIDEO+item(s)+recording"
fi

VIDEO != Video

1 Like

ZOMG how did I miss that.
#hangsHeadInShame

------ Post updated at 09:23 AM ------

OK, so while that certainly wasn't helping, it also didn't fix it. I still get Delayed because there is 0 stream(s) recording.

#!/bin/bash

#Check to make sure we aren't recording
DATA=$(curl -sq https://172-16-99-170.88e7a0e0e4e44af5b00a87c5b0baea80.plex.direct:32400/media/subscriptions/scheduled?X-Plex-Token=XXXXXXXXXXXXXX)
declare -i VIDEO=$(echo "$DATA" | grep '<MediaGrabOperation' | grep 'status="inprogress"'|wc -l)

if (( $VIDEO > 0 ))
then
Path=$1
Version=${Path##*/}
echo $Path >> /home/rhysers/log.txt
ssh plex@172.16.99.170 -p 778 -o StrictHostKeyChecking=no "rm /tmp/plex*"
ssh plex@172.16.99.170 -p 778 "wget -P /tmp/ $Path" >> /home/rhysers/log.txt
ssh plex@172.16.99.170 -t -p 778 "sudo dpkg -i /tmp/$Version" >> /home/rhysers/log.txt
curl "http://127.1.2.3:8181/tautulli/api/v2?apikey=XXXXXXXXXXXXXXXXXX&cmd=notify&notifier_id=5&subject=Plex+Update+Completed&body=Completed+Update+Cycle+to+$Version+on+$(date +%Y-%m-%d@%H:%M:%S)"
else
curl "http://127.1.2.3:8181/tautulli/api/v2?apikey=XXXXXXXXXXXXXXXXXX&cmd=notify&notifier_id=5&subject=Plex+Update+DELAYED&body=Update+Delayed+because+there+is+$VIDEO+item(s)+recording"
fi

Add set -x as the second line in your script and rerun.
what's the output of your curl ?
Please use code tags when posting.

Ok, the first curl to see if there is a recording going on is over 800 lines, you can view it here: Curl Response - Pastebin.com

The part I think you might be looking for at the end is this:

+ declare -i VIDEO=0
+ ((  0 > 0  ))
+ curl 'http://127.1.2.3:8181/tautulli/api/v2?apikey=e74b2a5a44054bab9c760ace16ac7310&cmd=notify&notifier_id=5&subject=Plex+Update+DELAYED&body=Update+Delayed+because+there+is+0+item(s)+recording'
{"response": {"message": "Notification sent.", "data": {}, "result": "success"}}rhysers@mypi:/etc/updatePlex $

Also, am I not using code tags correctly? I thought I was and I can see it treat my code differently than standard text.

Thanks!

------ Post updated at 10:20 AM ------

Holy shampoo my logic statement is backwards. Hold on while i fix it and try it again.
I guess it "worked" when $VIDEO was >0 because I was actually evaluating $Video, which was 0.
Thanks for working through this with me.

------ Post updated at 10:23 AM ------

That fixed it! Thanks for the help!

your nested grep ( grep '<MediaGrabOperation' | grep 'status="inprogress"' ) returns no matches given your sample curl output.
I'd suggest start off by capturing the output of curl in a file and debug the script statically first.

At this moment that's exactly what it should do. There are no recordings in progress so it should return 0. If there were recordings in progress there would be that many matches. If there are 0 matches then it should proceed with the update. If there are matches then it doesn't because the update would interrupt the recording.
I fixed the logic to If $VIDEO < 1
Everything works now.

Because the 0>0 was not true it ran the else branch.

You can make it more efficient like this

VIDEO=$(
  curl -sq https://172-16-99-170.88e7a0e0e4e44af5b00a87c5b0baea80.plex.direct:32400/media/subscriptions/scheduled?X-Plex-Token=XXXXXXXXXXXXXX |
  egrep -c '<MediaGrabOperation .* status="inprogress"'
)

Also it is more precise because it requires the status=... to follow the <MediaGrabOperation

Also, it might be more efficient NOT to store >800 lines of curl output in a variable to be echo-ed|grep-ed and wc-ed later.
I'd do it in one piped statement and store the count in a variable to be compared later.
My $.02