• Recent
  • Tags
  • Popular
  • Home
  • Docs
  • Register
  • Login
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.1k 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.
  • C
    cyperghost
    last edited by 30 Aug 2017, 18:54

    @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

    M 2 Replies Last reply 30 Aug 2017, 21:34 Reply Quote 0
    • M
      meleu @cyperghost
      last edited by 30 Aug 2017, 21:34

      @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
      • M
        meleu @cyperghost
        last edited by 1 Sept 2017, 12:14

        @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
        C 1 Reply Last reply 4 Sept 2017, 17:12 Reply Quote 1
        • C
          cyperghost @meleu
          last edited by cyperghost 9 Apr 2017, 18:16 4 Sept 2017, 17:12

          @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 ;)

          M 1 Reply Last reply 4 Sept 2017, 18:13 Reply Quote 1
          • M
            meleu @cyperghost
            last edited by meleu 9 Apr 2017, 19:46 4 Sept 2017, 18:13

            @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
            M 1 Reply Last reply 6 Sept 2017, 02:29 Reply Quote 1
            • M
              meleu @meleu
              last edited by 6 Sept 2017, 02:29

              @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
              C 1 Reply Last reply 6 Sept 2017, 17:22 Reply Quote 1
              • C
                cyperghost @meleu
                last edited by 6 Sept 2017, 17:22

                @meleu I created a new GIST ;)

                M 1 Reply Last reply 7 Sept 2017, 15:04 Reply Quote 0
                • M
                  meleu @cyperghost
                  last edited by 7 Sept 2017, 15:04

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

                  • Useful topics
                  • joystick-selection tool
                  • rpie-art tool
                  • achievements I made
                  C 1 Reply Last reply 7 Sept 2017, 16:05 Reply Quote 0
                  • C
                    cyperghost @meleu
                    last edited by 7 Sept 2017, 16:05

                    @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

                    M 1 Reply Last reply 9 Sept 2017, 01:39 Reply Quote 0
                    • M
                      meleu @cyperghost
                      last edited by 9 Sept 2017, 01:39

                      @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
                      C 1 Reply Last reply 9 Sept 2017, 05:35 Reply Quote 0
                      • L
                        lilbud
                        last edited by 9 Sept 2017, 02:14

                        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
                        • C
                          cyperghost @meleu
                          last edited by cyperghost 9 Sept 2017, 09:20 9 Sept 2017, 05:35

                          @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\""
                          
                          M 1 Reply Last reply 9 Sept 2017, 06:56 Reply Quote 0
                          • M
                            meleu @cyperghost
                            last edited by meleu 9 Sept 2017, 08:18 9 Sept 2017, 06:56

                            @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
                            • C
                              cyperghost
                              last edited by 9 Sept 2017, 08:37

                              @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
                              • M
                                meleu
                                last edited by 10 Sept 2017, 01:13

                                @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
                                • C
                                  cyperghost
                                  last edited by 18 Sept 2017, 15:45

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

                                  C 1 Reply Last reply 19 Sept 2017, 18:32 Reply Quote 0
                                  • C
                                    cyperghost @cyperghost
                                    last edited by 19 Sept 2017, 18:32

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

                                    M 1 Reply Last reply 19 Sept 2017, 21:06 Reply Quote 0
                                    • M
                                      meleu @cyperghost
                                      last edited by meleu 19 Sept 2017, 21:06

                                      @cyperghost There are as many approaches as creativity allows. I just tried to find a way to encompass as many use cases as possible. The explanation for how it works can be complex, but the steps to make it work is not.

                                      Using an alias has its limitations, but it's an option.

                                      • Useful topics
                                      • joystick-selection tool
                                      • rpie-art tool
                                      • achievements I made
                                      C 1 Reply Last reply 19 Sept 2017, 21:17 Reply Quote 0
                                      • C
                                        cyperghost @meleu
                                        last edited by cyperghost 19 Sept 2017, 21:17

                                        @meleu Hello brother ;)
                                        Yes I know it's maybe easy for me and you but take a look here
                                        https://retropie.org.uk/forum/topic/12930/
                                        I suggested the first solution (yours)
                                        but it didn't work, so I used the "old" way.....

                                        I bought now one of these mausberry devices to test ;)
                                        It's really hard to test without the device.


                                        AFAIK an alias can't be called within a script. So you may export the bash function with export -f func_call. But how this works? Do you have a clue?

                                        M 1 Reply Last reply 21 Sept 2017, 08:31 Reply Quote 0
                                        • M
                                          meleu @cyperghost
                                          last edited by 21 Sept 2017, 08:31

                                          @cyperghost said in shell scripting topic:

                                          @meleu Hello brother ;)
                                          Yes I know it's maybe easy for me and you but take a look here
                                          https://retropie.org.uk/forum/topic/12930/
                                          I suggested the first solution (yours)
                                          but it didn't work, so I used the "old" way.....

                                          I'll try to find what he made wrong. I think it was my fault: I didn't give really clear instructions for inexperienced Linux users.

                                          I bought now one of these mausberry devices to test ;)
                                          It's really hard to test without the device.

                                          Oh yeah! Let's break that infinite loop!!

                                          AFAIK an alias can't be called within a script.

                                          I thought you were talking about creating an alias for shutdown command, making it call the killes.sh script before actually shutdown. But this approach has limitations.

                                          So you may export the bash function with export -f func_call. But how this works? Do you have a clue?

                                          When you export something in a shell (functions or variables) only the child shells will know the respective variable/function.

                                          A good way would be to add this function and the export command to one of these files:

                                          • ~/.profile
                                          • ~/.bash_profile
                                          • ~/.bashrc
                                          • /etc/profile
                                          • create a file at /etc/profile.d/ directory (btw, did you notice that you have a /etc/profile.d/10-retropie.sh on your RetroPie? :) )

                                          But this approach also has its limitations...

                                          • Useful topics
                                          • joystick-selection tool
                                          • rpie-art tool
                                          • achievements I made
                                          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.

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