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.9k 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.
    • 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
                                • meleuM
                                  meleu
                                  last edited by

                                  Here is a proof:

                                  $ time find /usr -type f -print -quit
                                  /usr/i686-w64-mingw32/bin/ar.exe
                                  
                                  real    0m0.266s
                                  user    0m0.000s
                                  sys     0m0.140s
                                  
                                  $ time find /usr -type f | head -1
                                  /usr/i686-w64-mingw32/bin/ar.exe
                                  
                                  real    0m2.093s
                                  user    0m0.140s
                                  sys     0m0.403s
                                  

                                  (Yeah, I'm not using an actual Linux system. It's Cygwin running on Windows. But the test is valid anyway.)

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

                                    Nice find! I'm away on a business trip for the next two weeks and had to leave my Pi at home so I am hoping to find a way to mess with some scripts while it's inaccessible.

                                    I have Googled emulating a Pi in a VM but it doesn't seem like anyone's done that, just approximated things with installing Debian.

                                    📷 @obsidianspider

                                    1 Reply Last reply Reply Quote 1
                                    • Nathan1031982N
                                      Nathan1031982
                                      last edited by

                                      @obsidianspider
                                      This is right up my ally. I actually want to do something similar to this, but with something like a 20x2 LCD to just display the system and game title.
                                      However, I might want to get an OLED screen for displaying the information.
                                      I might ask you for some help setting up my own script..
                                      My only thing is that I will then need to make a custom case to hold the Pi, as well as the screen and keep it all clean.

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

                                        @Nathan1031982 The script he posted above gives a good direction to your script.
                                        The runcommand wiki can give some inspiration too: https://github.com/retropie/retropie-setup/wiki/runcommand#runcommand-onstart-and-runcommand-onend-scripts

                                        • Useful topics
                                        • joystick-selection tool
                                        • rpie-art tool
                                        • achievements I made
                                        1 Reply Last reply Reply Quote 1
                                        • F
                                          Finch106
                                          last edited by Finch106

                                          This is a very old topic, but I wanted to throw my script in so you can edit it to your liking. I don't have an Adafruit display, so I had to do some tinkering but you've all done the heavy lifting. Here are the minor edits for using a 3.5" display with fbi. Make sure you have the system.jpg images in the correct system rom folders.:

                                          # /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="/opt/retropie/configs/all/emulationstation/downloaded_images/$system/$system.jpg"
                                          fi
                                          
                                          # run the python script to display the image
                                          sudo fbi -T 2 -d /dev/fb1 -noverbose -a "$img"
                                          
                                          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.