RetroPie forum home
    • Recent
    • Tags
    • Popular
    • Home
    • Docs
    • Register
    • Login

    shell scripting topic

    Scheduled Pinned Locked Moved Ideas and Development
    shellshell scriptprogramming
    191 Posts 10 Posters 81.6k 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.
    • hiulitH
      hiulit @meleu
      last edited by

      @meleu Ok, you're right! I didn't explain myself clearly, hehe! It sounds weird not wanting to hardcode sudo -H -u pi but wanting to hardcode home="/home/pi" instead :P Nonsense!

      Now, I'm at home and I'm testing the script in an actual Raspberry Pi with RetroPie and I have a couple of things to comment:

      • I've added sudo ~/es-fun-facts-splashscreens/es-fun-facts-splashscreens.sh & and it seems to work when I launch /etc/rc.local from the terminal, but not when the system boots up.

      • I've tried adding -H -u pi and it doesn't seem to work.

      This code worked perfectly! :D I didn't had imagemagick installed on this Raspberri Pi and the warning pop up, nice!

      if ! which convert > /dev/null; then
          echo "ERROR: The imagemagick package is not installed!"
          echo "Please, install it with 'sudo apt-get install imagemagick'."
          exit 1
      fi
      

      But I didn't had feh installed and the warning didn't pop up :(

      if [[ -n "$DISPLAY" ]] && ! which feh  > /dev/null; then
          echo "ERROR: The feh package is not installed!"
          echo "Please, install it with 'sudo apt-get install feh'."
          exit 1
      fi
      

      Maybe instead of && it should be || ?
      Or maybe remove [[ -n "$DISPLAY" ]] and just leave if ! which feh > /dev/null ?

      Anyways, the script works perfectly by itself and when launching /etc/rc.local from the terminal, but not when it's supposed to be launched in /etc.rc.local at startup.

      My little contributions to the RetroPie project:

      • Shell-Script-Boilerplate
      • Fun-Facts-Splashscreens
      • Limit-Last-Played-Games
      1 Reply Last reply Reply Quote 0
      • hiulitH
        hiulit @cyperghost
        last edited by

        @cyperghost Ok, but how would we know which is "the best hit"? Based on what?

        My little contributions to the RetroPie project:

        • Shell-Script-Boilerplate
        • Fun-Facts-Splashscreens
        • Limit-Last-Played-Games
        cyperghostC 1 Reply Last reply Reply Quote 0
        • cyperghostC
          cyperghost @hiulit
          last edited by cyperghost

          @hiulit Well the xml in the root of the theme or keywords like "mainfont=font-1.ttf".... That are just suggestions.

          Or you write an identifier.txt so you say: theme, font

          # Example file
          Carbon, system.ttf
          IO, mainfont.ttf
          
          hiulitH 1 Reply Last reply Reply Quote 1
          • hiulitH
            hiulit @cyperghost
            last edited by

            @cyperghost don't get me wrong! I really appreciate the feedback :)

            I think we can try to find a generic method to get fonts if we squeeze our minds together

            My little contributions to the RetroPie project:

            • Shell-Script-Boilerplate
            • Fun-Facts-Splashscreens
            • Limit-Last-Played-Games
            1 Reply Last reply Reply Quote 0
            • hiulitH
              hiulit
              last edited by hiulit

              What do you think about this code?
              I want to add a line in /etc/rc.local to launch a script.
              Also, I want to check if that line is already in /etc/rc.local.
              Additionally, I want to check if /etc/rc.local has the needed exit 0 at the end (in case the user removed it).
              The latter is totally optional.

              # If the user mistakenly removed "exit 0", add it at the end of "/etc/rc.local".
              function check_safe_exit_boot_script() {
                  if [[ "$(tail -n1 /etc/rc.local)" != "exit 0" ]]; then
                      sed -i -e '$i \exit 0\' "/etc/rc.local"
                  fi
              }
              
              # Get the line that launches script at boot.
              # $SCRIPT_DIR="/home/pi/es-fun-facts-splashscreens".
              function check_boot_script() {
                  grep "$SCRIPT_DIR" "/etc/rc.local"
              }
              
              # Add the line to launch script in "/etc/rc.local"
              function add_boot_script() {
                  sed -i -e '$i \\n'"$home"'/es-fun-facts-splashscreens/es-fun-facts-splashscreens.sh &\n' "/etc/rc.local"
                  check_safe_exit_boot_script
              }
              
              # Check if the line that launches the script is already in "/etc/rc.local"
              # If it's not, add it.
              [[ -z "$(check_boot_script)" ]] && add_boot_script
              

              My little contributions to the RetroPie project:

              • Shell-Script-Boilerplate
              • Fun-Facts-Splashscreens
              • Limit-Last-Played-Games
              cyperghostC 1 Reply Last reply Reply Quote 1
              • cyperghostC
                cyperghost @hiulit
                last edited by cyperghost

                @hiulit Well I think as setup.sh this is possible. Withwget path/to/your/github/es-funfacts.sh a (half)automated installation shouldbe possible ;)

                About the correct font ... there is surly a generic solution. But what about adding just a path to a font via command line parameter?
                So you have es-funfacts.sh /path/to/font.ttf

                1. Find automatically the correct font
                2. If 1 fails, try to use Carbon font (as default theme)
                3. If there is a parameter available then 1 and 2 are ignored
                1 Reply Last reply Reply Quote 0
                • hiulitH
                  hiulit
                  last edited by hiulit

                  Hello my fellow nerds! :P

                  What do you think about this code?

                  First piece of code is this one:

                  function is_arm() {
                      uname -m | grep -q "arm"
                  }
                  

                  I'm trying to detect if the device is arm based. If not, I'm gussing it's x86 based .

                  And then I'm using this other piece of code to get the screen resolution.
                  arm based systems and x86based systems have different methods to get the screen resolution. And that's what I've come up with.

                  function get_screen_resolution_x() {
                      if is_arm; then
                          fbaset -s | cut -dx -f1
                      else
                          xdpyinfo | awk -F '[ x]+' '/dimensions:/{print $3}'
                      fi
                  }
                  
                  
                  function get_screen_resolution_y() {
                      if is_arm; then
                          fbaset -s | cut -dx -f2
                      else
                          xdpyinfo | awk -F '[ x]+' '/dimensions:/{print $4}'
                      fi
                  }
                  

                  My little contributions to the RetroPie project:

                  • Shell-Script-Boilerplate
                  • Fun-Facts-Splashscreens
                  • Limit-Last-Played-Games
                  mituM meleuM 2 Replies Last reply Reply Quote 1
                  • mituM
                    mitu Global Moderator @hiulit
                    last edited by

                    @hiulit If your intention is to detect whether the user is running under a desktop environment (X) or not, then I wouldn't use the architecture of the kernel to determine that, but something more simple like testing if the DISPLAY env is available or something like this.
                    Running on an ARM board doesn't mean the user's session is not running under Pixel and vice-versa.

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

                      @hiulit said in shell scripting topic:

                      First of all, we would be able to be more helpful if you say what exactly you want to do. Anyways, here are some thoughts about what you asked...

                      I'm trying to detect if the device is arm based. If not, I'm gussing it's x86 based .

                      I would use the system.sh scriptmodule for this or at least get some inspiration from its code.

                      Example using the existent code:

                      . $HOME/RetroPie-Setup/scriptmodules/helpers.sh
                      . $HOME/RetroPie-Setup/scriptmodules/system.sh
                      get_platform
                      echo "$__platform"
                      

                      Or get inspiration from the function starting on this line of code to write your own function.

                      The $__platform variable is one of these:
                      rpi1 rpi2 rpi3 odroid-c1 odroid-c2 imx6 odroid-xu tinker x86

                      (...) get the screen resolution.

                      I agree with @mitu above.

                      If your intention is to detect whether the user is running under a desktop environment (X) or not, then I wouldn't use the architecture of the kernel to determine that, but something more simple like testing if the DISPLAY env is available or something like this.

                      • Useful topics
                      • joystick-selection tool
                      • rpie-art tool
                      • achievements I made
                      hiulitH 1 Reply Last reply Reply Quote 1
                      • G
                        grant2258 Banned
                        last edited by

                        probably a useless one but might come in helpful if people dont know use -- in a command line if it has special characters and bash will ignore the special characters.

                        pi@retropie:~ $ touch -- +1.txt
                        pi@retropie:~ $ less -- +1.txt
                        pi@retropie:~ $ less +1.txt
                        Missing filename ("less --help" for help)

                        1 Reply Last reply Reply Quote 0
                        • hiulitH
                          hiulit
                          last edited by

                          Thanks to all! You were very useful :)

                          Sorry @meleu (and @mitu ) if I wasn't more precise.

                          The problem I'm facing is that I want to get the screen resolution to create launching images accordingly to that size, but the way I found to get it differs from, for example, my Raspberry Pi (arm) and my VM running Pixel (x86).

                          I think I like @meleu 's idea of using $__platform.

                          My little contributions to the RetroPie project:

                          • Shell-Script-Boilerplate
                          • Fun-Facts-Splashscreens
                          • Limit-Last-Played-Games
                          1 Reply Last reply Reply Quote 0
                          • hansolo77H
                            hansolo77
                            last edited by

                            I'm intrested in developing a script that will generate a custom list for my GameEx High Scoring Competition. GameEx is a frontend I use on my PC, and our forums have created a competition for it's users. I'm actually the primary moderator for that forum. We have a utility for keeping track of scores and generating forum tables. It's all nice and good. I came up with an idea to generate a map file, which allows you to create a "custom list" menu inside GameEx, so you can instantly get into a game that is available for posting high scores with. It works great. I was looking to do something like that for RetroPie too. My PC's keyboard no longer works, and the only way I can use that PC is through RemoteDesktop. However, I have all the games on my Pi, so why can't I just use that?

                            Here's a breakdown of what I need to accomplish:

                            • Upon turning on the system, as part of the boot process, have a script go out and download the updated list of games from a server. The utility we use to track scores already generates this list and uploads it somewhere, so I just need something to grab it. wget perhaps?
                              • This may not need to be done during EVERY bootup.. in fact it might not even work if there is no internet available.
                              • Maybe the script should just be available for manual launching? We only add new games every 2 months now.
                            • After getting the list, it needs edited to work with EmulationStation's custom list features, as by default the list is probably not directly usable.
                            • Save the edited list as a custom list file, in the appropriate folder for EmulationStation to pick it up.

                            For reservation, here is the map file:
                            http://gameinfo.hfc-essentials.com/hiscore/HiScore.map

                            And here is a MAME favorites ini file:
                            http://gameinfo.hfc-essentials.com/hiscore/HiScore.ini

                            I'm not sure which file would be easier to work with. But if you look at them, it should be easy enough to figure out the editing that needs to happen. Can detailed editing of the file be done automatically via a script? I wasn't sure so I thought I'd ask here. I also asked our developer of the tool we use to see if he can update the tool to also spit out a plain text list.. Looking to see what can happen, or happens. I'd just like a bit of hand holding, not really asking somebody to step in and do all the work (would be nice but I can't learn scripting without practice!).

                            What are your suggestions or recommendations?

                            Who's Scruffy Looking?

                            mituM 1 Reply Last reply Reply Quote 0
                            • mituM
                              mitu Global Moderator @hansolo77
                              last edited by mitu

                              @hansolo77 It can be done. This is how I'd do it.

                              1. Download the .map file (curl/wget) locally. Should be text only. Check for errors or if the file is empty and exit if so.

                              2. Read the file line by line, split it in col1 (zip name) and col2 (game name). For each col1:

                                • Look in the gamelist for the arcade system to search for col1.zip, either via grep or xmlstarlet. [1]
                                  The idea is to get the path to the ROM zip file as the value into path
                                • If you found the path, then check if arcade_folder_path/path exists and if so, add it to a list (list_rom).
                                • Repeat steps above if your rom might be in a different system (i.e. mame2003-libretro, fba) and nothing was found in the previous step.
                              3. At the end, walk over list_rom and produce a custom collection file in $HOME/.emulationstation/collections/custom-GameEx.cfg containing one line for each ROM path in the list. If a previous .cfg fie exists, back it up.

                              You should have to create the collection before running the script so you can choose in ES to make it visible, but once ES knows to show it, I think you only need to restart ES after each script run.

                              The above assumes you only have arcade ROMs, since the naming is standard. Make sure to lowecase the col1 in step 2 (I've seen that TMNT is uppercase, but the zip name should be lowercase and

                              [1] - get the path of rom using xmlstarlet and ROM name.

                              $ xmlstarlet sel -t -v "//game[contains(path,'1943.zip')]/path" .emulationstation/gamelists/arcade/gamelist.xml 
                              # outputs: 
                               ./1943.zip
                              
                              1 Reply Last reply Reply Quote 0
                              • hansolo77H
                                hansolo77
                                last edited by

                                I knew it could be done! :) Now to just decipher all that and make a script lol. I’m on vacation after next week, maybe I’ll try to work on it then!

                                Who's Scruffy Looking?

                                1 Reply Last reply Reply Quote 0
                                • hiulitH
                                  hiulit @meleu
                                  last edited by

                                  @meleu @mitu What I'd like to achieve is getting the screen resolution of the device that is/will be using Fun Facts! Splashscreens. And I've seen that it differs from different systems.

                                  My little contributions to the RetroPie project:

                                  • Shell-Script-Boilerplate
                                  • Fun-Facts-Splashscreens
                                  • Limit-Last-Played-Games
                                  meleuM 1 Reply Last reply Reply Quote 0
                                  • meleuM
                                    meleu @hiulit
                                    last edited by

                                    @hiulit said in shell scripting topic:

                                    @meleu @mitu What I'd like to achieve is getting the screen resolution of the device that is/will be using Fun Facts! Splashscreens. And I've seen that it differs from different systems.

                                    Uhmmm... maybe what you need is slightly different than just detect whether the user is running your script while in a x11 environment or not.

                                    If you need to discover if the emulators you installed/compiled on your RetroPie were for use on a X11 environment, I suggest you to try the isPlatform function. You can do something like this:

                                    . $HOME/RetroPie-Setup/scriptmodules/helpers.sh
                                    . $HOME/RetroPie-Setup/scriptmodules/system.sh
                                    if isPlatform "x11"; then
                                        echo "Your emulators runs on a X11 environment"
                                    else
                                        echo "Your emulators does NOT run on a X11 environment"
                                    fi
                                    

                                    Regarding resolution discovering techniques, you are surely ahead of me on this subject. :-)

                                    I hope it helps.

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

                                      @meleu My problem was that I had these xdpyinfo functions to get the screen resolution and they were working in my VM running Pixel, but when I tried them on the Raspberry Pi I got an error, something like: xdpyinfo: command not found. I tried sudo apt-get xdpyinfo but it didn't work, so I manage to found another way to get the screen resolution from a Raspberry pi, and that's where these fbset -s functions come from.

                                      So now I need to know when do I need to use one function or the other.

                                      What I have right now (and seems to be working) is:

                                      function get_system_platform() {
                                          . "$home/RetroPie-Setup/scriptmodules/helpers.sh"
                                          . "$home/RetroPie-Setup/scriptmodules/system.sh"
                                          get_platform
                                          echo "$__platform"
                                      }
                                      
                                      
                                      function get_screen_resolution_x() {
                                          if [[ "$(get_system_platform)" != "x86" ]]; then
                                              local screen_resolution
                                              screen_resolution="$(fbset -s | grep -o -P '(?<=").*(?=")')"
                                              echo "$screen_resolution" | cut -dx -f1
                                          else
                                              xdpyinfo | awk -F '[ x]+' '/dimensions:/{print $3}'
                                          fi
                                      }
                                      
                                      
                                      function get_screen_resolution_y() {
                                          if [[ "$(get_system_platform)" != "x86" ]]; then
                                              local screen_resolution
                                              screen_resolution="$(fbset -s | grep -o -P '(?<=").*(?=")')"
                                              echo "$screen_resolution" | cut -dx -f2
                                          else
                                              xdpyinfo | awk -F '[ x]+' '/dimensions:/{print $4}'
                                          fi
                                      }
                                      

                                      My little contributions to the RetroPie project:

                                      • Shell-Script-Boilerplate
                                      • Fun-Facts-Splashscreens
                                      • Limit-Last-Played-Games
                                      mituM 1 Reply Last reply Reply Quote 0
                                      • mituM
                                        mitu Global Moderator @hiulit
                                        last edited by

                                        @hiulit xdpyinfo is part of the x11-utils package (https://packages.debian.org/stretch/x11-utils), so it's not installed if you're not installing an X.org env (like in Raspbian Lite). But even if it is installed, it only works in a X.org session (desktop environment), otherwise it will return an error.

                                        hiulitH 1 Reply Last reply Reply Quote 0
                                        • hiulitH
                                          hiulit @mitu
                                          last edited by

                                          @mitu So then, would it be save to use something like this?

                                              if [[ $DISPLAY ]]; then
                                                  xdpyinfo blah, blah...
                                              else
                                                  do_something_for_none_X_org_session
                                              fi
                                          

                                          My little contributions to the RetroPie project:

                                          • Shell-Script-Boilerplate
                                          • Fun-Facts-Splashscreens
                                          • Limit-Last-Played-Games
                                          meleuM 1 Reply Last reply Reply Quote 0
                                          • meleuM
                                            meleu @hiulit
                                            last edited by

                                            @hiulit said in shell scripting topic:

                                            @mitu So then, would it be save to use something like this?

                                                if [[ $DISPLAY ]]; then
                                                    xdpyinfo blah, blah...
                                                else
                                                    do_something_for_none_X_org_session
                                                fi
                                            

                                            eh... did you read my last post above? I think it answers exactly what you asked on your last post.

                                            • Useful topics
                                            • joystick-selection tool
                                            • rpie-art tool
                                            • achievements I made
                                            hiulitH 1 Reply Last reply Reply Quote 0
                                            • 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.