程序等待输入时如何通过命令行传递文件名

How to pass filename via command line when program waits for input

我有一个程序在执行后等待用户输入文件名。有没有办法在执行前使用 bash(或任何其他 shell)传递文件名? 由于它不读取参数,因此“<”不是一个选项,但也许有一种简单的方法来编写它的脚本? (运行 打几次之后会变得无聊 ;-) )

你可以试试:

echo $filename| yourprogram

不行的话看看expect

希望对您有所帮助:

这是测试脚本:

$ cat test.sh
read file
echo $file

现在,一个包含 path/filename 内容的文本文件:

$ cat /tmp/somefile
/var/log/messages

现在,魔法:

./test.sh < /tmp/somefile
/var/log/messages

这样,您就可以告诉您的脚本从文件内容中获取 "read" 信息。