代码: 全选
#!/bin/bash
if [ $# -ge 1 ];
then
for line in $@
do
echo $line
exec 0 >$line
while read reply
do
echo $reply
done
exec 0<$line
done
fi
./test_arg test_read
test_read
./test_arg: line 7: exec: 0: not found
代码: 全选
#!/bin/bash
if [ $# -ge 1 ];
then
for line in $@
do
echo $line
exec 0 >$line
while read reply
do
echo $reply
done
exec 0<$line
done
fi
./test_arg test_read
test_read
./test_arg: line 7: exec: 0: not found
就是把标准输入重定向到参数文件,然后用read去读这个文件susbarbatus 写了:没看懂这是要做什么,不过 exec 后面要接一个可执行的命令,这里估计是 exec $0 吧
就是想把变准输入 重定向到 参数文件 $lineYeLee 写了:exec后面到底想运行什么程序来着?
YeLee 写了:
代码: 全选
#!/bin/bash
if [ $# -ge 1 ];
then
for File in $@
do
echo $File
exec 4<&0 <$File
while read line
do
echo $line
done
exec 0<&4 4<&-
done
fi
原始代码如下susbarbatus 写了:没看懂这是要做什么,不过 exec 后面要接一个可执行的命令,这里估计是 exec $0 吧
代码: 全选
#!/bin/bash
if [ $# -ge 1 ];
then
for File in $@
do
echo $File
exec 4<&0 <$File
while read line
do
echo $line
done
exec 0<&4 4<&-
done
fi
糊涂的小强 写了:其实原始代码是这样的,但是这条语句怎么解释呢exec 4<&0 <$File代码: 全选
#!/bin/bash if [ $# -ge 1 ]; then for File in $@ do echo $File exec 4<&0 <$File while read line do echo $line done exec 0<&4 4<&- done fi
所以exec 4<&0 <$File就是把当前shell的文件描述符0的内容复制到文件描述符4,作为备份;再把当前shell的文件描述符0设置为$FileNote that the exec builtin command can make redirections take effect in
the current shell.
可以写成exec 4<&0 0<$File,效果是一样的。而&0<$File这种写法是错误的糊涂的小强 写了: 其中exec 4<&0 <$File这一句 为什么不能写成exec 4<&0 0<$File或者exec 4<&0 &0<$File
所以一楼的报错因为是0和<之间不能有空格。Redirecting Input
Redirection of input causes the file whose name results from the expan‐
sion of word to be opened for reading on file descriptor n, or the
standard input (file descriptor 0) if n is not specified.
The general format for redirecting input is:
[n]<word
谢谢,昨天发帖时,脑呆昏昏沉沉的,被这个重定向弄糊涂了,其实这里exec 4<&0 <$File改成exec <$File,不影响结果,我当时可能是想不明白为什么要写成exec 4<&0 <$File,因为是别人举的例子,还以为必须这样呢,我其实是想搞清楚什么情况下需要另外定义一个文件描述符。。。其实是我没有理解重定向时复制的含义。学shell有段时间了,一直不得其法 唉astolia 写了:糊涂的小强 写了:其实原始代码是这样的,但是这条语句怎么解释呢exec 4<&0 <$File代码: 全选
#!/bin/bash if [ $# -ge 1 ]; then for File in $@ do echo $File exec 4<&0 <$File while read line do echo $line done exec 0<&4 4<&- done fi
还是看 man bash 的解释所以exec 4<&0 <$File就是把当前shell的文件描述符0的内容复制到文件描述符4,作为备份;再把当前shell的文件描述符0设置为$FileNote that the exec builtin command can make redirections take effect in
the current shell.
。