udev rules with a math expression












0















I have a udev rule that creates a symlink for serial devices (USB serial ports) using an expression like this: SYMLINK+="MyDevice_%n". The %n assigns the system device node number starting with 0. Is there a way to modify the numbering - instead of %n, perhaps %n+1? Math expressions don't work in a udev rule. I might be able to use a bash script to do the equivalent of echo $((%n+1)) but I'm not sure how to do this in a udev rule. Any helpful suggestions?










share|improve this question





























    0















    I have a udev rule that creates a symlink for serial devices (USB serial ports) using an expression like this: SYMLINK+="MyDevice_%n". The %n assigns the system device node number starting with 0. Is there a way to modify the numbering - instead of %n, perhaps %n+1? Math expressions don't work in a udev rule. I might be able to use a bash script to do the equivalent of echo $((%n+1)) but I'm not sure how to do this in a udev rule. Any helpful suggestions?










    share|improve this question



























      0












      0








      0








      I have a udev rule that creates a symlink for serial devices (USB serial ports) using an expression like this: SYMLINK+="MyDevice_%n". The %n assigns the system device node number starting with 0. Is there a way to modify the numbering - instead of %n, perhaps %n+1? Math expressions don't work in a udev rule. I might be able to use a bash script to do the equivalent of echo $((%n+1)) but I'm not sure how to do this in a udev rule. Any helpful suggestions?










      share|improve this question
















      I have a udev rule that creates a symlink for serial devices (USB serial ports) using an expression like this: SYMLINK+="MyDevice_%n". The %n assigns the system device node number starting with 0. Is there a way to modify the numbering - instead of %n, perhaps %n+1? Math expressions don't work in a udev rule. I might be able to use a bash script to do the equivalent of echo $((%n+1)) but I'm not sure how to do this in a udev rule. Any helpful suggestions?







      serial-port udev






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 5 '14 at 20:30







      JimFred

















      asked Jul 9 '13 at 3:43









      JimFredJimFred

      1,04177




      1,04177






















          2 Answers
          2






          active

          oldest

          votes


















          1














          Use the udev rules PROGRAM field to execute a /bin/sh echo expression and capture the result using %c, like this...



          PROGRAM="/bin/sh -c 'echo $((%n+1))'", SYMLINK+="MyDevice_%c"


          The resulting symlink will have the equivalent of MyDevice_$((%n+1)) or MyDevice_1 if %n is zero. The $(()) construct is called "arithmetic expansion" and causes the contents to be evaluated as an integer expression. It's a syntax element of the shell.






          share|improve this answer































            1














            Use ++%n to start the counter from 0 (MyDevice_0)



            PROGRAM="/bin/sh -c 'echo $((++%n))'", SYMLINK+="MyDevice_%c"






            share|improve this answer



















            • 2





              Maybe you mean "to start the counter from 1"? Starting at 0 is exactly what OP doesn't want :)

              – wazoox
              Jan 18 at 17:52











            • Oops, I misread the OP. Sorry. Yeah, there is no need to use PROGRAM at all to start with 0 since you can simply use %n

              – PooSH
              Jan 20 at 8:04











            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "3"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f617302%2fudev-rules-with-a-math-expression%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Use the udev rules PROGRAM field to execute a /bin/sh echo expression and capture the result using %c, like this...



            PROGRAM="/bin/sh -c 'echo $((%n+1))'", SYMLINK+="MyDevice_%c"


            The resulting symlink will have the equivalent of MyDevice_$((%n+1)) or MyDevice_1 if %n is zero. The $(()) construct is called "arithmetic expansion" and causes the contents to be evaluated as an integer expression. It's a syntax element of the shell.






            share|improve this answer




























              1














              Use the udev rules PROGRAM field to execute a /bin/sh echo expression and capture the result using %c, like this...



              PROGRAM="/bin/sh -c 'echo $((%n+1))'", SYMLINK+="MyDevice_%c"


              The resulting symlink will have the equivalent of MyDevice_$((%n+1)) or MyDevice_1 if %n is zero. The $(()) construct is called "arithmetic expansion" and causes the contents to be evaluated as an integer expression. It's a syntax element of the shell.






              share|improve this answer


























                1












                1








                1







                Use the udev rules PROGRAM field to execute a /bin/sh echo expression and capture the result using %c, like this...



                PROGRAM="/bin/sh -c 'echo $((%n+1))'", SYMLINK+="MyDevice_%c"


                The resulting symlink will have the equivalent of MyDevice_$((%n+1)) or MyDevice_1 if %n is zero. The $(()) construct is called "arithmetic expansion" and causes the contents to be evaluated as an integer expression. It's a syntax element of the shell.






                share|improve this answer













                Use the udev rules PROGRAM field to execute a /bin/sh echo expression and capture the result using %c, like this...



                PROGRAM="/bin/sh -c 'echo $((%n+1))'", SYMLINK+="MyDevice_%c"


                The resulting symlink will have the equivalent of MyDevice_$((%n+1)) or MyDevice_1 if %n is zero. The $(()) construct is called "arithmetic expansion" and causes the contents to be evaluated as an integer expression. It's a syntax element of the shell.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jul 9 '13 at 4:56









                JimFredJimFred

                1,04177




                1,04177

























                    1














                    Use ++%n to start the counter from 0 (MyDevice_0)



                    PROGRAM="/bin/sh -c 'echo $((++%n))'", SYMLINK+="MyDevice_%c"






                    share|improve this answer



















                    • 2





                      Maybe you mean "to start the counter from 1"? Starting at 0 is exactly what OP doesn't want :)

                      – wazoox
                      Jan 18 at 17:52











                    • Oops, I misread the OP. Sorry. Yeah, there is no need to use PROGRAM at all to start with 0 since you can simply use %n

                      – PooSH
                      Jan 20 at 8:04
















                    1














                    Use ++%n to start the counter from 0 (MyDevice_0)



                    PROGRAM="/bin/sh -c 'echo $((++%n))'", SYMLINK+="MyDevice_%c"






                    share|improve this answer



















                    • 2





                      Maybe you mean "to start the counter from 1"? Starting at 0 is exactly what OP doesn't want :)

                      – wazoox
                      Jan 18 at 17:52











                    • Oops, I misread the OP. Sorry. Yeah, there is no need to use PROGRAM at all to start with 0 since you can simply use %n

                      – PooSH
                      Jan 20 at 8:04














                    1












                    1








                    1







                    Use ++%n to start the counter from 0 (MyDevice_0)



                    PROGRAM="/bin/sh -c 'echo $((++%n))'", SYMLINK+="MyDevice_%c"






                    share|improve this answer













                    Use ++%n to start the counter from 0 (MyDevice_0)



                    PROGRAM="/bin/sh -c 'echo $((++%n))'", SYMLINK+="MyDevice_%c"







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 18 at 13:34









                    PooSHPooSH

                    111




                    111








                    • 2





                      Maybe you mean "to start the counter from 1"? Starting at 0 is exactly what OP doesn't want :)

                      – wazoox
                      Jan 18 at 17:52











                    • Oops, I misread the OP. Sorry. Yeah, there is no need to use PROGRAM at all to start with 0 since you can simply use %n

                      – PooSH
                      Jan 20 at 8:04














                    • 2





                      Maybe you mean "to start the counter from 1"? Starting at 0 is exactly what OP doesn't want :)

                      – wazoox
                      Jan 18 at 17:52











                    • Oops, I misread the OP. Sorry. Yeah, there is no need to use PROGRAM at all to start with 0 since you can simply use %n

                      – PooSH
                      Jan 20 at 8:04








                    2




                    2





                    Maybe you mean "to start the counter from 1"? Starting at 0 is exactly what OP doesn't want :)

                    – wazoox
                    Jan 18 at 17:52





                    Maybe you mean "to start the counter from 1"? Starting at 0 is exactly what OP doesn't want :)

                    – wazoox
                    Jan 18 at 17:52













                    Oops, I misread the OP. Sorry. Yeah, there is no need to use PROGRAM at all to start with 0 since you can simply use %n

                    – PooSH
                    Jan 20 at 8:04





                    Oops, I misread the OP. Sorry. Yeah, there is no need to use PROGRAM at all to start with 0 since you can simply use %n

                    – PooSH
                    Jan 20 at 8:04


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Super User!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f617302%2fudev-rules-with-a-math-expression%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                    Alcedinidae

                    Can an atomic nucleus contain both particles and antiparticles? [duplicate]