Automatically Feed Input to Linux Command Line

How to input automatically when running a shell over SSH?

For general command-line automation, Expect is the classic tool. Or try pexpect if you're more comfortable with Python.

Here's a similar question that suggests using Expect: Use expect in bash script to provide password to SSH command

Using Bash Script to feed input to command line

A couple of ways.

Group all the echo commands and pipe them to the command:

{ echo $firstname; echo $lastname ; } | somecommand

or use a heredoc:

somecommand <<EOF
$firstname
$lastname
EOF

How can I feed input within bash [Executed through the Network]

This is convoluted, but might do what you want.

sudo bash -c "$(cat read-my-name.sh)" <name-input-file

The -c says the next quoted argument are the commands to run (so, read the script as a string on the command line, instead of from a file), and the calling shell interpolates the contents of the file inside the double quotes before the sudo command gets evaluated. So if read-my-name.sh contains

#!/bin/bash
read -p "I want your name please"

then the command gets expanded into

sudo bash -c '#!/bin/bash
read -p "I want your name please"' <name-input-file

(where of course at this time the shell has actually removed the outer double quotes altogether; I put in single quotes in their place instead to show how this would look as actually executable, syntactically valid code).

filling user read input automatically in shell

The equivalent of pressing Enter is a newline character. There are a few ways you could send a y followed by a newline:

In this case you can use yes in a pipeline. yes simply keeps printing a string (by default y) followed by a newline until it's killed. Using your example:

$ yes | ./script.sh
./script.sh: line 3: do_that: command not found

Or you could use a here-string:

./script.sh <<< y

Or simply echo in a pipeline:

echo y | ./script.sh

How can I automate a python program that requires two inputs in a linux command line?

Just use for loops in bash.

for i in {1..1030}
do
python ex_script.py -d <the first input here> -r $i
done

The loop will iterate through the numbers 1 to 1030. Then, you could put any command-line instructions inside the loop and use the variable $i, passing it as an argument for the instruction.

You could put it into a .sh file and make it executable using chmod +x.

Then run it as an executable script from the terminal.

Pass input to terminal automatically?

what about yes | your_command.

or better yet add this line to your
~/.ssh/config

StrictHostKeyChecking no

How to automate providing input to a prompt using bash?

Okay I think I figured it out. Please let me know if there is something inherently wrong about it.

gmx rdf -f 600-c6h6-MolDynamics_good-PBC.xtc -o rg.xvg -s 600-c6h6-MolDynamics.tpr -selrpos whole_mol_com -seltype whole_mol_com -b 5000 -e 10000 << EOF
2
2
EOF

This seems to do the job.

How can I pipe initial input into process which will then be interactive?

You don't need to write a new tool to forward stdin - one has already been written (cat):

(echo "initial command" && cat) | some_tool

This does have the downside of connecting a pipe to some_tool, not a terminal.

Passing arguments to an interactive program non-interactively

For more complex tasks there is expect ( http://en.wikipedia.org/wiki/Expect ).
It basically simulates a user, you can code a script how to react to specific program outputs and related stuff.

This also works in cases like ssh that prohibits piping passwords to it.



Related Topics



Leave a reply



Submit