aristosv
Posts: 159
Joined: Mon Dec 08, 2014 7:47 pm

a way to consolidate multiple if statements?

Wed Feb 20, 2019 2:29 pm

I have a RaspberryPi, running Raspbian, playing an audio stream using omxplayer. If the first stream is offline, I want it to fall back to a secondary stream, and if both streams are down, I want it to play from a local music folder. So I created three services, each one handling a task. (2 streams and 1 local audio)

I also wrote a bash script with a bunch of if statements, which detects if the streams are up or down, and starting/stopping each service as necessary. But it seems to me this script can be made much shorter and probably more efficient.

Can you give me an idea of the logic I must follow to avoid all these if statements if possible? Is there a more tidy/structured way of handling this?

Thanks

Code: Select all

#!/bin/bash

# operations if relay stream is up
if [ $(curl -s --head -w %{http_code} $relaystreamurl -o /dev/null) -eq 200 ]
 then
  echo relay stream is up

 if [ $(sudo systemctl is-active localmedia.service) == active ]
  then
   echo stopping local media
   sudo systemctl stop localmedia.service
   sudo systemctl disable localmedia.service > /dev/null 2>&1   
 fi
 
 if [ $(sudo systemctl is-active localstream.service) == active ]
  then
   echo stopping local stream
   sudo systemctl stop localstream.service
   sudo systemctl disable localstream.service > /dev/null 2>&1   
 fi
 
 if [ $(sudo systemctl is-active relaystream.service) == inactive ]
  then
   echo starting relay stream
   sudo systemctl start relaystream.service
   sudo systemctl enable relaystream.service > /dev/null 2>&1   
 fi
fi

# operations if relay stream is down
if [ $(curl -s --head -w %{http_code} $relaystreamurl -o /dev/null) -eq 502 ]
 then
  echo relay stream is down

 if [ $(sudo systemctl is-active relaystream.service) == active ]
  then
   echo stoppping relay stream
   sudo systemctl stop relaystream.service
   sudo systemctl disable relaystream.service > /dev/null 2>&1   
 fi
 
 if [ $(sudo systemctl is-active localstream.service) == inactive ]
  then
   echo starting local stream
   sudo systemctl start localstream.service
   sudo systemctl enable localstream.service > /dev/null 2>&1   
 fi
 
 if [ $(sudo systemctl is-active localmedia.service) == active ]
  then
   echo stopping local media
   sudo systemctl stop localmedia.service
   sudo systemctl disable localmedia.service > /dev/null 2>&1   
 fi
fi

# operations if relay and local streams are down
if [ $(curl -s --head -w %{http_code} $relaystreamurl -o /dev/null) -eq 502 ] && [ $(curl -s --head -w %{http_code} $localstreamurl -o /dev/null) -eq 502 ]
 then
  echo relay and local streams are down
  
 if [ $(sudo systemctl is-active relaystream.service) == active ]
  then
   echo stoppping relay stream
   sudo systemctl stop relaystream.service
   sudo systemctl disable relaystream.service > /dev/null 2>&1   
 fi
 
 if [ $(sudo systemctl is-active localstream.service) == active ]
  then
   echo stopping local stream
   sudo systemctl stop localstream.service
   sudo systemctl disable localstream.service > /dev/null 2>&1   
 fi
 
 if [ $(sudo systemctl is-active localmedia.service) == active ]
  then
   echo starting local media
   sudo systemctl start localmedia.service
   sudo systemctl enable localmedia.service > /dev/null 2>&1   
 fi
fi 

User avatar
B.Goode
Posts: 10163
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: a way to consolidate multiple if statements?

Wed Feb 20, 2019 2:41 pm

If it works, and you understand it, is there anything 'broken' that needs to be 'fixed'?

But maybe the bash shell case statement might have a use here?

aristosv
Posts: 159
Joined: Mon Dec 08, 2014 7:47 pm

Re: a way to consolidate multiple if statements?

Wed Feb 20, 2019 2:46 pm

I understand your point, but I’m not really a fan of the “if it’s not broken, don’t fix it” philosophy. I like messing around with things :)

I’ll look into “case”, see if I can find some examples. Thanks for your suggestion.

User avatar
B.Goode
Posts: 10163
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: a way to consolidate multiple if statements?

Wed Feb 20, 2019 2:56 pm

aristosv wrote:
Wed Feb 20, 2019 2:46 pm
I understand your point, but I’m not really a fan of the “if it’s not broken, don’t fix it” philosophy. I like messing around with things :)

I’ll look into “case”, see if I can find some examples. Thanks for your suggestion.

Certainly there is a school of thought that 'refactoring' a working solution to improve and clarify it is A Good Thing. There is even a textbook by Martin Fowler on the topic.

And there is a regular poster in these forums whose .signature is something like "If it's not broken fiddle with it until it is" !

Good luck with your learnings.

Return to “General discussion”