How to use `exec` to execute a command with multiple steps in bash
P粉659378577
P粉659378577 2024-01-29 12:57:56
0
1
378

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粉659378577
P粉659378577

reply all(1)
P粉464113078

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!