Home  >  Q&A  >  body text

How to use `exec` to execute a command with multiple steps in bash

I have a multi-step command that asks several questions along the way. I need to do it in code.

php command.php
> 你叫什么名字?
User Smith
> 你多大了?
25
> 你已经够大了!

But when I call it with exec it gets stuck.

What I expect is:

<?php

$result = exec('php command.php')->next('User Smith')->next('25');

if ($result->response === '你已经够大了!') echo "Yahoo";
else  echo "哦不!";

P粉659378577P粉659378577287 days ago388

reply all(1)I'll reply

  • P粉464113078

    P粉4641130782024-01-30 09:38:26

    We can use the pipe operator to chain commands. We can do something like this:

    exec('(echo 'User Smith' & echo '25') | php command.php')

    The direction is from right to left.

    Pipe multiple commands into a single command

    reply
    0
  • Cancelreply