nicnut210
Posts: 8
Joined: Tue Oct 16, 2018 3:12 pm

if/else statement to run gui or no gui?

Sun Nov 18, 2018 7:53 pm

Hi,

Upon booting up my Pi launches a shell script that opens up a python menu that lets me open up Pure Data files (music/sound software). Here is the code:

#!/bin/bash

cd /home/pi/pdrpi
(sleep 10 && sudo python pd_menu.py) &


I mostly run my Pi in a headless manner so I want to start using Pure Data without the gui, because it’s supposed to run better. So now I have two different possible python menus, one with gui and one with nogui

Is it possible to do a true/false test, or an if else test to see if something is plugged into the HDMI port of the Pi?

when I run tvservice -s in terminal with a monitor in the HDMI port I get:

state 0x12000a [HDMI CEA (4) RGB lim 16:9], 1200x720 @60.00Hz, progressive

and

state 0x40001 [NTSC 4:3], 720x480 @60.00Hz, interlaced

if nothing is in the HDMI.

Is the following a good if/else way to achieve this in my shell script?

value=$( tvservice -s “state 0x12000a [HDMI CEA (4) RGB lim 16:9], 1200x720 @60.00Hz, progressive")
if [ $value -eq 1 ]
then
cd /home/pi/pdrpi
(sleep 10 && sudo python pd_menu.py) &

else
cd /home/pi/pdrpi
(sleep 10 && sudo python pd_nogui_menu.py) &


where pd_nogui_menu.py is the menu with nogui. Also, are the spaces, indents important in shell scripts like in python? Where the spacing has to be perfect?

Thank you.

Nick

Roken
Posts: 289
Joined: Sun Dec 31, 2017 4:35 pm
Location: UK

Re: if/else statement to run gui or no gui?

Sun Nov 18, 2018 8:29 pm

You are confusing yourself. You can have a full desktop running without being plugged in to a display, you just can't see it. If you always boot the same way, and you have a GUI when a monitor is connected, then you always have a GUI.
Headless PI. OMG, someone cut it's head off. Oh, hang on. it didn't have one to start with.

nicnut210
Posts: 8
Joined: Tue Oct 16, 2018 3:12 pm

Re: if/else statement to run gui or no gui?

Sun Nov 18, 2018 9:04 pm

Hi Roken,

I understand that a full desktop is running even though a monitor is not plugged in.

I can run Pure Data with a gui or withot a gui. If I am using the Pi headless I want to run Pure Data with no gui becuase that runs smoother and all the gui stuff slows down the software. I want the Pi to determine when it boots up if something is plugged into the HDMI port and lauch the program accordingly. Right now I always use Pure Data with a gui even if I'm using the Pi headless and I don't want to do that.

Does this make sense? I hope I explained it OK.

thanks, Nick

Roken
Posts: 289
Joined: Sun Dec 31, 2017 4:35 pm
Location: UK

Re: if/else statement to run gui or no gui?

Sun Nov 18, 2018 9:36 pm

Then you would probably be better with the minimal install (without X), which can be entirely headless. Out of 4 Pis, I only have two that have a GUI, and one of them doesn't launch it unless I tell it to manually. The other is retroPi, which would be kinda useless without an Xserver.
Headless PI. OMG, someone cut it's head off. Oh, hang on. it didn't have one to start with.

nicnut210
Posts: 8
Joined: Tue Oct 16, 2018 3:12 pm

Re: if/else statement to run gui or no gui?

Sun Nov 18, 2018 10:04 pm

HI Roken,

Well I use my Pi some of the time with the monitor and some of the time without the monitor. That's why I am asking about this if/else statement. it needs to be able to do both. Let me know what you think about the code in my first post and if that will work.

Thanks, Nick

nicnut210
Posts: 8
Joined: Tue Oct 16, 2018 3:12 pm

Re: if/else statement to run gui or no gui?

Wed Nov 21, 2018 3:56 am

Hey everyone.

I giving this a bump. I never found out if this is how this code should look, or even if it's possible to do if/then statements in a shell script.

Is that possible?

if so is this the best way to execute what I am trying to do?

value=$( tvservice -s “state 0x12000a [HDMI CEA (4) RGB lim 16:9], 1200x720 @60.00Hz, progressive")
if [ $value -eq 1 ]
then
cd /home/pi/pdrpi
(sleep 10 && sudo python pd_menu.py) &

else
cd /home/pi/pdrpi
(sleep 10 && sudo python pd_nogui_menu.py) &

Also is there a way to abbreviate "state 0x12000a [HDMI CEA (4) RGB lim 16:9], 1200x720 @60.00Hz, progressive" to just 0x12000 in this code?

Also, just to be clear, I use this Pi BOTH headless, and with a monitor. I want to be able to use it both ways and automatically launch Pure Data software accordingly. I'd rather not get into why I am doing it this way as that is another issue.

Thanks Nick

Return to “Beginners”