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 88.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.
    • cyperghostC
      cyperghost @meleu
      last edited by cyperghost

      @meleu I also have no clue why the thing won't catch up.
      I tried to load inotifywait throug rc.local and it worked.
      As I changed the monitored file I get a note and the process inotifywait finished.

      I've made some tests with @lostless (I was giving instructions via IRC) and he was able to turn off his raspi with echo 1 > /sys..., but not with the switch pressing.

      It would be interesting to see what is happening internal through the GPIO if you press a button. Maybe it's better do use an more "advanced" bibliothek like wiring pi. Because with wPi you can detect flanks. But that's the suggestion I made by using an driver to detect keypresses.

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

        @cyperghost Just to share some bash coding knowledge, I would like to make some comments about your bash coding philosophy :)

        • the bash scripting language is an interpreted programming language
        • if you google about "compiled vs intepreted language" and you realize that interpreted languaged tends to be slower.
        • type man bash on your terminal, go the end of the bash man page and look at the first sentence in the BUGS section. ;-)

        Why is @meleu trying to convince me that bash is slow if he loves bash?

        Well, I love bash because it is widely used. If it wasn't for that I would have migrated to zsh (which is even more powerfull), but let's not go deep into this nerdy stuff...

        I'm talking all those things because of your very good practice of checking if everything is really fine before running a command. In other languages, if you don't do these checkings, the program may crash. But when coding bash scripts (or any other shell language) it's a better yet practice to take advantage of every chance you have to optimize things.

        I'm talking specifically for this part:

        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
        

        You're making bash check if the runcommand.info exists, but sed already does that.
        You're getting the emulator's PID with pgrep, but pkill won't kill anything if there isn't a process matching the pattern.

        That's why I would change all the logic above with this here:

        emupid="$(sed -n 4p /dev/shm/runcommand.info | tr -d '\\"' | tr '^$[]*.()|+?{}' '.')"
        [[ -n "$emu_command" ]] && pkill -f "${emupid}" && sleep 9
        

        That's it. :-)

        Cheers.

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

          @meleu Me after reading that whole post
          0_1501613667625_b69a7a0f-c51a-4d8b-ab8e-27144bb02dbe-image.png

          Creator of the Radiocade: https://retropie.org.uk/forum/topic/6077/radiocade

          Backlog: http://backloggery.com/lilbud

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

            @lilbud this topic was created exactly to avoid this reaction on other people's topics. :-)

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

              @lilbud You're welcome to ask and post things affecting bash :)

              lilbudL 1 Reply Last reply Reply Quote 0
              • lilbudL
                lilbud @cyperghost
                last edited by

                @cyperghost OK, I have very little experience with code and zero experience with bash.

                Where in the hell do I start?

                Creator of the Radiocade: https://retropie.org.uk/forum/topic/6077/radiocade

                Backlog: http://backloggery.com/lilbud

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

                  @lilbud at prompt. Type echo "Hello World". ;-)

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

                    @meleu So...echo is like a print command?

                    Creator of the Radiocade: https://retropie.org.uk/forum/topic/6077/radiocade

                    Backlog: http://backloggery.com/lilbud

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

                      @lilbud
                      You start with

                      cd ~
                      touch hello.sh && chmod +x hello.sh && echo 'echo -e "Hello World\nThis is my 1. code in bash"' > hello.sh
                      ./hello.sh
                      
                      meleuM 1 Reply Last reply Reply Quote 1
                      • cyperghostC
                        cyperghost @meleu
                        last edited by

                        @meleu Upss..
                        Are we not in lesson #2?

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

                          @cyperghost Me after reading your tutorial:
                          confused

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

                            @meleu This was my reaction

                            0_1501614701833_7a38a99e-b1bb-4017-921f-63ad9b585696-image.png

                            Creator of the Radiocade: https://retropie.org.uk/forum/topic/6077/radiocade

                            Backlog: http://backloggery.com/lilbud

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

                              @meleu The interesting part is this [[ "$(pgrep -P $emupid)" ]] && kill $(pgrep -P $emupid) it worked flawless in my testing. I just called a bash within a bash and they were properly detected!

                              Mausberry shutdown script v 1.58

                              #!/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.58)
                              # 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.58 08/02/17 - generel method: Use PPID to detect child PIDs now (SCUMMVM fix)
                              # 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
                                  [[ "$(pgrep -P $emupid)" ]] && kill $(pgrep -P $emupid)
                                  kill $emupid
                                  sleep 4
                              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.58)
                              
                              sudo poweroff
                              fi
                              done
                              
                              meleuM 2 Replies Last reply Reply Quote 1
                              • meleuM
                                meleu @cyperghost
                                last edited by

                                @cyperghost OMG, this -P is a key for a strong solution. Will post something when the kids go to bed! ;)

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

                                  @meleu You would rather use pkill -P
                                  as pgrep and pkill are using same synthax
                                  But I stay to the basics and this is the PID number :D

                                  I think it's now your turn to rearange and optimize the code a bit. You can also check your outfindings with the ES PID Detector - it now offers PIDs detected via PPID - it's a serious helper for such usecases.

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

                                    @cyperghost
                                    actually there's no need for what I was thinking. Your solution is really nice. :)

                                    Maybe some code polishing can be made, but it worked flawlessly on my tests with ScummVM. Nice finding, bro!

                                    • 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

                                      @meleu Thank you mate. Was a nice piece of work anyway but pathed with pitfalls.
                                      You should write some more tutorials - could be a good guidance.
                                      But for now ... good night

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

                                        @cyperghost said in shell scripting topic:

                                        Was a nice piece of work anyway but pathed with pitfalls.

                                        You gave me an idea!
                                        pitfall

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

                                          @cyperghost
                                          This is logically the same script, I just cleaned up the code (and bumped the version to v1.59): https://ghostbin.com/paste/adsrw

                                          And this one is the PID detector: https://ghostbin.com/paste/f8tmt

                                          Cheers!

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

                                            Mausberry Shutdown Script 1.595

                                            by @meleu and @cyperghost

                                            Final version for PowerBlock users is here!

                                            Mausberry users may get development to v1.7x

                                            #!/bin/bash
                                            # End Emulationstation if condition of running binary is true (v1.59)
                                            # 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 // cyperghost
                                            # v1.2 07/23/17 - Some small improvments, easier to maintain, removed echo, removed else branch // cyperghost
                                            # v1.5 07/27/17 - Great step to exit ES even if emulators is running by runcommand.sh are started // meleu
                                            # v1.55 07/29/17 - all kudos go to @meleu for his alltime genious RegEx hack! // meleu
                                            # v1.56 07/30/17 - All emulators will be detected. // meleu
                                            # v1.58 08/02/17 - generel method: Use PPID to detect child PIDs now (ScummVM fix) // cyperghost
                                            # v1.59 08/03/17 - nothing new, just polishing the code // meleu
                                            # v1.595 11/14/17 - Inserted newest emucall detection // meleu
                                            # 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! 
                                            
                                            #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 true; do
                                                power="$(cat /sys/class/gpio/gpio$GPIOpin1/value)"
                                                if [[ "$power" == 0 ]]; then
                                                    sleep 1
                                                else
                                                    # emucall="$(sed -n 4p /dev/shm/runcommand.info | tr -d '\\"' | tr '^$[]*.()|+?{}' '.')"
                                                    emucall="$(sed '4!d; s/\([\\"]\|[[:alnum:]_]\+=[^ ]* \)//g; s/[][(){}^$*.|+? ]/\\&/g' /dev/shm/runcommand.info)"
                                                    if [[ -n "$emucall" ]]; then
                                                        emupid="$(pgrep -f "$emucall" | tr '\n' ' ')"
                                                        pkill -P "$(echo $emupid | tr ' ' ',')"
                                                        kill "$emupid"
                                                        sleep 4
                                                    fi    
                                            
                                                    espid="$(pgrep -f "/opt/retropie/supplementary/.*/emulationstation([^.]|$)")"
                                                    if [[ -n "$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
                                            
                                                    sudo poweroff
                                                fi
                                            done
                                            
                                            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.