Bash (shell script)

Hi,

I am trying to create a Game Launcher in bash (shell script) for retroarch. I have some of the code already completed, but it turns out I want to make it more efficient. I would like to shorten the code so that instead of having a ‘menu’ to start the retroarch core, I would like to remove the menu, and I would like to allow the user to select any retroarch supported rom file from an array and when they select a supported rom file, the correct emulator will launch it automatically. Can someone guide me to getting that working?

Here’s my current code:

Please note, you can probably tell, I am no bash expert by any means, but I should be able to follow tutorials and piece things together…

#!/bin/bash

config="$HOME/.config/retroarch/retroarch.cfg"

core_snes="$HOME/.config/retroarch/cores/snes9x_libretro.so" core_nes="$HOME/.config/retroarch/cores/mesen_libretro.so" core_gb="$HOME/.config/retroarch/cores/sameboy_libretro.so" core_n64="$HOME/.config/retroarch/cores/parallel_n64_libretro.so"

case “$1” in *.fds) core="$core_nes" ;; *.nes) core="$core_nes" ;; *.smc) core="$core_snes" ;; *.sfc) core="$core_snes" ;; *.gb) core="$core_gb" ;; *.gbc) core="$core_gbc" ;; *.z64) core="$core_n64" ;; *.n64) core="$core_n64" ;;

esac

clear

PS3=’ Please enter your choice: ’ options=(“Play Nintendo Game” “Play Super Nintendo Game” “Quit”) COLUMNS=0 select opt in “${options[@]}” do case $opt in “Play Nintendo Game”) nes_roms="$HOME/.config/retroarch/roms/Nintendo/Nes/" file=zenity --file-selection --filename="$nes_roms*" --file-filter=" "*"" --title="Select Nintendo Game" retroarch -L $core_nes “$file” --fullscreen --config=$config; clear exit ;; “Play Super Nintendo Game”) snes_roms="$HOME/.config/retroarch/roms/Nintendo/Snes/" FILE=zenity --file-selection --filename="$snes_roms" --file-filter=" "*"" --title="Select Super Nintendo Game"

        retroarch -L $core_snes "$FILE" --fullscreen --config=$config;

clear exit

        ;;
    "Option 3")
   "you chose choice $REPLY which is $opt"
        ;; 
    "Quit")

clear break ;; *) echo “invalid option $REPLY”;;

esac

done

Brian

Neat idea! And your probably fine if every system’s rom you use has a different extension. But I am unsure what you do when multiple systems use the same extension like .bin .iso or .chd?

I am not real familiar with bash either, but maybe it’s possible to select a core based on a part of the pathname instead?

Hi Sloth,

Thanks for the comment! I am not at that stage of having the same extensions, but yes, I do believe the last part of your message is possible, I will have to experiment with the code.

1 Like

I would recommend taking a look at my lobby viewer, which allows me to join rooms through a terminal.

It’s Python not Bash, but converting between shell scripts and Python is simple enough.

I use RetroArch’s JSON playlists to fetch CRCs and ROM paths; an INI file is used to configure the viewer and to set up links between system/cores and playlists.

I was thinking of something like this pseudo code:

creating an arrary to store all the supported retroarch extensions, so for example:

extArrary would have something like this array:

nes smc gen n64

and then I would create an if then else statement that would go something like:

if ext=nes then

retroarch -L nes_core “$FILE” --fullscreen --config=$config;

else if ext=smc then retroarch -L nes_core “$FILE” --fullscreen --config=$config;

note about $FILE that would be a zenity call that would open a file select dialog so any rom can be selected.

actual zenity call:

FILE=zenity --file-selection --filename="$HOME/.config/retroarch/roms/" --file-filter=" "*"" --title="Select a Game"

Could someone help me create the array AND the if statement to work? I just haven’t had any luck yesterday, and I am running out (away from computer for a while) so I would be so greatful if someone could find a way to get the array and if statement to work so that when I choose a rom file from the zenity file select box, the correct core will be loaded and run the game.

I have been messing around, but still hitting pitfalls with the code, here’s is what I have been thinking would work, but I have come up short:

Basically, I am trying to load retroarch based on file selected and if the file selected is .nes then it will load the nes retroarch core. That’s how I am trying to go about this script. If some one could enlighten me with a resolution to my code I would greatly appreciate it.

#!/bin/bash

Retroarch Config Path

config="$HOME/.config/retroarch/retroarch.cfg"

Core Selection

core_nes="$HOME/.config/retroarch/cores/mesen_libretro.so" core_snes="$HOME/.config/retroarch/cores/snes9x_libretro.so"

Retroarch Supported Extensions

ext=(“nes smc”)

File Selection (Game Selection)

FILE=zenity --file-selection --filename="$HOME/.config/retroarch/roms/" --file-filter=" "*"" --title="Select a Game"

Case Selection for Retroarch Supported Extensions

case “$ext” in

*.fds) core="$core_nes" ;; *.nes) core="$core_nes" ;; *.smc) core="$core_snes" ;; *.sfc) core="$core_snes" ;; *.gb) core="$core_gb" ;; *.gbc) core="$core_gbc" ;; *.z64) core="$core_n64" ;; *.n64) core="$core_n64" ;;

esac

If Statement not working

if $ext==nes then retroarch -L $core_nes “$FILE” --fullscreen --config=$config;

else if $ext==smc then retroarch -L $core_snes “$FILE” --fullscreen --config=$config;

fi

exit

Hi. I do this kind of thing already and you might be interested into it. First I had a Bash script too, but quickly did a more complete program in Rust language for use on Linux. It’s exactly the same idea to run games based on their extension. And for files with extension that are used by multiple cores such as .chd can have special treating to look for in what folder it is. There is a user config file for settings:

And you can set the file extension (let’s say .smc) default application to enjoy (that’s the name of the program) in the file explorer you are using. Then you don’t even need to use the commandline or scripting, just double click the game in your filemanager and it would play the game.