• Recent
  • Tags
  • Popular
  • Home
  • Docs
  • Register
  • Login
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.
  • O
    obsidianspider @meleu
    last edited by obsidianspider 9 Jan 2016, 18:23 1 Sept 2016, 17:22

    @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

    M 1 Reply Last reply 1 Sept 2016, 17:32 Reply Quote 0
    • M
      meleu @obsidianspider
      last edited by meleu 1 Sept 2016, 17:32

      @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 1 Sept 2016, 17:36

        @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.

        O 1 Reply Last reply 1 Sept 2016, 17:38 Reply Quote 0
        • O
          obsidianspider @RetroResolution
          last edited by 1 Sept 2016, 17:38

          @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 1 Sept 2016, 17:39 Reply Quote 0
          • R
            RetroResolution @obsidianspider
            last edited by 1 Sept 2016, 17:39

            @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
            • O
              obsidianspider
              last edited by obsidianspider 9 Feb 2016, 11:51 1 Sept 2016, 18:09

              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

              M 1 Reply Last reply 1 Sept 2016, 18:12 Reply Quote 1
              • M
                meleu @obsidianspider
                last edited by 1 Sept 2016, 18:12

                @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
                O 1 Reply Last reply 1 Sept 2016, 18:45 Reply Quote 0
                • O
                  obsidianspider @meleu
                  last edited by obsidianspider 9 Jan 2016, 19:46 1 Sept 2016, 18:45

                  @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

                  M 1 Reply Last reply 1 Sept 2016, 19:31 Reply Quote 0
                  • M
                    meleu @obsidianspider
                    last edited by meleu 9 Jan 2016, 20:40 1 Sept 2016, 19:31

                    @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
                    O B 2 Replies Last reply 1 Sept 2016, 21:10 Reply Quote 1
                    • O
                      obsidianspider @meleu
                      last edited by 1 Sept 2016, 21:10

                      @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

                      M 1 Reply Last reply 1 Sept 2016, 21:17 Reply Quote 0
                      • M
                        meleu @obsidianspider
                        last edited by 1 Sept 2016, 21:17

                        @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
                        • B
                          BuZz administrators @meleu
                          last edited by BuZz 9 Jan 2016, 22:34 1 Sept 2016, 21:34

                          @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

                          O 1 Reply Last reply 2 Sept 2016, 00:44 Reply Quote 0
                          • O
                            obsidianspider @BuZz
                            last edited by 2 Sept 2016, 00:44

                            @BuZz Thanks! I updated the script to use find

                            📷 @obsidianspider

                            M 2 Replies Last reply 2 Sept 2016, 04:11 Reply Quote 0
                            • M
                              meleu @obsidianspider
                              last edited by 2 Sept 2016, 04:11

                              @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
                              • M
                                meleu @obsidianspider
                                last edited by 2 Sept 2016, 04:15

                                @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
                                O 1 Reply Last reply 2 Sept 2016, 10:26 Reply Quote 0
                                • O
                                  obsidianspider @meleu
                                  last edited by 2 Sept 2016, 10:26

                                  @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 2 Sept 2016, 21:48

                                    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
                                    • M
                                      meleu
                                      last edited by 25 Sept 2016, 06:56

                                      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
                                      • M
                                        meleu
                                        last edited by 25 Sept 2016, 07:04

                                        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
                                        • O
                                          obsidianspider
                                          last edited by 25 Sept 2016, 13:07

                                          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
                                          • 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.

                                            This community forum collects and processes your personal information.
                                            consent.not_received