RetroPie forum home
    • Recent
    • Tags
    • Popular
    • Home
    • Docs
    • Register
    • Login
    Please do not post a support request without first reading and following the advice in https://retropie.org.uk/forum/topic/3/read-this-first

    [SOLVED] Variables with runcommand-onstart.sh

    Scheduled Pinned Locked Moved Help and Support
    runcommandscriptingbash
    25 Posts 7 Posters 13.4k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • obsidianspiderO
      obsidianspider
      last edited by obsidianspider

      Now that I (kind of) have my secondary display working I was messing around with the runcommand-onstart.sh to display an image when you start a game, and to use runcommand-onend.sh to display another image when you exit a game. So far, so good, but what I'd like to do is pass the script a variable (the game name) so I can display a different image for each game being played. I tried searching but either I'm using the wrong keywords or I don't know what I'm looking for.

      my very simple test script is

      sudo python ~/Adafruit_Python_ILI9341/examples/imageparam.py ~/Adafruit_Python_ILI9341/examples/smw.jpg
      

      the ~/Adafruit_Python_ILI9341/examples/smw.jpg part is the location of the image I want to display with imageparam.py.

      If I could figure out how to get the ROM name from RetroPie I could use that to select a different image.

      Are there variables (ROM name, system, etc) that the runcommand scripts can use? If so, is there a list somewhere?

      📷 @obsidianspider

      meleuM R 2 Replies Last reply Reply Quote 0
      • meleuM
        meleu @obsidianspider
        last edited by meleu

        @obsidianspider

        These scripts are called with this line:

        bash "$script" "$system" "$emulator" "$rom" "$command" </dev/tty 2>>"$log"
        

        So the arg1 is the system, arg2 is the emulator, arg3 is the full path to the rom, and the arg4 is the full command line used to invoke the emulator with the ROM.

        And I think that redirecting messages to stderr is a good practice, so you can see it in runcommand.log later.

        • Useful topics
        • joystick-selection tool
        • rpie-art tool
        • achievements I made
        obsidianspiderO 1 Reply Last reply Reply Quote 1
        • obsidianspiderO
          obsidianspider @meleu
          last edited by obsidianspider

          @meleu

          Based on the script from the scraping images post

          system="$1"
          rom="$3"
          rom_bn="${rom##*/}"
          rom_bn="${rom_bn%.*}"
          

          it looks like rom_btn is the actual game name, but can you explain what's going on with the (I assume) regular expressions that are stripping out the path and extension?

          📷 @obsidianspider

          meleuM 1 Reply Last reply Reply Quote 0
          • meleuM
            meleu @obsidianspider
            last edited by meleu

            @obsidianspider

            it looks like rom_btn is the actual game name

            actually it is the rom filename excluding the full path and the extension. Let me explain:

            # get the full path filename of the rom
            rom="$3"
            
            # rom_bn receives $rom excluding everything from the first char to the last slash '/'
            rom_bn="${rom##*/}"
            
            # rom_bn receives $rom_bn excluding everything from the last char to the first dot '.'
            rom_bn="${rom_bn%.*}"
            

            You can find more info about this in the Advanced Bash-Scripting Guide, section "Manipulating Strings". The tricks I used here are in the subsection named "Substring Removal".
            http://tldp.org/LDP/abs/html/string-manipulation.html

            I hope it helps you! ;-)

            • Useful topics
            • joystick-selection tool
            • rpie-art tool
            • achievements I made
            1 Reply Last reply Reply Quote 2
            • R
              RetroResolution @obsidianspider
              last edited by

              @obsidianspider not wishing to side-track your thread, but can I ask what hardware you are using to obtain a second display? I use my Pi as a lightweight PC as well as a gaming system, and would find a second monitor very useful...

              If a post has helped you, please encourage the author by up-voting via the ^ icon located in the bottom-right corner.

              RetroResolution.com - Adventures in retro gaming on original hardware and via emulation with RetroPie on the Raspberry Pi.

              obsidianspiderO 1 Reply Last reply Reply Quote 0
              • obsidianspiderO
                obsidianspider @RetroResolution
                last edited by

                @meleu Thanks! That helps me understand what I'm trying to pass with these variables.

                @RetroResolution Right now I'm still learning how to get it working properly, but I'm using a 2.2" TFT I got from eBay.

                📷 @obsidianspider

                R 1 Reply Last reply Reply Quote 0
                • R
                  RetroResolution @obsidianspider
                  last edited by

                  @obsidianspider cool, thanks! Best of luck

                  If a post has helped you, please encourage the author by up-voting via the ^ icon located in the bottom-right corner.

                  RetroResolution.com - Adventures in retro gaming on original hardware and via emulation with RetroPie on the Raspberry Pi.

                  1 Reply Last reply Reply Quote 0
                  • obsidianspiderO
                    obsidianspider
                    last edited by obsidianspider

                    Woo hoo! It's working!

                    # /opt/retropie/configs/all/runcommand-onstart.sh
                    
                    # get the full path filename of the ROM
                    rom=$3
                    
                    # rom_bn receives $rom excluding everything from the first char to the last slash '/'
                    rom_bn="${rom##*/}"
                    
                    # rom_bn receives $rom_bn excluding everything from the last char to the first dot '.'
                    rom_bn="${rom_bn%.*}"
                    
                    # get the system name
                    system=$1
                    
                    # set the image file to the first result matching the ROM name - accounts for various possible file extensions
                    img="$(find "/opt/retropie/configs/all/emulationstation/downloaded_images/${system}" -type f -name "${rom_bn}-image.*" | head -1)"
                    
                    # check to see if there was a file found (length of the file name not zero), if not, use an image based on system
                    if [[ -z "${img}" ]];
                    then
                       img="/home/pi/Adafruit_Python_ILI9341/examples/$system.jpg"
                    fi
                    
                    # run the python script to display the image
                    sudo python ~/Adafruit_Python_ILI9341/examples/imageparam.py "$img"
                    

                    [Edit] Updated script to use @meleu 's line for looking for any file regardless of extension, used a different method to see if the file exists
                    [Edit 2] Updated syntax for checking for zero length strings and an updated find

                    Now back to getting the darn screen to turn on consistently…

                    📷 @obsidianspider

                    meleuM 1 Reply Last reply Reply Quote 1
                    • meleuM
                      meleu @obsidianspider
                      last edited by

                      @obsidianspider great, dude!
                      just a touch: there's no need for that else (img=$img) ;-)

                      • Useful topics
                      • joystick-selection tool
                      • rpie-art tool
                      • achievements I made
                      obsidianspiderO 1 Reply Last reply Reply Quote 0
                      • obsidianspiderO
                        obsidianspider @meleu
                        last edited by obsidianspider

                        @meleu Thanks for the tip on the else. I updated the script above.

                        Looks like I need to do a bit more tweaking with my "is there an image to display" part of the script.

                        I have some games with a .png extension for the image and instead of saying the file isn't found, it displays a blank screen instead of the default image and a few other games where there's a different image name than the ROM name (due to me tweaking after scraping) and things are hanging instead of displaying the default.

                        📷 @obsidianspider

                        meleuM 1 Reply Last reply Reply Quote 0
                        • meleuM
                          meleu @obsidianspider
                          last edited by meleu

                          @obsidianspider maybe this line can give you what you need:

                          img=$(ls -1 "/opt/retropie/configs/all/emulationstation/downloaded_images/${system}/${rom_bn}-image".* | head -1)
                          

                          [EDIT: the command is "ls dash one", not "ls dash el". ;-)]
                          [EDIT2: use double quotes to avoid problems with roms with spaces in file name.]

                          • Useful topics
                          • joystick-selection tool
                          • rpie-art tool
                          • achievements I made
                          obsidianspiderO BuZzB 2 Replies Last reply Reply Quote 1
                          • obsidianspiderO
                            obsidianspider @meleu
                            last edited by

                            @meleu That helped a lot. Things are working now. I've updated the script above.

                            Here's a short video of it working with a game that has art, a game with no art, and another game with art, always reverting back to the RetroPie image when on EmulationStation. I don't know if there's a way to detect what system is up on EmulationStation as you're browsing, but maybe I can look into that in the future.

                            📷 @obsidianspider

                            meleuM 1 Reply Last reply Reply Quote 0
                            • meleuM
                              meleu @obsidianspider
                              last edited by

                              @obsidianspider really cool man! I remember of somebody asking for this kind of trick here in the forum. And I think he would like to see it, but I can't remember his name...

                              • Useful topics
                              • joystick-selection tool
                              • rpie-art tool
                              • achievements I made
                              1 Reply Last reply Reply Quote 1
                              • BuZzB
                                BuZz administrators @meleu
                                last edited by BuZz

                                @meleu said in Variables with runcommand-onstart.sh:

                                @obsidianspider maybe this line can give you what you need:

                                img=$(ls -1 "/opt/retropie/configs/all/emulationstation/downloaded_images/${system}/${rom_bn}-image".* | head -1)
                                

                                [EDIT: the command is "ls dash one", not "ls dash el". ;-)]
                                [EDIT2: use double quotes to avoid problems with roms with spaces in file name.]

                                ls will throw an error if there is no matching filename - you can redirect stderr or alternatively use find img="$(find "/opt/retropie/configs/all/emulationstation/downloaded_images/${system}" -type f -name "${rom_bn}-image.*" | head -1)"

                                To help us help you - please make sure you read the sticky topics before posting - https://retropie.org.uk/forum/topic/3/read-this-first

                                obsidianspiderO 1 Reply Last reply Reply Quote 0
                                • obsidianspiderO
                                  obsidianspider @BuZz
                                  last edited by

                                  @BuZz Thanks! I updated the script to use find

                                  📷 @obsidianspider

                                  meleuM 2 Replies Last reply Reply Quote 0
                                  • meleuM
                                    meleu @obsidianspider
                                    last edited by

                                    @obsidianspider Your updated script with comments is really cool to share with others the bash tricks.

                                    I only would like to share two tips with you:

                                    1. The use of [[ ... ]] is preferred over [ ... ] or test ...; it reduces errors as no pathname expansion or word splitting takes place between [[ and ]] (I learned it in RetroPie Shell Style Guide)
                                    2. The common practice to check if a string is empty is to use [[ -z "$string" ]].
                                    • Useful topics
                                    • joystick-selection tool
                                    • rpie-art tool
                                    • achievements I made
                                    1 Reply Last reply Reply Quote 0
                                    • meleuM
                                      meleu @obsidianspider
                                      last edited by

                                      @obsidianspider
                                      Oh! I look more carefully: This line is wrong:

                                      img=$(find "/opt/retropie/configs/all/emulationstation/downloaded_images/${system}/${rom_bn}-image".* | head -1)
                                      

                                      What BuZz suggested is:

                                      img="$(find "/opt/retropie/configs/all/emulationstation/downloaded_images/${system}" -type f -name "${rom_bn}-image.*" | head -1)"
                                      
                                      • Useful topics
                                      • joystick-selection tool
                                      • rpie-art tool
                                      • achievements I made
                                      obsidianspiderO 1 Reply Last reply Reply Quote 0
                                      • obsidianspiderO
                                        obsidianspider @meleu
                                        last edited by

                                        @meleu

                                        Thanks for all the help, I've updated the script with the suggestions you gave.

                                        I'm learning a LOT from this community and I want to share what I've learned so someone else can make something even better with their own project. The comments help because a lot of the examples you see online don't explain what a particular thing is doing, they just say "do this" but not why you should do that.

                                        Now that I've got the screen working and finding images properly the next step is a pushbutton to control a script that toggles the backlight…

                                        📷 @obsidianspider

                                        1 Reply Last reply Reply Quote 1
                                        • R
                                          robertybob
                                          last edited by

                                          Looks interesting! We've had a Pi in a Gameboy, now we need someone to create their own DS-like console with the artwork displayed on the second screen (!)

                                          1 Reply Last reply Reply Quote 2
                                          • meleuM
                                            meleu
                                            last edited by

                                            Hey guys! Just sharing a find trick I've learned today...

                                            Using this:

                                            find /directory -type f -name "filename.*" -print -quit
                                            

                                            Is better than use this:

                                            find /directory -type f -name "filename.*" | head -1
                                            

                                            Benefit: the -quit makes find exit immediately after the first match. Well, if we want the first match only, there's no need to let the find keep looking for files after the first match.
                                            OBS.: when using -quit you have to use -print, otherwise the find won't print the file name.

                                            • Useful topics
                                            • joystick-selection tool
                                            • rpie-art tool
                                            • achievements I made
                                            1 Reply Last reply Reply Quote 1
                                            • First post
                                              Last post

                                            Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.

                                            Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.