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

      Answering your questions on this nerdy thread.

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

      @meleu
      About using variables? You see I'm avoiding these and use just a bunch of them...
      Is there any difference? Speed up? Memory usage?

      I know it's better for clear code reading.... but is that effcient?

      It was just for clear code reading. Each variable have a meaningful name. In such a small script the impact on performance is null. But to be honest, using emupid didn't cause any impact on code reading too. :)

      About avoiding variables and using the same command two or more times, that would cause impact on performance.

      Example:

      [[ "$(pgrep -P $emupid)" ]] && kill $(pgrep -P $emupid)
      

      Using a variable for $(pgrep -P $emupid) is preferable in this case.

      We (programmers) should always think where (and when) our code is supposed to be executed when deciding about "Memory Usage vs. Processing Usage". Some questions we should keep in mind when coding:

      • The system has a good amount of RAM?
      • The system has a good CPU power?
      • The system has usually several users/processes sharing resources?
      • How often will my code run?
      • How long will my code remain running?

      There are more questions, obviously.

      By the way, those last 2 question are the ones that bother me so much about that infinite loop! We are looping to check a file content every single second just to execute a command to SHUT DOWN THE WHOLE SYSTEM! In my head the thought is: "this script is wasting 99.99999% of its CPU time slice!". Specially because there are ways to make a script do nothing (just wait) while that file doesn't change!

      But... Well... I don't have that f!@#%ing mausberry switch, so there's no need for me to worry about it. Those guys who have it are happy with our current solution... Let's play some games! :D

      This subject would be a good nerdy chat while drinking some Caipirinhas when you come to Rio.

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

        Hi guys, sorry, wasn't here for a while.
        I've read @meleu last post and I just checked "things", as I'm also not fond of the infinite loop. That said, I don't use mausberry or gpio either.

        Did you take a look at the gpio util from wiringpi ?
        http://wiringpi.com/the-gpio-utility/

        I found this option in the doc, that may help (if I understood your need) :

        gpio wfi <pin> rising/falling/both
        
        This causes GPIO to perform a non-busy wait on a single GPIO pin until it changes state to that indicated.
        
        1 Reply Last reply Reply Quote 2
        • cyperghostC
          cyperghost
          last edited by

          @meleu I have to say thank you for your detailed explanation. I'm currently on holidays ... so I'm a bit quieter in this forum for couple of days. I think during these days a few things will change to ES ...
          But I take my portable Pi to the airport with a copy of Terranigma on it (I deeply hope that I will save humankind during may holiday)...

          We are looping to check a file content every single second just to execute a command to SHUT DOWN THE WHOLE SYSTEM! In my head the thought is: "this script is wasting 99.99999% of its CPU time slice!".

          I understand you... about the inotify thing...
          But without the real hardware it's a bit a pain ... but it's solvable imho
          Maybe a chmod to the exported GPIO files will help.

            echo "$GPIOpin1" > /sys/class/gpio/export
            echo "in" >/sys/class/gpio/$GPIOpin1/direction
            chmod 660 /sys/class/gpio/$GPIOpin1/direction
            chmod 660 /sys/class/gpio/$GPIOpin1/value
          

          This subject would be a good nerdy chat while drinking some Caipirinhas when you come to Rio.

          Oh yes :) It's all about games ... As I told you it's more likely to expand to the eastern hemisphere but never say never and if I'm on buisness journey I'll surly come back to your offer.

          @Sano
          Welcome to this thread

          Yes I told also to use gpio-control. wPi is also a solution but I think this approach is an overkill to just monitor ONE Gpio. I use wPi in my work as it got's powerfull IRQ functions but you have to load a whole C programm to just monitor ONE GPIO and I think that's not attentend to be called "effective".

          Also think about you have to compile the thing and I think this approach isn't user friendly anymore. So way meleu want it to do is really nice. It comes very far to RT processing!

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

            @meleu

            My new project is a portable device with 4,3" screen and I2S soundmodule.
            It has a foldable screen build in and if you open it you activate an trigger....
            As it is still useable with HDMI I have two /boot/config.txt

            One force activates HDMI and BCM sound
            The other deactivaes HDMI, activates SDTV and uses I2S sound

            So I coded a bash script that would exchange the the boot files in depence to the sensors.

            I've build in a LED that show status. If SDTV config is already active and the foldable monitor is open then it will boot
            If the SDTV config is active and the monitor is closed then LED will blink for a few seconds ... at this time you have the chance to open the LED screen. If not the config for HDMI will be exchanged and a reboot is forced.....

            So here is my code
            Bash is really a powerfull language, the functions call are super easy ;)

            This one is dedicated to you... because it wasn't possible without your help

            I hope you find some time to read the code and give opinion to the structure. Are the intenions setted right, are there some things in general to improve. This is not for puplic usage so if I set a file I know that I need it and I would not check .....

            https://ghostbin.com/paste/kqqcq

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

              @cyperghost dude, your coding style really improved! :-)

              I did a quick look and have two or three comments about the coding (didn't focused on the logic). Will do soon. I'm on the phone now.

              • 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 Looks like a ghost got your code! I was going to make my comments now and the code isn't there.

                • 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 Sorry ;)
                  I setted wrong timer - this will be alive for 7 days now. I'm just busy :( But I hope to come back soon with a RetroAchivments link to you ;)
                  https://pastebin.com/LyA4dTwN

                  Neustart > reboot
                  Blinkschleife > loop for blinking
                  Befehle > commands
                  Wahl > selection
                  Abfrage Hallsensor > request hall sensors

                  Just to understand the comments a bit ;)

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

                    @cyperghost I've created a gist for your script: https://gist.github.com/meleu/0ea0b96ccafdbd0a432cb5a59a8c1835

                    We can comment there! ;-)

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

                      @cyperghost I've just noticed that I don't receive notifications on a gist comment...

                      I answered some of your questions there. :-)

                      • 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 I created a new GIST ;)

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

                          @cyperghost and I answered your questions there too. ;)

                          • 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

                            @meleu
                            Thank you for all your help.... I've something for you

                            inkey() { char="" ; stty -icanon min 0 time 1 ; char=`dd count=1 2>/dev/null` ; }
                            

                            I hope you find that usefull ;)
                            source - Unix Linux Forums

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

                              @cyperghost Not sure what would be the purpose of that crazy function. I never programmed in Basic and don't know what INKEY$ does... :-)


                              bash lovers, I've found this doc and liked it: https://github.com/progrium/bashstyle

                              I didn't like every tip/recommendation there, but found it useful.

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

                                This post is more of a forewarning

                                Don't use climate on the raspberry pi. This program is made to simplify several terminal commands to one or two words, sounds good in theory. But it auto runs and takes up resources. My Pi was sitting idle with climate running and it was at 80 Celsius (176 fahrenheit)

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

                                Backlog: http://backloggery.com/lilbud

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

                                  @meleu You can interrupt loops with that.
                                  But I have to test if this is working with keypads also. If yes you can trigger events by keypad press and don't need enter key

                                  In BASIC (afaik) you wrote it like this.... ucase is a function for uppercase characters so you dont use check for "q" and "Q".

                                  Retro: Do loop was introduced in QBasic in 1991, former there were just if-then clauses and the goto/gosub command - really spooky

                                  REM inkey function for basic
                                  REM in anicient basic $ for charstrings is obligatory 
                                  PRINT "Press Q to quit"
                                  DO : LOOP UNTIL UCASE$(INKEY$)="Q"
                                  
                                  !/bin/bash
                                  # Demo do show function inkey
                                  inkey() { char="" ; stty -icanon min 0 time 1 ; char=`dd count=1 2>/dev/null` ; }
                                  
                                  # ---- main -----
                                  echo "Press some keys and I will show you keybuffer after 3 seconds"
                                  sleep 3 && inkey
                                  echo "You wrote \"$char\""
                                  
                                  meleuM 1 Reply Last reply Reply Quote 0
                                  • meleuM
                                    meleu @cyperghost
                                    last edited by meleu

                                    @cyperghost be careful using this. It keeps your terminal busy and then you will be unable to input something to a simple read.

                                    Check this script (I just added one single line at the end of your demo):

                                    #!/bin/bash -
                                    inkey() { char="" ; stty -icanon min 0 time 1 ; char=`dd count=1 2>/dev/null` ;}
                                    
                                    # ---- main -----
                                    echo "Press some keys and I will show you keybuffer after 3 seconds"
                                    sleep 3 && inkey
                                    echo "You wrote \"$char\""
                                    
                                    read -p "Type something: " var
                                    

                                    And here is the error I've found:

                                    $ bash inkey.sh
                                    Press some keys and I will show you keybuffer after 3 seconds
                                    asdfgYou wrote "asdfg"
                                    Type something: inkey.sh: linha 10: read: erro de leitura: 0: Resource temporarily unavailable
                                    
                                    $ # and it messed my terminal even outside the script, look...
                                    
                                    $ read -p "Type something: " var
                                    Type something: -bash: read: erro de leitura: 0: Resource temporarily unavailable
                                    
                                    

                                    "erro de leitura" (pt_BR) == "reading error" (en)

                                    After this tests I used git to push some commits to one of my repos and when git asked my github username I've got that same error "Resource temporarily unavailable".

                                    The good thing is that you can "fix" your terminal using the reset command (yes, just type reset and then <ENTER>).

                                    I don't know the details of that stty command, but I think it's the responsible for the mess.

                                    Summing up: use that inkey trick very carefully. Maybe performming a reset command after using inkey function is a good idea. :-)

                                    Cheers!

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

                                      @meleu Wow you are really master meleu

                                      #!/bin/bash
                                      # RESET is mandotary for releasing terminal from being busy
                                      # inkey function demo
                                      inkey() { char="" ; stty -icanon min 0 time 1 ; char=`dd count=1 2>/dev/null` ; }
                                      
                                      # ---- main -----
                                      
                                      echo "Press some keys and I will show you keybuffer after 3 seconds"
                                      sleep 3
                                      inkey && reset # <-- this is mandotary
                                      echo "You wrote \"$char\""
                                      
                                      read -p "Type something: " var
                                      echo $var
                                      

                                      I think this function is just a dirty trick. Thanks for looking deeper in. Just did a few simple tests with that.

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

                                        @cyperghost talking about indentations in this thread to not messing that other.

                                        @meleu ;) How about the indentations?

                                        it really improved, I just would like to make one little note.

                                        This snippet here

                                        until [[ "${console[idx]}" == "$1" ]]
                                            do
                                               idx=$(( $idx + 1 ))
                                            done
                                        

                                        Could be better indented this way:

                                        until [[ "${console[idx]}" == "$1" ]]
                                        do
                                            idx=$(( $idx + 1 ))
                                        done
                                        

                                        It's a more common practice.

                                        I'm glad to see you improving your coding style! :-)

                                        Cheers!

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

                                          @meleu About your newest ES-termination code.
                                          Is tty is setting the trigger to start the service? Don't get me wrong I thought tty is just the terminal like ssh ;) Can you please explain?

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

                                            @meleu
                                            Is there no annother approach?
                                            systemd is very complicated?
                                            Can we export a function call to shell that would end ES?
                                            Can we use an alias ... as it can't be used from scripts - is there annother way?

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