syrkull
syrkull

Reputation: 2344

filling user read input automatically in shell

there is a shell library that I am not in control of that does not accept user parameters. When I run script.sh, it asks me to input a parameter. I was wondering if there is a way to assign those parameters automatically in another file.

More details: I am guessing the shell library that I am not able to edit or view source code has something like this:

echo "Do that? [Y,n]"
read DO_THAT
if [ "$DO_THAT" = "y" ]; then
  do_that
fi

What I want is to run a file that will assign 'y' to the read parameter 'DO_THAT'.. but I do not know what the read parameter is called.

current command that I tried is:

./script.sh
echo "y"

or

./script.sh y

both did not work.

What happens if I run ./script.sh directly:

-bash-4.2$ ./script.sh
-bash-4.2$ Do that? [Y,n]:

I input y then click enter. What is the equivalent to user inputting a y then clicking enter in bash code in my scenario?

Upvotes: 1

Views: 2371

Answers (2)

wjandrea
wjandrea

Reputation: 32964

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

Upvotes: 1

kabanus
kabanus

Reputation: 25895

The standard way to have scripts take the standard input from a file is input redirection. With some_file.params containing 'y', Your example script will work as expected with

./script.sh < somefile.params

Note this is sensitive to any character - in particular, you need new lines to "press enter", and if you have two you may input an empty string to some of your reads.

Upvotes: 1

Related Questions