[SOLVED] Bash script to catch prompts and execute them
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am currently setting up an IRC server and I have multiple servers to add to the tree. I am currently writing a bash script that will auto compile the configure script that comes with the software. I am trying to catch these phrases mainly:
Code:
In what directory do you wish to install the InspIRCd base?
[/home/ircd/run] ->
In what directory are the configuration files?
[/home/ircd/run/conf] ->
In what directory are the modules to be compiled to?
[/home/ircd/run/modules] ->
In what directory is the IRCd binary to be placed?
[/home/ircd/run/bin] ->
In what directory are variable data files to be located in?
[/home/ircd/run/data] ->
In what directory are the logs to be stored in?
[/home/ircd/run/logs] ->
In what directory do you want the build to take place?
[/home/ircd/build] ->
You are running a Linux 2.6+ operating system, and epoll
was detected. Would you like to enable epoll support?
This is likely to increase performance.
If you are unsure, answer yes.
Enable epoll? [y] ->
Could not detect OpenSSL or GnuTLS. Make sure pkg-config is installed and
is in your path.
Would you like to check for updates to third-party modules? [n] -> y
On each line it will need to just do a return or have an option to put the install path.
I am not 100% sure I follow. Are you asking can you set a default value that will be used if the user chooses to use the default and not add their own?
If yes, then you can look at the -t option for read (assuming this is the construct you are using).
If above is not the correct guess, please try and explain a little further?
I am not 100% sure I follow. Are you asking can you set a default value that will be used if the user chooses to use the default and not add their own?
If yes, then you can look at the -t option for read (assuming this is the construct you are using).
If above is not the correct guess, please try and explain a little further?
Its more for unattended installing. Basically it will auto input the predefined options into the script once its prompted for that question.
What you want to accomplish is somewhat difficult in bash. Basically, you need to set up a bi-directional pipe from you application to the script.
It might be somewhat easier to do this in gawk. (I was working on an example, but it didn't work on your sample data. My wife insists that I desist now and come to bed since it's 22:45 here now. For what it's worth, here's the NON-WORKING code I had.)
Code:
$ cat ~/Scripts/gawk/deadlySniper/run_it.gawk
#!/bin/gawk -f
#
# Verify the argument list
BEGIN {
echo="y" # Set this to any non-null value if you want the program input an output echoed to /dev/stdout
}
BEGIN {
# Do we have any arguments?
if (ARGC != 3) {
printf("\n\
Usage: run_it questions program\n\
\n\
Where \"questions\" is a file containing a regular expression (in quotes) followed by the response when\n\
that expression is matched. (The first matches expression is the one whic will be used.)\n\
and \"program\" is the command to start the program running.\n\n\
")
exit(0)
}
# Load the question set from the first argument
nq=0
while ((getline line < ARGV[1]) > 0) {
nf=split(line, part, /"/)
prompt[++nq] = part[2]
response[nq] = part[3]
}
close(ARGV[1])
program=ARGV[2]
if (echo) {
print "Processing \"" program "\" using responses from \"" ARGV[1] "\"."
print ""
}
do {
if ((program |& getline line) < 0) {
break
}
if (echo) printf(line)
for (i =1; i<=nq;++i) {
if (line ~ prompt[i]) {
if (echo) print response[i]
print response[i] |& program
break
}
}
} while(line)
close(program)
exit(0)
}
I'll try to get this working, but not now. Sorry.
<edit>
My bad!
gawk is a record processing language.
gawk won't work for this because the getline read from the pipe (even if FS=RS="") will not return a string until a record termination character (\n) is seen.
</edit>
Last edited by PTrenholme; 05-10-2014 at 02:54 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.