3

I have a python script that expects user input like this:

script image

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?

2
  • 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". Feb 26, 2019 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, 2019 at 9:41

3 Answers 3

4

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 '%s\n' "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.

5
  • The script is actually simple: script.py name = raw_input("Enter your name: ") print name
    – n00b
    Feb 26, 2019 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. Feb 26, 2019 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). Feb 26, 2019 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, 2019 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). Feb 27, 2019 at 5:28
2

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.

0

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 "$val\r";
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/)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .