How can I pipe initial input into process which will then be interactive?

后端 未结 4 1452
长发绾君心
长发绾君心 2020-11-27 03:56

I\'d like to be able to inject an initial command into the launching of an interactive process, so that I can do something like this:

echo \"initial command\         


        
4条回答
  •  Happy的楠姐
    2020-11-27 04:04

    This is easy to do with the program "expect" which is intended to let you write scripts to interact with programs.

    I tested this by writing an expect script bc.exp to launch the calculator "bc" and send it the command "obase=16" to put it into hexadecimal output mode, and then turn over control to me.

    The script (in a file named bc.exp) is

    spawn bc
    send "obase=16\n"
    interact {
     \003 exit
    }
    

    One runs it with

    expect bc.exp
    

提交回复
热议问题