Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash shell:使用输入重定向使用标准输入运行脚本_Bash_Shell - Fatal编程技术网

Bash shell:使用输入重定向使用标准输入运行脚本

Bash shell:使用输入重定向使用标准输入运行脚本,bash,shell,Bash,Shell,我有这个脚本我的程序 #!/bin/bash echo This is the first one ${1}. echo This is the second one ${2}. 以及输入文件test.txt Hi Hello 我希望使用输入重定向来使用test.txt的输入运行脚本,它应该输出 This is the first one Hi. This is the second one Hello. 我想用 ./myprogram < test.txt 有人能帮我吗?位置参数

我有这个脚本我的程序

#!/bin/bash
echo This is the first one ${1}.
echo This is the second one ${2}.
以及输入文件test.txt

Hi
Hello
我希望使用输入重定向来使用test.txt的输入运行脚本,它应该输出

This is the first one Hi.
This is the second one Hello.
我想用

./myprogram < test.txt
有人能帮我吗?

位置参数(又名命令行参数)与stdin无关。下面是一个同时使用这两种方法的示例:

$ cat myscript
#!/bin/bash
echo "These are the first two arguments: $1 and $2"
read -r first
echo "This is the first input line on stdin: $first"
read -r second
echo "This is the second input line on stdin: $second"

$ ./myscript foo bar < test.txt
These are the first two arguments: foo and bar
This is the first input line on stdin: Hi
This is the second input line on stdin: Hello
$cat myscript
#!/bin/bash
echo“这是前两个参数:$1和$2”
先读r
echo“这是标准输入上的第一行:$first”
读r秒
echo“这是标准输入上的第二个输入行:$second”
$./myscript foo bar
位置参数(又称命令行参数)与stdin无关。下面是一个同时使用这两种方法的示例:

$ cat myscript
#!/bin/bash
echo "These are the first two arguments: $1 and $2"
read -r first
echo "This is the first input line on stdin: $first"
read -r second
echo "This is the second input line on stdin: $second"

$ ./myscript foo bar < test.txt
These are the first two arguments: foo and bar
This is the first input line on stdin: Hi
This is the second input line on stdin: Hello
$cat myscript
#!/bin/bash
echo“这是前两个参数:$1和$2”
先读r
echo“这是标准输入上的第一行:$first”
读r秒
echo“这是标准输入上的第二个输入行:$second”
$./myscript foo bar
您的脚本从未尝试从标准输入读取,因此您在stdin上重定向的内容没有任何效果。您的脚本从未尝试从标准输入读取,因此您在stdin上重定向的内容没有任何效果。