bash function implement python command












0















I have this



python -c "import sys, urllib as ul; 
print ul.quote('sample hihi ')"


How can I wrap it into a bash function, so I can call it like a function like so:



urlencode('sample hihi ')


Thanks










share|improve this question







New contributor




Born vs. Me is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    This question should asked on stackoverflow, the possibility to receive an answer is higher then here.

    – AtomiX84
    6 hours ago
















0















I have this



python -c "import sys, urllib as ul; 
print ul.quote('sample hihi ')"


How can I wrap it into a bash function, so I can call it like a function like so:



urlencode('sample hihi ')


Thanks










share|improve this question







New contributor




Born vs. Me is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    This question should asked on stackoverflow, the possibility to receive an answer is higher then here.

    – AtomiX84
    6 hours ago














0












0








0








I have this



python -c "import sys, urllib as ul; 
print ul.quote('sample hihi ')"


How can I wrap it into a bash function, so I can call it like a function like so:



urlencode('sample hihi ')


Thanks










share|improve this question







New contributor




Born vs. Me is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I have this



python -c "import sys, urllib as ul; 
print ul.quote('sample hihi ')"


How can I wrap it into a bash function, so I can call it like a function like so:



urlencode('sample hihi ')


Thanks







bash functions






share|improve this question







New contributor




Born vs. Me is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Born vs. Me is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Born vs. Me is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 6 hours ago









Born vs. MeBorn vs. Me

82




82




New contributor




Born vs. Me is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Born vs. Me is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Born vs. Me is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1





    This question should asked on stackoverflow, the possibility to receive an answer is higher then here.

    – AtomiX84
    6 hours ago














  • 1





    This question should asked on stackoverflow, the possibility to receive an answer is higher then here.

    – AtomiX84
    6 hours ago








1




1





This question should asked on stackoverflow, the possibility to receive an answer is higher then here.

– AtomiX84
6 hours ago





This question should asked on stackoverflow, the possibility to receive an answer is higher then here.

– AtomiX84
6 hours ago










1 Answer
1






active

oldest

votes


















3














You should pass the function arguments into your Python code as regular shell arguments, which can be accessed there through the sys.argv array. In the Bash function, its call arguments can be accessed e.g. through "$*", which returns all arguments as a single, space separated string argument (as if it were quoted as a whole, not separate tokens).



Here's an example that follows your original code:



urlquote() { python -c 'import sys, urllib as ul; print ul.quote(sys.argv[1])' "$*" ;}


However, as Python 2 support will end soon, here's the better Python 3 equivalent:



urlquote() { python3 -c 'import sys, urllib.parse as ul; print(ul.quote(sys.argv[1]))' "$*" ;}


The invocation is the same for both, type the function name followed by the string you want to encode as argument. Don't forget to quote it appropriately:



$ urlquote "roses are red, violets are blue..."
roses%20are%20red%2C%20violets%20are%20blue...




Note: Don't ever directly inject shell arguments into your Python code string, like python -c "print('$x')", as this could lead to easily exploitable security risks if you ever plan to run your function on untrusted input, allowing people to run arbitrary commands!



Have a look at this shell example, where I craft the argument in the variable x in a way to run the system command uname (which is harmless and just prints "Linux", but you can do everything here):



$ x="');import os;os.system('uname');('"

$ python3 -c "print('$x')"
Linux





share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "89"
    };
    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
    });


    }
    });






    Born vs. Me is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1109581%2fbash-function-implement-python-command%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    You should pass the function arguments into your Python code as regular shell arguments, which can be accessed there through the sys.argv array. In the Bash function, its call arguments can be accessed e.g. through "$*", which returns all arguments as a single, space separated string argument (as if it were quoted as a whole, not separate tokens).



    Here's an example that follows your original code:



    urlquote() { python -c 'import sys, urllib as ul; print ul.quote(sys.argv[1])' "$*" ;}


    However, as Python 2 support will end soon, here's the better Python 3 equivalent:



    urlquote() { python3 -c 'import sys, urllib.parse as ul; print(ul.quote(sys.argv[1]))' "$*" ;}


    The invocation is the same for both, type the function name followed by the string you want to encode as argument. Don't forget to quote it appropriately:



    $ urlquote "roses are red, violets are blue..."
    roses%20are%20red%2C%20violets%20are%20blue...




    Note: Don't ever directly inject shell arguments into your Python code string, like python -c "print('$x')", as this could lead to easily exploitable security risks if you ever plan to run your function on untrusted input, allowing people to run arbitrary commands!



    Have a look at this shell example, where I craft the argument in the variable x in a way to run the system command uname (which is harmless and just prints "Linux", but you can do everything here):



    $ x="');import os;os.system('uname');('"

    $ python3 -c "print('$x')"
    Linux





    share|improve this answer




























      3














      You should pass the function arguments into your Python code as regular shell arguments, which can be accessed there through the sys.argv array. In the Bash function, its call arguments can be accessed e.g. through "$*", which returns all arguments as a single, space separated string argument (as if it were quoted as a whole, not separate tokens).



      Here's an example that follows your original code:



      urlquote() { python -c 'import sys, urllib as ul; print ul.quote(sys.argv[1])' "$*" ;}


      However, as Python 2 support will end soon, here's the better Python 3 equivalent:



      urlquote() { python3 -c 'import sys, urllib.parse as ul; print(ul.quote(sys.argv[1]))' "$*" ;}


      The invocation is the same for both, type the function name followed by the string you want to encode as argument. Don't forget to quote it appropriately:



      $ urlquote "roses are red, violets are blue..."
      roses%20are%20red%2C%20violets%20are%20blue...




      Note: Don't ever directly inject shell arguments into your Python code string, like python -c "print('$x')", as this could lead to easily exploitable security risks if you ever plan to run your function on untrusted input, allowing people to run arbitrary commands!



      Have a look at this shell example, where I craft the argument in the variable x in a way to run the system command uname (which is harmless and just prints "Linux", but you can do everything here):



      $ x="');import os;os.system('uname');('"

      $ python3 -c "print('$x')"
      Linux





      share|improve this answer


























        3












        3








        3







        You should pass the function arguments into your Python code as regular shell arguments, which can be accessed there through the sys.argv array. In the Bash function, its call arguments can be accessed e.g. through "$*", which returns all arguments as a single, space separated string argument (as if it were quoted as a whole, not separate tokens).



        Here's an example that follows your original code:



        urlquote() { python -c 'import sys, urllib as ul; print ul.quote(sys.argv[1])' "$*" ;}


        However, as Python 2 support will end soon, here's the better Python 3 equivalent:



        urlquote() { python3 -c 'import sys, urllib.parse as ul; print(ul.quote(sys.argv[1]))' "$*" ;}


        The invocation is the same for both, type the function name followed by the string you want to encode as argument. Don't forget to quote it appropriately:



        $ urlquote "roses are red, violets are blue..."
        roses%20are%20red%2C%20violets%20are%20blue...




        Note: Don't ever directly inject shell arguments into your Python code string, like python -c "print('$x')", as this could lead to easily exploitable security risks if you ever plan to run your function on untrusted input, allowing people to run arbitrary commands!



        Have a look at this shell example, where I craft the argument in the variable x in a way to run the system command uname (which is harmless and just prints "Linux", but you can do everything here):



        $ x="');import os;os.system('uname');('"

        $ python3 -c "print('$x')"
        Linux





        share|improve this answer













        You should pass the function arguments into your Python code as regular shell arguments, which can be accessed there through the sys.argv array. In the Bash function, its call arguments can be accessed e.g. through "$*", which returns all arguments as a single, space separated string argument (as if it were quoted as a whole, not separate tokens).



        Here's an example that follows your original code:



        urlquote() { python -c 'import sys, urllib as ul; print ul.quote(sys.argv[1])' "$*" ;}


        However, as Python 2 support will end soon, here's the better Python 3 equivalent:



        urlquote() { python3 -c 'import sys, urllib.parse as ul; print(ul.quote(sys.argv[1]))' "$*" ;}


        The invocation is the same for both, type the function name followed by the string you want to encode as argument. Don't forget to quote it appropriately:



        $ urlquote "roses are red, violets are blue..."
        roses%20are%20red%2C%20violets%20are%20blue...




        Note: Don't ever directly inject shell arguments into your Python code string, like python -c "print('$x')", as this could lead to easily exploitable security risks if you ever plan to run your function on untrusted input, allowing people to run arbitrary commands!



        Have a look at this shell example, where I craft the argument in the variable x in a way to run the system command uname (which is harmless and just prints "Linux", but you can do everything here):



        $ x="');import os;os.system('uname');('"

        $ python3 -c "print('$x')"
        Linux






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 5 hours ago









        Byte CommanderByte Commander

        63.5k26172290




        63.5k26172290






















            Born vs. Me is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Born vs. Me is a new contributor. Be nice, and check out our Code of Conduct.













            Born vs. Me is a new contributor. Be nice, and check out our Code of Conduct.












            Born vs. Me is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Ask Ubuntu!


            • 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%2faskubuntu.com%2fquestions%2f1109581%2fbash-function-implement-python-command%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

            GameSpot

            connect to host localhost port 22: Connection refused

            Getting a Wifi WPA2 wifi connection