Identifying interactive scripts

so for the purposes of this thread, interactive scripts are shell scripts that prompts for a response from a user and then waits for the user to enter a response before proceeding.

now, from my understanding of this, the one common string i can expect to find in all interactive scripts is some variation of this:

read -p "Would you like to proceed? [y/n]: " yn

notice i bolded the "read -p".

the reason i'm asking about this is, i need to add a line to every line that contains the pattern "read -p" or any other pattern that can be used to identify interactive scripts.

the line i need to add after every line that identifies a script as interactive should be:

exec < /tmp/roles > /dev/roles

below is command im using:

awk '{print} /read -p/ {while (getline < "exec < /tmp/roles > /dev/roles" ) print}' thescript.sh > the_interactive_script.sh

Question:

  1. Also, are there any other patterns I can look for to help me identify if a script is an interactive script?

There are an unlimited number of patterns you could look for that are common to all POSIX conforming shells. There are others that are specific to specific shells. There are others that are specific to certain versions of certain shells.

Adding the line you say you want to add in the place you say you want to add it will be a syntax error in many cases. You CAN NOT add fixed text in a fixed position after a prompt is written and a response is read without fully understanding the context of the code in question. For example:

printf 'Initial prompt for filename: '
while IFS= read -r answer
do	# Validate response...
	if [ -r "$answer" ]
	then	break
	fi
	printf '"%s" is not readable.\nSecondary prompt for filename: ' "$answer"
done

will not work if you insert your new statement after the while before the do .

Note also that most of the code you could be used to produce prompts in an interactive script can, with appropriate file redirections, be used in a non-interactive script with the prompt being redirected to a log file and the response being read from a configuration file.

Note also that interactive programs can be written in C, C++, FORTRAN java, awk , and hundreds of other programming and scripting languages; and sticking shell code into the middle of an object file or into the source of an interactive program that is not a shell script will not work.