Contents
How to write a bash script that answers interactive prompts?
A Sample Script That Requires Multiple Inputs Let’s consider a bash script that asks three simple questions to the user: #!/usr/bin/env bash echo “Hello, please introduce yourself.” echo -n “Your name: ” read -r name echo “Are you human?” echo -n “y/n: ” read -r human echo “What is your favorite programming language?”
Where does the bash script try to run?
Because of the shebang ( #!) defined in the first line, our shell tries to run the script with the interpreter located at the path /usr/bin/expect. If expect is installed and found at this location, it starts to process our script.
How to pass yes to all prompts in Bash?
Just want to add one more way. Found it elsewhere, and is quite simple. Say I want to pass yes for all the prompts at command line for a command “execute_command”, Then I would simply pipe yes to it. This will use yes as the answer to all yes/no prompts. You can also use printf to pipe the input to your script. Highly active question.
Can you pass arguments to an interactive script?
It’s not just one option that I have to pass to the interactive script. 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.
When is Bash invoked as an interactive login shell?
When bash is invoked as an interactive login shell, or as a non-interactive shell with the –login option, it first reads and executes commands from the file /etc/profile, if that file exists.
Why does Bash check if the current shell is running?
When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute.
How to make a bash script switch to interactive mode?
The command prompt is returned, with the shell still configured. You start script in the same shell, using source or .; The first is obvious; I write a little bit more details about the second. As you can see, the script has made the environment for the user and then has given him the prompt.
How to emulate an interactive shell in a script?
It is even possible to emulate an interactive shell in a script. #!/bin/bash MY_PROMPT=’$ ‘ while : do echo -n “$MY_PROMPT” read line eval “$line” done exit 0 # This example script, and much of the above explanation supplied by # Stéphane Chazelas (thanks again).