使用bash调用另一个shell脚本并指定输入

使用bash调用另一个shell脚本并指定输入,bash,shell,Bash,Shell,我试图用bash调用另一个shell脚本,但是这个脚本不接受任何命令行参数。相反,它会运行、检查一些内容,然后提示用户输入如下内容: Checking....done Please enter ID #: (user input here) 可以这样做吗?试试这样的方法: echo "My id" | ./script Checking... done Please enter ID: My id 或 试着这样做: echo "My id" | ./script Checking... do

我试图用bash调用另一个shell脚本,但是这个脚本不接受任何命令行参数。相反,它会运行、检查一些内容,然后提示用户输入如下内容:

Checking....done
Please enter ID #: (user input here)

可以这样做吗?

试试这样的方法:

echo "My id" | ./script
Checking... done
Please enter ID: My id


试着这样做:

echo "My id" | ./script
Checking... done
Please enter ID: My id

解决方案是使用expect

参考资料:

解决方案是使用expect


参考资料:

如果您有一个输入,那么echo就足够了

回显输入|./script.sh

如果您有1个以上的输入,那么expect是好的,也是唯一的选项

如上所述录制脚本,然后运行它


expect sampleexpect.expec

如果您有一个输入要馈送,那么echo就足够了

回显输入|./script.sh

如果您有1个以上的输入,那么expect是好的,也是唯一的选项

如上所述录制脚本,然后运行它


expect sampleexpect.expec

您正在寻找read命令吗?有点相反。我试图调用的脚本是使用read命令获取用户输入并进行相应的处理。您正在寻找read命令吗?有点相反。我试图调用的脚本使用read命令获取用户输入并相应地处理它。
#!/usr/bin/expect
spawn php /path/to/script
expect "ID #: "
send "{ID#}\r"
interact