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

      @cyperghost and all shell lovers, let's use this topic to share our thoughts/questions/knowledge about this subject.

      (I mean, let's stop messing other people's topics with bash code! :-) )

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

        @cyperghost just continuing what we were talking in another topic...

        Here is a nice doc about parameter substitution: http://www.tldp.org/LDP/abs/html/parameter-substitution.html

        Sometimes you can use those tricks instead of cut, sed, etc...

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

          @meleu Hey mate, thank you for creating this thread. I will use it if there is any future questions.

          1 Reboot script

          #!/bin/bash
          
          #this is the GPIO pin connected to the lead on switch labeled OUT
          GPIOpin1=23
          
          #this is the GPIO pin connected to the lead on switch labeled IN
          GPIOpin2=24
          
          echo "$GPIOpin1" > /sys/class/gpio/export
          echo "in" > /sys/class/gpio/gpio$GPIOpin1/direction
          echo "$GPIOpin2" > /sys/class/gpio/export
          echo "out" > /sys/class/gpio/gpio$GPIOpin2/direction
          echo "1" > /sys/class/gpio/gpio$GPIOpin2/value
          while [ 1 = 1 ]; do
          power=$(cat /sys/class/gpio/gpio$GPIOpin1/value)
          if [ $power = 0 ]; then
          sleep 1
          else
          
          # End Emulationstation if condition of running binary is true (v1.56)
          # v1.0 07/21/17 by cyperghost - Inital run 
          # v1.1 07/22/17 - Added chown command to set right user permission for creating es-shutdown
          # v1.2 07/23/17 - Some small improvments, easier to maintain, removed echo, removed else branch 
          # v1.5 07/27/17 - Great step to exit ES even if emulators is running by runcommand.sh are started
          # v1.55 07/29/17 - all kudos go to @meleu for his alltime genious RegEx hack!
          # v1.56 07/30/17 - All emulators will be detected. This is a full functional code equal to developing v1.7
          # v1.7 is in work flow - This will be cleaner and better coded and is better to maintain
          # I just checked with SSH command - it saved my metadata! Maybe you need to extend sleeptimer!
          # greetings @pjft for his famous favorits and @meleu for his RegEx sniplets and his constant help! 
          
          espid="$(pgrep -f "/opt/retropie/supplementary/.*/emulationstation([^.]|$)")"
          if [[ -e "/dev/shm/runcommand.info" ]]; then
              emupid="$(sed -n 4p /dev/shm/runcommand.info | tr -d '\\"' | tr '^$[]*.()|+?{}' '.')"
              emupid="$(pgrep -f "$emupid")" 
          fi
          
          if [ "$emupid" ]; then
              kill $emupid && sleep 9
          fi    
          
          if [ "$espid" ]; then
             touch /tmp/es-shutdown && chown pi:pi /tmp/es-shutdown
             kill $espid
             exit
          fi
          # End Emulationstation if condition of running binary is true (v1.56)
          
          sudo poweroff
          fi
          done
          

          I think that is ready for official testing.
          I made a clean finish of this - hope you find it good arranged.
          I think it's better to give it a clear style. In head all needed PIDs are obtained.
          If the PID value is true then kill used emulator and ES or just ES :)
          Thank your for your help I see my progresses in using bash.

          EDIT:
          @hansolo77 detected an error
          I forget to break the loop so I added exit command :)
          Thanks to him - I setted sleeptimer to 9 seconds during emulator is running. It may be need to be extended :)

          meleuM 1 Reply Last reply Reply Quote 1
          • cyperghostC
            cyperghost
            last edited by cyperghost

            2 Garbage: PID Detector

            May be usefull for later investigation or this sniplet will help some people out (to escape emulators or to find out if there is an emulator currently running)

            #!/bin/bash
            # Show PIDS from ES and Retroarch
            # Garbage product from mausberry shutdown script
            # v1.0 07/27/17 ES PID and all emulator calls via runcommand.sh will be showen
            # v1.1 07/29/17 Check file existance of runcommand.info first and then use the genious RegEx scniplet introduced by @meleu
            # v1.2 08/02/17 Check PPID and gives hint to terminate
            # greetings @pjft for his famous favorits and @meleu for the RegEx sniplet 
            
            espid="$(pgrep -f "/opt/retropie/supplementary/.*/emulationstation([^.]|$)")"
            if [[ -e /dev/shm/runcommand.info ]]; then
                emupid="$(sed -n 4p /dev/shm/runcommand.info | tr -d '\\"' | tr '^$[]*.()|+?{}' '.')"
                emupid="$(pgrep -f "$emupid")" 
            fi
            
            if [[ "$emupid" ]]; then
                    echo -e "\nAn emulator is currently running!\nIt's PID is: "$emupid
                    if  [[ "$(pgrep -P $emupid)" ]]; then
                        echo -e "\n\nThis emulator got's a child process!\nYou have to kill,too!\nIt's PID is: "$(pgrep -P $emupid)
                    fi
                else
                    echo -e "\nThere seems to be no emulator is currently running or\nit wasn't called by runcommand.sh!\n\n\n"
            fi
            
            
            if [ "$espid" ]; then 
                    echo -e "\nEmulationStation is runnuning!\nES's PID is: "$espid
                else
                    echo -e "\nIt seems you are not in ES. Don't wanna play?\n\n\n"
            
            meleuM 1 Reply Last reply Reply Quote 0
            • meleuM
              meleu @cyperghost
              last edited by

              @cyperghost do you plan to learn python? It's a language where you are forced to use correct indentation, otherwise the logic doesn't work. :-)

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

                @meleu Yes python is on my todo list. It seems to be a great language with a bright future (I hope it will not end like delphi).

                1 Reply Last reply Reply Quote 1
                • cyperghostC
                  cyperghost
                  last edited by cyperghost

                  @meleu
                  This can also be usefull for GPIO control
                  https://github.com/larsks/gpio-watch

                  gpio-watch uses debouncing on switches and automatically starts scripts on keypress. I compiled it - it's a small binary and it makes GPIO keyevents really really easy!


                  Out from Readme.md

                  Watch switches connected to pins 21 only
                  
                  gpio-watch -e switch 21
                  
                  If you were to press a button connected to pin 21, gpio-watch would attempt to run:
                  
                  /etc/gpio-scripts/21
                  
                  
                  Watch switches connected to pins 21, 22, and 23:
                  
                  gpio-watch -e switch 21 22 23
                  
                  If you were to press a button connected to pin 23, gpio-watch would attempt to run:
                  
                  /etc/gpio-scripts/23 23 0
                  
                  That is, the event script is called with both the pin number and the current pin value 
                  to permit a single script to handle multiple events.
                  
                  meleuM 1 Reply Last reply Reply Quote 1
                  • meleuM
                    meleu @cyperghost
                    last edited by

                    @cyperghost Unfortunately I don't have any electronics knowledge to test this GPIO stuff on my raspi... :/

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

                      @cyperghost said in Mausberry Shutdown Script Doesn't Save Metadata:

                      Can you please exchange your lines

                          emu_command="$(sed -n 4p /dev/shm/runcommand.info)"
                          [[ -n "$emu_command" ]] && pkill -f "${emu_command%% *}" && sleep 12 # the value of 12 was suggested by @lostless 
                      

                      with this one?

                      emupid="$(sed -n 4p /dev/shm/runcommand.info)"                                #v1.5 @meleu readout 4th line command.info 
                      emupid="$(pgrep -f "${emupid%% *}")"                                          #v1.5 optain PID emucall by runcommand.sh
                      
                      if [ "$emupid" ]; then                                                        #v1.5 check correct PID > avoid sleeptimer if shutdown from ES
                          kill $emupid && sleep 10
                      fi    
                      

                      It is really the better way. Because you check value of command.info ... this value will also be active if you are back in ES so your [[ -n "$emu_command" ]] is always true and the sleep timer will used in every situation. Even if an emulator is not active. So this would be the last version called 1.6 release :)

                      Look this:

                      [prompt]$ pkill -f inexistent_process && echo blablabla
                      [prompt]$ pkill -f inexistent_process ; echo $?
                      1
                      [prompt]$ 
                      

                      Got it?

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

                        --- erased ---

                        1 Reply Last reply Reply Quote 0
                        • cyperghostC
                          cyperghost
                          last edited by cyperghost

                          @meleu
                          About the nerdy stuff :)
                          https://retropie.org.uk/forum/topic/11750/mausberry-shutdown-script-doesn-t-save-metadata/122

                          Why you prefer [[ -n ]] rather if [ "$pid" ]
                          The value in your bracket is always true because runcommand.info will be available and contains values of last emucall even after you go back to ES view (caroussel-mode) and so you try to kill an emulator that is not active and you activate sleep timer.

                          I extract PID and check if the PID is "true" if not then the value is untrue and so I'm pretty sure there is no emulator actually running and proceed with quitting ES. I'm also sure that 12 seconds aren't needed :) Maybe for a Pi 1/0 :)

                          See my garbage PID script

                          PS You should dive into the experience of GPIOs :)

                          1 Reply Last reply Reply Quote 0
                          • cyperghostC
                            cyperghost @meleu
                            last edited by

                            @meleu said in shell scripting topic:

                            Look this:

                            [prompt]$ pkill -f inexistent_process && echo blablabla
                            [prompt]$ pkill -f inexistent_process ; echo $?
                            1
                            [prompt]$ 
                            

                            Got it?

                            Yes for sure.... got it!
                            So the && addition breaks if one condition is not true!
                            Okay got it :) But I prefer not to use pkill if it is not needed. I would always obtain PID via pgrep and then use kill-command. I think that is just philosophic

                            Thank you very much

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

                              @cyperghost said in shell scripting topic:

                              Why you prefer [[ -n ]] rather if [ "$pid" ]

                              The initial reason was because I like to follow RetroPie's Shell Style Guide, but there's a more reasonable answer here.

                              The value in your bracket is always true because runcommand.info will be available and contains values of last emucall

                              I think you already noticed that this isn't true, right?

                              you try to kill an emulator that is not active and you activate sleep timer.

                              It's also not true.

                              This code:

                              command1 && command2 && command3
                              

                              is equivalent to

                              if command1; then
                                 if command2; then
                                    command3
                                 fi
                              fi
                              

                              The use of && and || is useful when you have to test something and then execute a single command.

                              More info about this syntax here: http://mywiki.wooledge.org/BashGuide/TestsAndConditionals#Control_Operators_.28.26.26_and_.7C.7C.29

                              Cheers!

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

                                @cyperghost said in shell scripting topic:

                                Yes for sure.... got it!
                                So the && addition breaks if one condition is not true!

                                I think I answered myself but you explained it now more didactic

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

                                  @cyperghost let's get back to the nerdy topic :-)

                                  you said in Mausberry Shutdown Script Doesn't Save Metadata:

                                  #!/bin/bash
                                  emucall=$(sed -n 4p /dev/shm/runcommand.info)
                                  emupid=${emucall#* }
                                  pos=$(expr ${#emucall} - ${#emupid})
                                  $emupid=$(pgrep -f -n ${emucall:0:$pos})
                                  kill $emupid
                                  

                                  The code sniplet above should still do the job as it was introduced for a few days and was titled "complex" - it isn't ;)
                                  It's robust string operation and searches for first occurence for space and then kills the latest process :)

                                  Look this example using your approach:

                                  [PROMPT]$ # look how RetroPie calls ScummVM
                                  [PROMPT]$ sed -n 4p /dev/shm/runcommand.info 
                                  bash /home/meleu/RetroPie/roms/scummvm/+Start\ ScummVM.sh "ft"
                                  [PROMPT]$ emucall=$(sed -n 4p /dev/shm/runcommand.info)
                                  [PROMPT]$ emupid=${emucall#* }
                                  [PROMPT]$ pos=$(expr ${#emucall} - ${#emupid})
                                  [PROMPT]$ echo "${emucall:0:$pos}"
                                  bash 
                                  

                                  Now with the other approach:

                                  [PROMPT]$ emucall=$(sed -n 4p /dev/shm/runcommand.info)
                                  [PROMPT]$ echo "${emucall%% *}"
                                  bash
                                  

                                  See? Exactly the same result.

                                  But the bug I reported isn't about this nerdy stuff... I'll expand on the next post... :-)

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

                                    @meleu Yes...
                                    But I also wrote... to use the -n switch for pkill or pgrep then only the latests process will be killed or PID obtained and all is good :)

                                    My approch about the string operation is following
                                    If you search for the first occurence of " /" then you can be sure you got the name of the running process. I think I must take a deeper look to sed or even awk scripting.

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

                                      @cyperghost the bug I reported here happens because the pattern for pgrep -f and pkill -f is a regular expression, and that 4th line on runcommand.info is not a regex and can have characters with special meanings in a regex context.

                                      In my ScummVM example:

                                      bash /home/meleu/RetroPie/roms/scummvm/+Start\ ScummVM.sh "ft"
                                                                             ^
                                                                             |
                                                     This plus '+' sign is "confusing" pgrep/pkill
                                      
                                      

                                      So this is the solution I've found (note: a dot . in a RegEx context means "any character or nothing"):

                                      emu_command="$(sed -n 4p /dev/shm/runcommand.info | tr -d '\\"' | tr '^$[]*.()|+?{}' '.')"
                                      # explaining the crazy pipes above:
                                      # 1. get 4th line of runcommand.info (aka "emulator command")
                                      # 2. delete backslash '\' character
                                      # 3. replace every character that has a special meaning in a regex context with a dot '.'
                                      
                                      • Useful topics
                                      • joystick-selection tool
                                      • rpie-art tool
                                      • achievements I made
                                      cyperghostC 1 Reply Last reply Reply Quote 0
                                      • meleuM
                                        meleu @cyperghost
                                        last edited by

                                        @cyperghost said in shell scripting topic:

                                        But I also wrote... to use the -n switch for pkill or pgrep then only the latests process will be killed or PID obtained and all is good :)

                                        In the ScummVM example your approach would return the last bash process. I can't see how you can assure that the most recent bash call is the ScummVM caller one.

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

                                          @meleu Okay then I was on the wrong way. I thought pkill killed ALL bash calls because it just extracted "bash" so my intentention was to kill only latest bash call and therefore use the coded bash sniplet
                                          Sorry I didn't test with SCUMMVM and so did not realize that there is a problem with the RegEx call :)

                                          I'm really glad you find out!


                                          EDIT:

                                          About latest bash. No that can't be 100% true but 99%
                                          Is SCUMMVM the only caveeat?

                                          meleuM 1 Reply Last reply Reply Quote 0
                                          • cyperghostC
                                            cyperghost
                                            last edited by cyperghost

                                            @meleu Thank you in advance :) I know you want 100% working solutions and I'm thankfull for your immediate help :) So I see ... when is version 1.7 ready?
                                            It's unbelievable how much energy you invest in such small sniplets.

                                            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.