How to pass input to a script from terminal?bash: how to pass command line arguments containing special...
Is there a verb that means to inject with poison?
Square Root Distance from Integers
A Missing Symbol for This Logo
Why is it that Bernie Sanders is always called a "socialist"?
After checking in online, how do I know whether I need to go show my passport at airport check-in?
Stuck on a Geometry Puzzle
Does the ditching switch allow an A320 to float indefinitely?
Calculate of total length of edges in Voronoi diagram
Equivalent of "illegal" for violating civil law
Is `Object` a function in javascript?
What is the wife of a henpecked husband called?
How would an AI self awareness kill switch work?
How can the probability of a fumble decrease linearly with more dice?
Boss asked me to sign a resignation paper without a date on it along with my new contract
Count repetitions of an array
Why maximum length of IP, TCP, UDP packet is not suit?
A fantasy book with seven white haired women on the cover
Custom shape shows unwanted extra line
Does it take energy to move something in a circle?
Why zero tolerance on nudity in space?
How do I prevent a homebrew Grappling Hook feature from trivializing Tomb of Annihilation?
Why did Luke use his left hand to shoot?
How do you funnel food off a cutting board?
What does an unprocessed RAW file look like?
How to pass input to a script from terminal?
bash: how to pass command line arguments containing special charactershow to pass parameters from a file into a script?How to by-pass user interactions in a script?forcing windows 7 not to open python script executables (specificaly pip an IPython) in a new popup cmd windowEOFError when executing python scripts that need user input inside emacsHow can I make a Bash Script take an input before the script is invoked?Type on python script on the Unix command line (without a .py file)How to run Python script in a terminal window by double clicking it?How to take user input from bash script when run as executable without terminal?Pass ffmpeg stream through python script
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
New contributor
n00b is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
New contributor
n00b is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
1 hour ago
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
New contributor
n00b is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
New contributor
n00b is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
n00b is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 1 hour ago
Kamil Maciorowski
27.6k156084
27.6k156084
New contributor
n00b is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 3 hours ago
n00bn00b
11
11
New contributor
n00b is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
n00b is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
n00b is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
1 hour ago
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
1 hour ago
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
1 hour ago
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
1 hour ago
add a comment |
2 Answers
2
active
oldest
votes
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 |
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
printf '%sn' "John" | python script.py
and this should work even in plain sh. There is also
echo "John" | python script.py
Note printf is in general better than echo, but with this fixed string both methods 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.
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
});
}
});
n00b is a new contributor. Be nice, and check out our Code of Conduct.
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
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 3 hours ago
Arcane BlackwoodArcane Blackwood
33
33
add a comment |
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
printf '%sn' "John" | python script.py
and this should work even in plain sh. There is also
echo "John" | python script.py
Note printf is in general better than echo, but with this fixed string both methods 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.
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
printf '%sn' "John" | python script.py
and this should work even in plain sh. There is also
echo "John" | python script.py
Note printf is in general better than echo, but with this fixed string both methods 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.
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
printf '%sn' "John" | python script.py
and this should work even in plain sh. There is also
echo "John" | python script.py
Note printf is in general better than echo, but with this fixed string both methods 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
printf '%sn' "John" | python script.py
and this should work even in plain sh. There is also
echo "John" | python script.py
Note printf is in general better than echo, but with this fixed string both methods 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.
answered 1 hour ago
Kamil MaciorowskiKamil Maciorowski
27.6k156084
27.6k156084
add a comment |
add a comment |
n00b is a new contributor. Be nice, and check out our Code of Conduct.
n00b is a new contributor. Be nice, and check out our Code of Conduct.
n00b is a new contributor. Be nice, and check out our Code of Conduct.
n00b is a new contributor. Be nice, and check out our Code of Conduct.
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
1 hour ago