1

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?

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.