Switch Between Application Suites on your Multipurpose Pi with Ease
In this article, which builds upon the preceding post Multipurpose Raspberry Pi: Installing a Media, Gaming, PC Replacement, I demonstrate the addition of a menu to allow easy switching between application suites.
Whilst the solution overall is relatively straightforward, I’ve gone into some depth in order that this post can serve as a general how-to guide, providing some insights into Bash shell scripts, including: installing scripts using the desktop GUI or command line tools; how the code file is made executable; automatically running a script after login, and after programs are exited by the user; and other related concepts.
Topics Covered in this Post
- Menu Design Requirements and Goals
Introducing the Menu Script
– a Bash script handling user input and launching of the appropriate application suite
– re-displaying the menu upon exit from the selected suite
Installing the Menu Script
– either using the Raspbian desktop, or
– via a remote SSH session
Marking the Script File as Executable
– either using the File Manger GUI, or the command line
Adding Code to Launch the Menu
– updating the .bashrc script to automatically run the menu when the Raspberry Pi first logs in
– preventing the menu from loading when Bash terminal windows are opened in the desktop, or when a remote SSH session is launched
Optional Command Line Alias
– adding a Bash command alias to allow simple manual launching of the menu script
Optionally Booting with Numlock On
– start the Bash terminal with Numlock enabled to aid menu option selection
Menu Design Requirements and Goals
As noted in the post detailing the installation of the Multipurpose Pi system, the RetroPie emulator system cannot be launched from the X-Windows Raspbian desktop GUI. This restriction forced the requirement that the Pi boot to the text-mode Bash terminal, which in turn required launching of a chosen application suite via typed commands:
startx for the Raspbian desktop,
kodi for the Kodi Media Center, and
emulationstation for RetroPie.
Whilst this wasn’t a major issue, I wanted a method to launch a given suite without having to type commands at the Bash terminal – ideally via a menu-driven selection system. Whilst this necessarily must be text-based, it is friendlier than facing a blank command prompt upon booting.
Furthermore, I wanted the menu to automatically run when the Pi boots to the terminal, and also to be re-displayed whenever the user closes one of the selected application suites (using Kodi‘s power button, Emulation Station‘s Quit submenu, or Raspbian desktop’s main menu Shutdown option):
Most solutions which automatically run a script after the Pi is logged in, whether manually or automatically, will run the code each and every time a Bash shell is launched, whether this is as a terminal emulator under the Raspbian desktop, or a remote SSH session. This was not desirable, given that I make heavy use of secure shell remote connections, and use the command line often, even when using the desktop; thus I wanted to limit the menu to launching under a very narrow set of circumstances.
To achieve these design aims, several components are combined to provide the end result, however the overall solution is reasonably straightforward.
Introducing the Menu Script
The complete menu script is located on the page entitled Multipurpose Menu Selector Script: Source Code on this site.
A slightly modified version of the script, allowing a default option to be launched after a configurable timeout period has elapsed, can be found on the page entitled Multipurpose Menu Selector Script: Source Code V2 Including Timeout Default on this site.
Both versions are presented on separate pages to aid cutting and pasting into a local file whilst following this guide.
The code for both versions is modified from an original example by user Alex Lucard on askubuntu.com
In the script a
Case statement handles the user input, and either launches an application – Raspbian desktop, Kodi, or RetroPie – or Reboots or performs a Shutdown of the Raspberry Pi.
If an application is launched, the code in the
Case statement also recursively runs the menu script. This is done so that the menu is present if the user exits the application (as opposed to rebooting or shutting down the machine). Note that the full path and script name
/home/pi/tools/selector.sh are used, and should be updated if you rename and / or relocate the code.
The reason that this works is that the Bash terminal that is launched as the Pi completes the boot process remains running even after one of the main applications is launched; the selected application, such as RetroPie, is launched as a separate process on a different thread, independently of the terminal.
Installing the Menu Script
The menu is mainly implemented as a Bash script, which is installed by copying and pasting the code into a new file on the Raspberry Pi. There are a couple of ways to achieve this, using either GUI tools from the Raspbian desktop, or text-mode utilities via a remote connection from another machine.
Create a new text document using a tool such as Leafpad or gedit.
– Leafpad is installed as standard and can be accessed via the Raspbian desktop Menu, selecting Accessories, then Text Editor.
– gedit is a more sophisticated, but easy to use, text editor useful for simple programming and script editing, and supports syntax colourisation. To install gedit, run the following command from a terminal:
sudo apt-get install gedit
Copy the complete menu script from the page entitled Multipurpose Menu Selector Script: Source Code
– click the link to open the page in a separate browser tab, than copy the code, and paste it into the text editor
Save the file into the
home/pi/toolsdirectory, naming it as
– if the
toolsdirectory does not exist, create it from the terminal using the command:
mkdir /home/pi/tools, or from within the GUI File Manager
Note: please ensure that the file does not have a
.txtextension appended to it by the text editor during the save process
- Connect to the Raspberry Pi from another machine (Windows, Linux, Android, Mac, etc.) using a remote shell client programme, such as PuTTY
Open a new file using the in-built Nano text editor (or your editor of choice). When launching Nano use the
sudoprefix command to ensure the new file can be written back to the file system:
sudo nano selector.sh
- Select and copy all of the script code from the page entitled Multipurpose Menu Selector Script: Source Code
- Paste the code into the newly created file on the Pi (if using PuTTY, a right-click of the mouse will copy the contents of the Windows clipboard into the shell).
- Save the file. If using Nano, press
Control + 'X', then
Mark the Script File as Executable
Whichever approach is used to create the script, it must be marked as an executable file so that the code within can be run by the operating system. This can be done via the GUI File Manager, or at the command line:
To Mark the File as Executable Using the GUI:
– select File Manager from the Accessories menu within the main desktop Menu
selector.sh, right click the file, select Properties, then the Permissions tab, and finally set Execute to Anyone
To Mark the File as Executable Using the Command Line:
– open a Bash console using the desktop Menu, selecting Accessories, then Terminal
– run the following command:
sudo chmod +x /home/pi/tools/selector.sh
Adding Code to Launch the Menu
The code which runs the script to display the menu is placed in the
.bashrc file, which is located in the home directory of the user Pi.
As this system file already exists, it is not possible to simply open a GUI text editor from the Raspbian desktop menu, add the necessary code, and save the file. This is due to the fact that the operating system launches programs, including text editors, with the permissions of the currently logged-in user; the Pi user account does not have permission to save changes to this system file.
One option is to use a Bash terminal window to launch a text editor with super-user privileges:
sudo leafpad /home/pi/.bashrc
Another option is to use the text-mode nano editor:
sudo nano /home/pi/.bashrc
Whichever option you select, edit the
/home/pi/.bashrc script, adding the following code to the end of the file:
########################## ## auto launch the Menu ## ########################## # launch Menu only when main (non gui) terminal loads (once per boot) # do not run Menu when shell is launched within GUI, or from SSH # TERM value = 'linux' when at main non-gui terminal # and 'xterm' for a shell launched from the GUI if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then #echo Session is remote / ssh. # : is a no-op : else if [ "$TERM" = "linux" ]; then /home/pi/tools/selector.sh #run the menu script fi fi
Please note the absolute path and file name in the line:
/home/pi/tools/selector.sh #run the menu script
This must reflect the actual location and name used for the menu script file, if these have been altered from those used in the preceding guide.
A couple of further notes on the above code may be of interest for those unfamiliar with bash shell scripting in particular, or programming in general. In order to meet the design goals of only running the menu script when the Raspberry Pi is first booted and the login process completes, but not when a Bash terminal is launched within the Raspbian desktop, or remotely via an SSH session, a couple of system variables are interrogated.
.bashrc script is automatically launched by the operating system whenever a bash terminal is launched. The
$TERM variable holds the value
x-term if terminal has been launched under X-Windows desktop, and the value
linux if at the main text-mode console. The code only runs the selector.sh menu script if
$TERM indicates that the desktop is not running.
When a remote connection is made to the Raspberry Pi,
$TERM still returns the value
linux, as SSH connections are non GUI text-mode sessions. For this reason a additional check is made to see if either of the system variables
$SSH_TTY exist (the actual values aren't of consequence). If one or both is present, the Bash terminal is being created by a remote session, in which case we prevent the menu script from being executed.
Optional Bash Command Alias to Launch the Menu
To aid manual launching of the menu from a terminal, a command alias can be created which saves typing the full path and name of the menu script.
Edit the existing
sudo nano ~/.bash_aliases
Add the following to end of the file, and save the changes:
alias menu='bash ~/tools/selector.sh'
Please note: the
~ (tilde) symbol represents the home directory of the logged in user, and is the equivalent of
The menu can now be run from the command line, regardless of the current directory, by typing:
This is more convenient than the alternative:
Optionally Boot with Numlock On
To aid use of the menu, add a line of code to the keyboard configuration file to enable Numlock in the terminal when the Pi first boots.
Open the config file:
sudo nano /etc/kbd/config
Locate and uncomment the following entry by removing the
# from the start of the line (or create the line if it is not present), then save the changes:
Please note that the Raspbian desktop will still default to switching Numlock off; whilst there is a utility available, numlockx, to address this, it causes (harmless) errors to be displayed in the console.
A Labour of Love
- Links: Raspberry Pi and Gaming Emulation via RetroPie
- Multipurpose Raspberry Pi: Installing a Media, Gaming, PC Replacement
- Don’t Fear The Command Line: Raspbian Linux Shell Commands and Tools – Part 1: Just the Basics
- Navigating the Raspberry Pi’s File System. Raspbian Linux Shell Commands and Tools – Part 2