Using Bash Script to Feed Input to Command Line

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).

How do I provide input to a C program from bash?

You can feed input into a program from bash using any of the following mechanisms.

For a single line of input, you can use a here-string:

./ex17 <<<'w'

For multiple lines, you can use a here-document:

./ex17 <<'EOF'
w
second line of input
more input
EOF

Or you can move those lines out of the script and into a separate file:

./ex17 <filename    

More generally, you can run a command that generates as its output the desired input to your program, and connect them together with a pipe. For instance, the above could also be written:

cat filename | ./ex17

or the original example as

echo w | ./ex17

That's more general because you can replace cat and echo here with any sort of program, which can do all sorts of computation to determine what it outputs instead of just dumping the contents of a static string or file.

But what you can't easily do from bash is drive input, read output, and make decisions about what to send as the next input. For that, you should look at expect. An expect script would look something like this:

#!/usr/bin/env expect
spawn ./ex17
expect ">"
send "w\n"
expect "Whats next?"
send "next line here\n"
# turn it back over to interactive user
interact

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.

In bash, how to process all user input on command line

If I understand you correctly, sounds like you just need to set up some aliases:

$ alias animal1=goldfish
$ animal1
bash: goldfish: command not found

This allows the shell to be used interactively as usual but will make the substitutions you want.

You can add this alias definition to one of your startup files, commonly ~/.bashrc or ~/.profile, to have them take effect on any new shell that you open.

how to pass keyboard input along to linux command?

Use the pipe | operator to connect the output of one command to the input of another.

echo 1 | command

If you want to repeat some input to a command, you can use yes. By default it sends the string "y" repeatedly but it also repeat a different string of your choice.

yes | cp * /tmp  # Answer "y" to all of cp's "Are you sure?" prompts.
yes 1 | command # Answer "1" repeatedly until the command exits.


Related Topics



Leave a reply



Submit