bash function implement python command
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
add a comment |
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
1
This question should asked on stackoverflow, the possibility to receive an answer is higher then here.
– AtomiX84
Jan 14 at 9:49
@AtomiX84 shell scripting is perfectly on topic here. Please don't recommend moving on topic questions.
– terdon♦
Jan 15 at 15:14
add a comment |
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
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
bash functions
asked Jan 14 at 9:41
Born vs. MeBorn vs. Me
115
115
1
This question should asked on stackoverflow, the possibility to receive an answer is higher then here.
– AtomiX84
Jan 14 at 9:49
@AtomiX84 shell scripting is perfectly on topic here. Please don't recommend moving on topic questions.
– terdon♦
Jan 15 at 15:14
add a comment |
1
This question should asked on stackoverflow, the possibility to receive an answer is higher then here.
– AtomiX84
Jan 14 at 9:49
@AtomiX84 shell scripting is perfectly on topic here. Please don't recommend moving on topic questions.
– terdon♦
Jan 15 at 15:14
1
1
This question should asked on stackoverflow, the possibility to receive an answer is higher then here.
– AtomiX84
Jan 14 at 9:49
This question should asked on stackoverflow, the possibility to receive an answer is higher then here.
– AtomiX84
Jan 14 at 9:49
@AtomiX84 shell scripting is perfectly on topic here. Please don't recommend moving on topic questions.
– terdon♦
Jan 15 at 15:14
@AtomiX84 shell scripting is perfectly on topic here. Please don't recommend moving on topic questions.
– terdon♦
Jan 15 at 15:14
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Jan 14 at 10:28
Byte CommanderByte Commander
64.1k27176294
64.1k27176294
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
This question should asked on stackoverflow, the possibility to receive an answer is higher then here.
– AtomiX84
Jan 14 at 9:49
@AtomiX84 shell scripting is perfectly on topic here. Please don't recommend moving on topic questions.
– terdon♦
Jan 15 at 15:14