How to automatically answer interactive cli program (not script) with bash script?

10,346

With parted, you can just add the -s option:

parted -a optimal -s /dev/sda mklabel msdos

From the Trusty man page for parted:

[...]
       -s, --script
              never prompts for user intervention
[...]
Share:
10,346

Related videos on Youtube

Mas Bagol
Author by

Mas Bagol

Updated on September 18, 2022

Comments

  • Mas Bagol
    Mas Bagol over 1 year

    I've googled this, and almost all I could find is to answer a bash script and this doesn't work for me. The answers I found told to do:

    echo "yes" | ./script
    

    or

    ./script <<< yes
    

    or something like that. It didn't work for me. My guess is that's because what I want to do is to auto answer an executable program, not a script. More specifically, I want to auto answer parted. Here

    parted -a optimal /dev/sda mklabel msdos
    Warning: The existing disk label on /dev/sda will be destroyed and
    all data on this disk will be lost. Do you want to continue?
    Yes/No? _
    

    I try to do

    echo "yes" | parted -a optimal /dev/sda mklabel msdos
    

    and

    parted -a optimal /dev/sda mklabel msdos <<< yes
    

    Both methods didn't work. Those didn't answer yes to parted prompt.

    So, how can I automatically answer that parted prompt without using a bash script?

    • j0h
      j0h about 9 years
      There is a language for specifically that purpose, to automate interactive programs. it is called "expect" and you can call it from within a bash script.
    • Eliah Kagan
      Eliah Kagan about 9 years
  • Mas Bagol
    Mas Bagol about 9 years
    never prompts ... does it means, yes to all?
  • A.B.
    A.B. about 9 years
    @BagolDaplun Never ask in this case means that the default is used.
  • kos
    kos about 9 years
    @BagolDaplun As A.B. said -s makes parted use the default (which is yes). Also I corrected the command because here you can't combine options since -a needs an argument.