我可以在bash中调用交互式脚本吗?

时间:2020-04-26 13:59:12

标签: linux bash sh

例如,有一个交互式脚本,该脚本读取用户输入的姓名,然后将该姓名写到文件中。

#! /bin/bash 
read name
echo $name>>name.txt

如果我不能更改交互式脚本,如何使用anothor bash引用交互式脚本?

例如,我想编写一个bash,从文本中提取名称,然后调用此交互式脚本。

那么我能实现我的想法吗?

1 个答案:

答案 0 :(得分:2)

使用pipe将一个程序的输出与下一个程序的输入合并:

echo 'name from other script' | ./script1.sh

echo可以用任何其他可执行文件或脚本替换:

$ cat >script2.sh
#!/bin/sh
echo 'first input'
echo 'second input'
^D
$ chmod u+x script2.sh
$ ./script2.sh | ./script1.sh

如果您想要的输入名称已经在文件中,请改用 IO重定向

$ ./script2.sh < file_containing_name.txt
相关问题