How to pass input to a script from terminal?
I have a python script that expects user input like this:
Instead of executing the program and inputting "John" I want to pass the input to it from the command line like $ python script.py < "John"
but it doesn't work. Is there a way to achieve what I want?
linux bash python
add a comment |
I have a python script that expects user input like this:
Instead of executing the program and inputting "John" I want to pass the input to it from the command line like $ python script.py < "John"
but it doesn't work. Is there a way to achieve what I want?
linux bash python
For future reference: (1) Instead of "it doesn't work" you should post the specific error message you got. (2) Instead of this screenshot from execution you should post (the relevant part of) the actual code. The screenshot tells nothing about how the script "expects user input".
– Kamil Maciorowski
Feb 26 at 6:20
Also, for parameter parsing, look into the argparse package. It may not be what you need here, but is incredibly helpful when passing parameters on the command line, like "python fruit_shop.py bananas --cost 2 -- buy 3
"
– Mawg
Feb 27 at 9:41
add a comment |
I have a python script that expects user input like this:
Instead of executing the program and inputting "John" I want to pass the input to it from the command line like $ python script.py < "John"
but it doesn't work. Is there a way to achieve what I want?
linux bash python
I have a python script that expects user input like this:
Instead of executing the program and inputting "John" I want to pass the input to it from the command line like $ python script.py < "John"
but it doesn't work. Is there a way to achieve what I want?
linux bash python
linux bash python
edited Feb 26 at 6:20
Kamil Maciorowski
28.8k156287
28.8k156287
asked Feb 26 at 4:40
n00bn00b
83
83
For future reference: (1) Instead of "it doesn't work" you should post the specific error message you got. (2) Instead of this screenshot from execution you should post (the relevant part of) the actual code. The screenshot tells nothing about how the script "expects user input".
– Kamil Maciorowski
Feb 26 at 6:20
Also, for parameter parsing, look into the argparse package. It may not be what you need here, but is incredibly helpful when passing parameters on the command line, like "python fruit_shop.py bananas --cost 2 -- buy 3
"
– Mawg
Feb 27 at 9:41
add a comment |
For future reference: (1) Instead of "it doesn't work" you should post the specific error message you got. (2) Instead of this screenshot from execution you should post (the relevant part of) the actual code. The screenshot tells nothing about how the script "expects user input".
– Kamil Maciorowski
Feb 26 at 6:20
Also, for parameter parsing, look into the argparse package. It may not be what you need here, but is incredibly helpful when passing parameters on the command line, like "python fruit_shop.py bananas --cost 2 -- buy 3
"
– Mawg
Feb 27 at 9:41
For future reference: (1) Instead of "it doesn't work" you should post the specific error message you got. (2) Instead of this screenshot from execution you should post (the relevant part of) the actual code. The screenshot tells nothing about how the script "expects user input".
– Kamil Maciorowski
Feb 26 at 6:20
For future reference: (1) Instead of "it doesn't work" you should post the specific error message you got. (2) Instead of this screenshot from execution you should post (the relevant part of) the actual code. The screenshot tells nothing about how the script "expects user input".
– Kamil Maciorowski
Feb 26 at 6:20
Also, for parameter parsing, look into the argparse package. It may not be what you need here, but is incredibly helpful when passing parameters on the command line, like "
python fruit_shop.py bananas --cost 2 -- buy 3
"– Mawg
Feb 27 at 9:41
Also, for parameter parsing, look into the argparse package. It may not be what you need here, but is incredibly helpful when passing parameters on the command line, like "
python fruit_shop.py bananas --cost 2 -- buy 3
"– Mawg
Feb 27 at 9:41
add a comment |
3 Answers
3
active
oldest
votes
If the script uses its stdin to read data, this line you used
python script.py < "John"
should work, except it tries to send the content of a file named John
to the stdin of the script (and it will fail if there's no such file; I guess this happened to you). In Bash there's a way to send a string though, here string:
python script.py <<< "John"
A newline is appended automatically. Another way is with a pipeline:
printf '%sn' "John" | python script.py
and this should work even in plain sh
. So should this:
echo "John" | python script.py
Note printf
is in general better than echo
, but with this fixed string both commands should work right.
Neither of the above will work if the script directly uses its controlling terminal (/dev/tty
) instead of its stdin to read user's response. If so, expect
(like in this other answer) will be useful. You didn't show us the script itself so it's impossible to tell for sure; you should know.
The script is actually simple: script.pyname = raw_input("Enter your name: ") print name
– n00b
Feb 26 at 14:03
@n00b Tested, my answer works, you don't need any complex tool likeexpect
. The only difference is "John" is not printed to the terminal while it's being read; it's only printed while it's actually printed and you're one "John" short on your screen (this is normal, the first "John" is printed by your terminal, not the script). Still the variable inside the script is assigned the right value. In general my way is better thanexpect
when you just want "to pass the input to it from the command line".expect
emulates the whole terminal-based interaction, so it prints extra "John" for you.
– Kamil Maciorowski
Feb 26 at 16:37
@n00b The above comment is in case you thought my answer fails "to pass the input to it from the command line". You may have thought it was the second "John" that was not printed, while it was the first one (which was never printed by the script itself anyway).
– Kamil Maciorowski
Feb 26 at 16:44
This solves my issue, thank you! It would be wonderful if you shared a resource about how it works.
– n00b
Feb 27 at 1:14
@n00b I added a link documenting pipelines; the link for here strings is there from the beginning. Explaining howraw_input
works is rather off-topic (Super User is not about programming).
– Kamil Maciorowski
Feb 27 at 5:28
add a comment |
A really simple way to achieve this is to use sys.argv
from the sys
module, which allows you to access command line arguments. sys.argv
is a list of the command line arguments, with sys.argv[0]
being the script name.
You could accept a command line argument if there is one, otherwise prompt the user for input:
#!/usr/bin/python3
import sys
if len(sys.argv) > 1:
name = sys.argv[1]
else:
name = input("Enter name:")
print(name)
You then call the script with a command line argument if needed: ./script.py John
.
add a comment |
A simple way is creating a script to input the information:
#!/usr/bin/expect
set cmd [lrange $argv 1 end]
set val [lindex $argv 0]
eval spawn $cmd
expect ":"
send "$valr";
interact
Save this file somwehere (eg ~/sendInput.sh
)
and run sudo chmod +x ~/sendInput.sh
to make the file executable
now run !/sendInput.sh "Jhon" python script.py
This should send the input "Jhon" to the script.py once the character ":" is sent.
(Adpated from https://srvfail.com/how-to-provide-ssh-password-inside-a-script-or-oneliner/)
add a comment |
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
});
}
});
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%2fsuperuser.com%2fquestions%2f1409426%2fhow-to-pass-input-to-a-script-from-terminal%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If the script uses its stdin to read data, this line you used
python script.py < "John"
should work, except it tries to send the content of a file named John
to the stdin of the script (and it will fail if there's no such file; I guess this happened to you). In Bash there's a way to send a string though, here string:
python script.py <<< "John"
A newline is appended automatically. Another way is with a pipeline:
printf '%sn' "John" | python script.py
and this should work even in plain sh
. So should this:
echo "John" | python script.py
Note printf
is in general better than echo
, but with this fixed string both commands should work right.
Neither of the above will work if the script directly uses its controlling terminal (/dev/tty
) instead of its stdin to read user's response. If so, expect
(like in this other answer) will be useful. You didn't show us the script itself so it's impossible to tell for sure; you should know.
The script is actually simple: script.pyname = raw_input("Enter your name: ") print name
– n00b
Feb 26 at 14:03
@n00b Tested, my answer works, you don't need any complex tool likeexpect
. The only difference is "John" is not printed to the terminal while it's being read; it's only printed while it's actually printed and you're one "John" short on your screen (this is normal, the first "John" is printed by your terminal, not the script). Still the variable inside the script is assigned the right value. In general my way is better thanexpect
when you just want "to pass the input to it from the command line".expect
emulates the whole terminal-based interaction, so it prints extra "John" for you.
– Kamil Maciorowski
Feb 26 at 16:37
@n00b The above comment is in case you thought my answer fails "to pass the input to it from the command line". You may have thought it was the second "John" that was not printed, while it was the first one (which was never printed by the script itself anyway).
– Kamil Maciorowski
Feb 26 at 16:44
This solves my issue, thank you! It would be wonderful if you shared a resource about how it works.
– n00b
Feb 27 at 1:14
@n00b I added a link documenting pipelines; the link for here strings is there from the beginning. Explaining howraw_input
works is rather off-topic (Super User is not about programming).
– Kamil Maciorowski
Feb 27 at 5:28
add a comment |
If the script uses its stdin to read data, this line you used
python script.py < "John"
should work, except it tries to send the content of a file named John
to the stdin of the script (and it will fail if there's no such file; I guess this happened to you). In Bash there's a way to send a string though, here string:
python script.py <<< "John"
A newline is appended automatically. Another way is with a pipeline:
printf '%sn' "John" | python script.py
and this should work even in plain sh
. So should this:
echo "John" | python script.py
Note printf
is in general better than echo
, but with this fixed string both commands should work right.
Neither of the above will work if the script directly uses its controlling terminal (/dev/tty
) instead of its stdin to read user's response. If so, expect
(like in this other answer) will be useful. You didn't show us the script itself so it's impossible to tell for sure; you should know.
The script is actually simple: script.pyname = raw_input("Enter your name: ") print name
– n00b
Feb 26 at 14:03
@n00b Tested, my answer works, you don't need any complex tool likeexpect
. The only difference is "John" is not printed to the terminal while it's being read; it's only printed while it's actually printed and you're one "John" short on your screen (this is normal, the first "John" is printed by your terminal, not the script). Still the variable inside the script is assigned the right value. In general my way is better thanexpect
when you just want "to pass the input to it from the command line".expect
emulates the whole terminal-based interaction, so it prints extra "John" for you.
– Kamil Maciorowski
Feb 26 at 16:37
@n00b The above comment is in case you thought my answer fails "to pass the input to it from the command line". You may have thought it was the second "John" that was not printed, while it was the first one (which was never printed by the script itself anyway).
– Kamil Maciorowski
Feb 26 at 16:44
This solves my issue, thank you! It would be wonderful if you shared a resource about how it works.
– n00b
Feb 27 at 1:14
@n00b I added a link documenting pipelines; the link for here strings is there from the beginning. Explaining howraw_input
works is rather off-topic (Super User is not about programming).
– Kamil Maciorowski
Feb 27 at 5:28
add a comment |
If the script uses its stdin to read data, this line you used
python script.py < "John"
should work, except it tries to send the content of a file named John
to the stdin of the script (and it will fail if there's no such file; I guess this happened to you). In Bash there's a way to send a string though, here string:
python script.py <<< "John"
A newline is appended automatically. Another way is with a pipeline:
printf '%sn' "John" | python script.py
and this should work even in plain sh
. So should this:
echo "John" | python script.py
Note printf
is in general better than echo
, but with this fixed string both commands should work right.
Neither of the above will work if the script directly uses its controlling terminal (/dev/tty
) instead of its stdin to read user's response. If so, expect
(like in this other answer) will be useful. You didn't show us the script itself so it's impossible to tell for sure; you should know.
If the script uses its stdin to read data, this line you used
python script.py < "John"
should work, except it tries to send the content of a file named John
to the stdin of the script (and it will fail if there's no such file; I guess this happened to you). In Bash there's a way to send a string though, here string:
python script.py <<< "John"
A newline is appended automatically. Another way is with a pipeline:
printf '%sn' "John" | python script.py
and this should work even in plain sh
. So should this:
echo "John" | python script.py
Note printf
is in general better than echo
, but with this fixed string both commands should work right.
Neither of the above will work if the script directly uses its controlling terminal (/dev/tty
) instead of its stdin to read user's response. If so, expect
(like in this other answer) will be useful. You didn't show us the script itself so it's impossible to tell for sure; you should know.
edited Feb 27 at 5:24
answered Feb 26 at 6:12
Kamil MaciorowskiKamil Maciorowski
28.8k156287
28.8k156287
The script is actually simple: script.pyname = raw_input("Enter your name: ") print name
– n00b
Feb 26 at 14:03
@n00b Tested, my answer works, you don't need any complex tool likeexpect
. The only difference is "John" is not printed to the terminal while it's being read; it's only printed while it's actually printed and you're one "John" short on your screen (this is normal, the first "John" is printed by your terminal, not the script). Still the variable inside the script is assigned the right value. In general my way is better thanexpect
when you just want "to pass the input to it from the command line".expect
emulates the whole terminal-based interaction, so it prints extra "John" for you.
– Kamil Maciorowski
Feb 26 at 16:37
@n00b The above comment is in case you thought my answer fails "to pass the input to it from the command line". You may have thought it was the second "John" that was not printed, while it was the first one (which was never printed by the script itself anyway).
– Kamil Maciorowski
Feb 26 at 16:44
This solves my issue, thank you! It would be wonderful if you shared a resource about how it works.
– n00b
Feb 27 at 1:14
@n00b I added a link documenting pipelines; the link for here strings is there from the beginning. Explaining howraw_input
works is rather off-topic (Super User is not about programming).
– Kamil Maciorowski
Feb 27 at 5:28
add a comment |
The script is actually simple: script.pyname = raw_input("Enter your name: ") print name
– n00b
Feb 26 at 14:03
@n00b Tested, my answer works, you don't need any complex tool likeexpect
. The only difference is "John" is not printed to the terminal while it's being read; it's only printed while it's actually printed and you're one "John" short on your screen (this is normal, the first "John" is printed by your terminal, not the script). Still the variable inside the script is assigned the right value. In general my way is better thanexpect
when you just want "to pass the input to it from the command line".expect
emulates the whole terminal-based interaction, so it prints extra "John" for you.
– Kamil Maciorowski
Feb 26 at 16:37
@n00b The above comment is in case you thought my answer fails "to pass the input to it from the command line". You may have thought it was the second "John" that was not printed, while it was the first one (which was never printed by the script itself anyway).
– Kamil Maciorowski
Feb 26 at 16:44
This solves my issue, thank you! It would be wonderful if you shared a resource about how it works.
– n00b
Feb 27 at 1:14
@n00b I added a link documenting pipelines; the link for here strings is there from the beginning. Explaining howraw_input
works is rather off-topic (Super User is not about programming).
– Kamil Maciorowski
Feb 27 at 5:28
The script is actually simple: script.py
name = raw_input("Enter your name: ") print name
– n00b
Feb 26 at 14:03
The script is actually simple: script.py
name = raw_input("Enter your name: ") print name
– n00b
Feb 26 at 14:03
@n00b Tested, my answer works, you don't need any complex tool like
expect
. The only difference is "John" is not printed to the terminal while it's being read; it's only printed while it's actually printed and you're one "John" short on your screen (this is normal, the first "John" is printed by your terminal, not the script). Still the variable inside the script is assigned the right value. In general my way is better than expect
when you just want "to pass the input to it from the command line". expect
emulates the whole terminal-based interaction, so it prints extra "John" for you.– Kamil Maciorowski
Feb 26 at 16:37
@n00b Tested, my answer works, you don't need any complex tool like
expect
. The only difference is "John" is not printed to the terminal while it's being read; it's only printed while it's actually printed and you're one "John" short on your screen (this is normal, the first "John" is printed by your terminal, not the script). Still the variable inside the script is assigned the right value. In general my way is better than expect
when you just want "to pass the input to it from the command line". expect
emulates the whole terminal-based interaction, so it prints extra "John" for you.– Kamil Maciorowski
Feb 26 at 16:37
@n00b The above comment is in case you thought my answer fails "to pass the input to it from the command line". You may have thought it was the second "John" that was not printed, while it was the first one (which was never printed by the script itself anyway).
– Kamil Maciorowski
Feb 26 at 16:44
@n00b The above comment is in case you thought my answer fails "to pass the input to it from the command line". You may have thought it was the second "John" that was not printed, while it was the first one (which was never printed by the script itself anyway).
– Kamil Maciorowski
Feb 26 at 16:44
This solves my issue, thank you! It would be wonderful if you shared a resource about how it works.
– n00b
Feb 27 at 1:14
This solves my issue, thank you! It would be wonderful if you shared a resource about how it works.
– n00b
Feb 27 at 1:14
@n00b I added a link documenting pipelines; the link for here strings is there from the beginning. Explaining how
raw_input
works is rather off-topic (Super User is not about programming).– Kamil Maciorowski
Feb 27 at 5:28
@n00b I added a link documenting pipelines; the link for here strings is there from the beginning. Explaining how
raw_input
works is rather off-topic (Super User is not about programming).– Kamil Maciorowski
Feb 27 at 5:28
add a comment |
A really simple way to achieve this is to use sys.argv
from the sys
module, which allows you to access command line arguments. sys.argv
is a list of the command line arguments, with sys.argv[0]
being the script name.
You could accept a command line argument if there is one, otherwise prompt the user for input:
#!/usr/bin/python3
import sys
if len(sys.argv) > 1:
name = sys.argv[1]
else:
name = input("Enter name:")
print(name)
You then call the script with a command line argument if needed: ./script.py John
.
add a comment |
A really simple way to achieve this is to use sys.argv
from the sys
module, which allows you to access command line arguments. sys.argv
is a list of the command line arguments, with sys.argv[0]
being the script name.
You could accept a command line argument if there is one, otherwise prompt the user for input:
#!/usr/bin/python3
import sys
if len(sys.argv) > 1:
name = sys.argv[1]
else:
name = input("Enter name:")
print(name)
You then call the script with a command line argument if needed: ./script.py John
.
add a comment |
A really simple way to achieve this is to use sys.argv
from the sys
module, which allows you to access command line arguments. sys.argv
is a list of the command line arguments, with sys.argv[0]
being the script name.
You could accept a command line argument if there is one, otherwise prompt the user for input:
#!/usr/bin/python3
import sys
if len(sys.argv) > 1:
name = sys.argv[1]
else:
name = input("Enter name:")
print(name)
You then call the script with a command line argument if needed: ./script.py John
.
A really simple way to achieve this is to use sys.argv
from the sys
module, which allows you to access command line arguments. sys.argv
is a list of the command line arguments, with sys.argv[0]
being the script name.
You could accept a command line argument if there is one, otherwise prompt the user for input:
#!/usr/bin/python3
import sys
if len(sys.argv) > 1:
name = sys.argv[1]
else:
name = input("Enter name:")
print(name)
You then call the script with a command line argument if needed: ./script.py John
.
answered Feb 26 at 14:30
DavidCaraDavidCara
1461
1461
add a comment |
add a comment |
A simple way is creating a script to input the information:
#!/usr/bin/expect
set cmd [lrange $argv 1 end]
set val [lindex $argv 0]
eval spawn $cmd
expect ":"
send "$valr";
interact
Save this file somwehere (eg ~/sendInput.sh
)
and run sudo chmod +x ~/sendInput.sh
to make the file executable
now run !/sendInput.sh "Jhon" python script.py
This should send the input "Jhon" to the script.py once the character ":" is sent.
(Adpated from https://srvfail.com/how-to-provide-ssh-password-inside-a-script-or-oneliner/)
add a comment |
A simple way is creating a script to input the information:
#!/usr/bin/expect
set cmd [lrange $argv 1 end]
set val [lindex $argv 0]
eval spawn $cmd
expect ":"
send "$valr";
interact
Save this file somwehere (eg ~/sendInput.sh
)
and run sudo chmod +x ~/sendInput.sh
to make the file executable
now run !/sendInput.sh "Jhon" python script.py
This should send the input "Jhon" to the script.py once the character ":" is sent.
(Adpated from https://srvfail.com/how-to-provide-ssh-password-inside-a-script-or-oneliner/)
add a comment |
A simple way is creating a script to input the information:
#!/usr/bin/expect
set cmd [lrange $argv 1 end]
set val [lindex $argv 0]
eval spawn $cmd
expect ":"
send "$valr";
interact
Save this file somwehere (eg ~/sendInput.sh
)
and run sudo chmod +x ~/sendInput.sh
to make the file executable
now run !/sendInput.sh "Jhon" python script.py
This should send the input "Jhon" to the script.py once the character ":" is sent.
(Adpated from https://srvfail.com/how-to-provide-ssh-password-inside-a-script-or-oneliner/)
A simple way is creating a script to input the information:
#!/usr/bin/expect
set cmd [lrange $argv 1 end]
set val [lindex $argv 0]
eval spawn $cmd
expect ":"
send "$valr";
interact
Save this file somwehere (eg ~/sendInput.sh
)
and run sudo chmod +x ~/sendInput.sh
to make the file executable
now run !/sendInput.sh "Jhon" python script.py
This should send the input "Jhon" to the script.py once the character ":" is sent.
(Adpated from https://srvfail.com/how-to-provide-ssh-password-inside-a-script-or-oneliner/)
answered Feb 26 at 4:48
Arcane BlackwoodArcane Blackwood
35
35
add a comment |
add a comment |
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.
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%2fsuperuser.com%2fquestions%2f1409426%2fhow-to-pass-input-to-a-script-from-terminal%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
For future reference: (1) Instead of "it doesn't work" you should post the specific error message you got. (2) Instead of this screenshot from execution you should post (the relevant part of) the actual code. The screenshot tells nothing about how the script "expects user input".
– Kamil Maciorowski
Feb 26 at 6:20
Also, for parameter parsing, look into the argparse package. It may not be what you need here, but is incredibly helpful when passing parameters on the command line, like "
python fruit_shop.py bananas --cost 2 -- buy 3
"– Mawg
Feb 27 at 9:41