LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-09-2014, 06:07 PM   #1
deadlySniper
Member
 
Registered: Nov 2008
Location: Rye,NY
Distribution: Linux Mint
Posts: 33

Rep: Reputation: 0
Bash script to catch prompts and execute them


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.

Is this possible?

Sorry I am not well versed in bash.
 
Old 05-09-2014, 10:12 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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?
 
Old 05-10-2014, 12:15 AM   #3
deadlySniper
Member
 
Registered: Nov 2008
Location: Rye,NY
Distribution: Linux Mint
Posts: 33

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
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.
 
Old 05-10-2014, 12:42 AM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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.
 
Old 05-10-2014, 12:50 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
First you should examine if you could give these values to 'configure' without prompting, ie via command line options.
 
Old 05-10-2014, 09:23 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
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 think expect is the tool most suited to this task.

Quote:
First you should examine if you could give these values to 'configure' without prompting, ie via command line options.
But this would obviously be the best option, if available.
 
Old 05-10-2014, 12:27 PM   #7
deadlySniper
Member
 
Registered: Nov 2008
Location: Rye,NY
Distribution: Linux Mint
Posts: 33

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ntubski View Post
I think expect is the tool most suited to this task.



But this would obviously be the best option, if available.
I knew I seen something like that before. I couldnt remember it. THANK YOU!!!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] execute bash script from pySide script sharky Programming 3 12-23-2013 01:49 PM
Catch exit code before pipe in bash script? prl77 Linux - General 4 05-24-2012 12:09 PM
[bash script] How to catch the return string? thomas2004ch Linux - Software 3 02-27-2012 09:06 AM
Bash script: catch file not found error and send to /dev/null noir911 Programming 7 04-24-2010 08:37 PM
Can't execute Bash script. zbe Linux - Software 4 10-17-2008 08:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration