Ayra
Ayra

Reputation: 348

Send Ansible expect (or shell) module a backspace (to answer a scrpit in term)

- name : Execute the script and answer to question in term
  expect:
    command: ./"{{ script_name }}"
    responses:
      Question:
        - ''
        - ''
        - ''
        - 'backspace' #?


  become: yes

I'd like to use backspace because the script i'm using is answering in advance so that I just have to press enter if i'm ok but for one question i'd like to change the given answer. But so far i didn't find a way to use backspace. Anyone would know ?

Upvotes: 2

Views: 1160

Answers (1)

kabanus
kabanus

Reputation: 26005

The ASCII control for backspace is '\b'. This works with double quotes:

- name : Execute the script and answer to question in term
  expect:
  command: "echo Q"
  echo: "yes"
  responses:
   Q: "b\bc"

for example returns prints to stdout 1^Hc, where ^H is indeed the backspace control.

This may only move the cursor backwards though, not erase the previous character. Just for example, in my bash shell:

/bin/bash -c "echo a$'\b'"

produces a as output, but

/bin/bash -c "echo a$'\b'c"

produces c, since the cursor was moved back, than the a was overwritten.

Upvotes: 3

Related Questions