0

I'm trying to write a script to automate installation of a program in Linux. It requires some user input during installation. Most are straight forward like simple y or n. I add these responses to a file responses.txt and just pipe it to the installation script like this: ./start < responses.txt.

However, complications arise at a particular point in the installation. The program asks the user to hit CTRL+C at a particular point if they want to skip installation of a particular component. If you don't hit CTRL+C during this limited time window, the script goes ahead and installs this component.

I want to install the program without this component. How can I send CTRL+C during this time window programmatically?

Based on my limited understanding, my automated solution would need a way to detect when the installation asks the user to send these keystrokes and then my script should send some sort of interrupt?

Is it even possible to do something like this?

wiseindy
  • 19,434
  • 5
  • 27
  • 38
  • 1
    Can you update the script, or avoid calling it in the first place? It's really difficult when programs don't follow good UNIX practices and are difficult to automate. A good install script isn't only interactive but can also be controlled by command-line arguments or a configuration file. – John Kugelman Jul 30 '18 at 14:17
  • 1
    When you hit CTRL-C (in a unix-like environment), you are sending SIGINT to the foreground process. Very likely, you just need to use `kill` to send the signal. – William Pursell Jul 30 '18 at 14:17
  • @JohnKugelman you're right. I've to explore more options such as probably modifying the installation script. Seems it isn't going to be straightforward. – wiseindy Jul 30 '18 at 15:01
  • take a look at [*sexpect*](https://github.com/clarkwang/sexpect) with which you can start a program in background and talk to it with shell scripts. – pynexj Jul 30 '18 at 15:33
  • Piping a `CTRL+C` character to the program will not terminate it.
    See here for more information on why this is: https://stackoverflow.com/questions/6196321/echo-control-c-character Thus you will have to think of a different way to automate this.
    – Quentin Kniep Jul 30 '18 at 14:13

2 Answers2

1

The Ctlr C character when entered at the keyboard causes the device driver or pseudo terminal to send an interrupt signal (SIGINT). Hence, the actual Ctlr C never reaches the program. The exception to this is if the terminal has been set in raw mode, but this is unlikely.

You can send a SIGINT signal using the kill command:

kill -2 pid

The challenge for you in your script will be to find the process that you need to kill and to interleave the signal with the rest of your input.

You may be able to find the process-id using ps and grep, if you know what it is called.

You can try interleaving the fixed input and the kill signal by replacing your simple text file with a script and pipe the output of that script into your installer. For example:

( echo "response 1"
  echo "response-2"
  sleep 1
  pid=$(ps -something | grep something | sed something)
  kill -2 $pid
) | installer

That might give you an idea how it could be done.

However, as suggested elsewhere, there are tools that can do this kind of thing for you. echo

rghome
  • 8,529
  • 8
  • 43
  • 62
0

If you want to simulate interaction with another program, expect is generally the appropriate tool. And according to this other SO post, it can easily send Ctlr C characters that will be interpreted as SIGINT by the slave process.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252