Bash Script - Auto Fill Answer

Bash script - Auto fill answer

I would pass a here document to stdin:

./script.sh install <<EOF
y
2
1
n
n
EOF

If you want it on one line, you can also use echo:

echo -e "y\n2\n1\nn\nn" | ./script.sh install

However, I prefer the here document solution since it is IMHO more readable.

Have bash script answer interactive prompts

This is not "auto-completion", this is automation. One common tool for these things is called Expect.

You might also get away with just piping input from yes.

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

Is it possible to partially auto-fill inputs in bash?

Yes, using read:

read -e -p "What is your name: " -i "Mathew" RESULT
echo $RESULT

Will give you:

Enter your name: Mathew

And you can edit or just press enter and $RESULT will hold the value

How can I respond to prompts in a Linux Bash script automatically?

Try this:

echo -e "yes\nyes\nno" | /path/to/your/script

From help echo:

-e: enable interpretation of the following backslash escapes

How to configure bash autocomplete for file search and custom search?

You can use complete's -o default option (Usually I'd use both -o default and -o bashdefault):

complete -o default -F _test_complete remote

According to man bash:

  • bashdefault

    Perform the rest of the default bash completions if the compspec generates no matches.

  • default

    Use readline's default filename completion if the compspec generates no matches.

How to assign variable with autocomplete in the script

You should write like this:

read -p "Enter path to the file: " -i "" -e VARIABLE

Then you can use TAB to select a file and the file pathname will be saved in the VARIABLE.

How do I prompt for Yes/No/Cancel input in a Linux shell script?

The simplest and most widely available method to get user input at a shell prompt is the read command. The best way to illustrate its use is a simple demonstration:

while true; do
read -p "Do you wish to install this program? " yn
case $yn in
[Yy]* ) make install; break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done

Another method, pointed out by Steven Huwig, is Bash's select command. Here is the same example using select:

echo "Do you wish to install this program?"
select yn in "Yes" "No"; do
case $yn in
Yes ) make install; break;;
No ) exit;;
esac
done

With select you don't need to sanitize the input – it displays the available choices, and you type a number corresponding to your choice. It also loops automatically, so there's no need for a while true loop to retry if they give invalid input.

Also, Léa Gris demonstrated a way to make the request language agnostic in her answer. Adapting my first example to better serve multiple languages might look like this:

set -- $(locale LC_MESSAGES)
yesexpr="$1"; noexpr="$2"; yesword="$3"; noword="$4"

while true; do
read -p "Install (${yesword} / ${noword})? " yn
if [[ "$yn" =~ $yesexpr ]]; then make install; exit; fi
if [[ "$yn" =~ $noexpr ]]; then exit; fi
echo "Answer ${yesword} / ${noword}."
done

Obviously other communication strings remain untranslated here (Install, Answer) which would need to be addressed in a more fully completed translation, but even a partial translation would be helpful in many cases.

Finally, please check out the excellent answer by F. Hauri.



Related Topics



Leave a reply



Submit