有没有办法 "echoing" 将被重定向为程序(Unix)的标准输出的内容?
Is there a way of "echoing" to stdout what is being redirected as stdin to a program (Unix)?
假设我有一个程序可以从控制台获取有关用户的信息。
$ ./program
Enter your name : Foo
Enter your phone number : Bar
Your name is Foo and phone number Bar.
现在如果我不想手动输入 "Foo" 和 "Bar",而是想从文件重定向输入...
inputfile.txt
Foo
Bar
输出结果是这样的...
$ ./program < inputfile.txt
Enter your name :
Enter your phone number :
Your name is Foo and phone number Bar.
通过重定向,您无法看到冒号后输入的内容。有没有办法让输入在控制台上可见(如第一个示例)?
编辑:这基本上与该线程提出的问题相同:
https://unix.stackexchange.com/questions/228954/c-how-to-redirect-from-a-file-to-cin-and-display-as-if-user-typed-the-input
但是我只找到了关于更改程序和添加功能的建议isatty
,但是有没有不更改现有程序的方法?
这取决于您的程序。我找到了一个程序的技巧
printf "%s : " "Enter your name"
read name
printf "%s : " "Enter your phone number"
read phone
echo "Your name is ${name} and phone number ${phone}"
这里可以使用
while read -r line; do
sleep 1
echo "${line}"
done < inputfile.txt | tee >(./program)
程序改成
会失败
read -p "Enter your name : " name
read -p "Enter your phone number : " phone
echo "Your name is ${name} and phone number ${phone}"
所以你可以测试这个解决方案并希望最好。
tee /dev/tty < inputfile.txt | program
将回显文件的内容,但它不会与您的提示匹配。它看起来像
$ tee /dev/tty < inputfile.txt | ./program
Foo
Bar
Enter your name :
Enter your phone number :
Your name is Foo and phone number Bar.
我不认为有一种方法可以让所有内容都按您想要的方式对齐。
假设我有一个程序可以从控制台获取有关用户的信息。
$ ./program
Enter your name : Foo
Enter your phone number : Bar
Your name is Foo and phone number Bar.
现在如果我不想手动输入 "Foo" 和 "Bar",而是想从文件重定向输入...
inputfile.txt
Foo
Bar
输出结果是这样的...
$ ./program < inputfile.txt
Enter your name :
Enter your phone number :
Your name is Foo and phone number Bar.
通过重定向,您无法看到冒号后输入的内容。有没有办法让输入在控制台上可见(如第一个示例)?
编辑:这基本上与该线程提出的问题相同: https://unix.stackexchange.com/questions/228954/c-how-to-redirect-from-a-file-to-cin-and-display-as-if-user-typed-the-input
但是我只找到了关于更改程序和添加功能的建议isatty
,但是有没有不更改现有程序的方法?
这取决于您的程序。我找到了一个程序的技巧
printf "%s : " "Enter your name"
read name
printf "%s : " "Enter your phone number"
read phone
echo "Your name is ${name} and phone number ${phone}"
这里可以使用
while read -r line; do
sleep 1
echo "${line}"
done < inputfile.txt | tee >(./program)
程序改成
会失败read -p "Enter your name : " name
read -p "Enter your phone number : " phone
echo "Your name is ${name} and phone number ${phone}"
所以你可以测试这个解决方案并希望最好。
tee /dev/tty < inputfile.txt | program
将回显文件的内容,但它不会与您的提示匹配。它看起来像
$ tee /dev/tty < inputfile.txt | ./program
Foo
Bar
Enter your name :
Enter your phone number :
Your name is Foo and phone number Bar.
我不认为有一种方法可以让所有内容都按您想要的方式对齐。