关于Linux重定向的应用实例,请教了

sh/bash/dash/ksh/zsh等Shell脚本
回复
糊涂的小强
帖子: 71
注册时间: 2014-04-28 14:33
系统: ubuntu12&centos6

关于Linux重定向的应用实例,请教了

#1

帖子 糊涂的小强 » 2014-06-11 16:58

代码: 全选

#!/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
头像
susbarbatus
帖子: 2966
注册时间: 2010-04-10 16:14
系统: Arch Linux

Re: 关于Linux重定向的应用实例,请教了

#2

帖子 susbarbatus » 2014-06-11 17:01

没看懂这是要做什么,不过 exec 后面要接一个可执行的命令,这里估计是 exec $0 吧
沉迷将棋中……
头像
YeLee
论坛版主
帖子: 26406
注册时间: 2008-08-13 8:48
系统: Fundu i64
来自: 东海硇州,一双管钥。
联系:

Re: 关于Linux重定向的应用实例,请教了

#3

帖子 YeLee » 2014-06-11 17:04

exec后面到底想运行什么程序来着? :em20
◎当我站在道德的高度上俯视别人的时候,发现自己是多么渺小。
♥执着但不偏激,反对而不排斥,坚决捍卫矛盾体的存在方式。
★★★天气预报★★★
fcitx-yatable一个可以使用的码表输入法
[教程]几个实例攻克软件编译难关
Gentoo Development Guide
字体相关
糊涂的小强
帖子: 71
注册时间: 2014-04-28 14:33
系统: ubuntu12&centos6

Re: 关于Linux重定向的应用实例,请教了

#4

帖子 糊涂的小强 » 2014-06-11 17:06

susbarbatus 写了:没看懂这是要做什么,不过 exec 后面要接一个可执行的命令,这里估计是 exec $0 吧
就是把标准输入重定向到参数文件,然后用read去读这个文件
糊涂的小强
帖子: 71
注册时间: 2014-04-28 14:33
系统: ubuntu12&centos6

Re: 关于Linux重定向的应用实例,请教了

#5

帖子 糊涂的小强 » 2014-06-11 17:07

YeLee 写了:exec后面到底想运行什么程序来着? :em20
就是想把变准输入 重定向到 参数文件 $line
头像
YeLee
论坛版主
帖子: 26406
注册时间: 2008-08-13 8:48
系统: Fundu i64
来自: 东海硇州,一双管钥。
联系:

Re: 关于Linux重定向的应用实例,请教了

#6

帖子 YeLee » 2014-06-11 17:21

:em20
◎当我站在道德的高度上俯视别人的时候,发现自己是多么渺小。
♥执着但不偏激,反对而不排斥,坚决捍卫矛盾体的存在方式。
★★★天气预报★★★
fcitx-yatable一个可以使用的码表输入法
[教程]几个实例攻克软件编译难关
Gentoo Development Guide
字体相关
糊涂的小强
帖子: 71
注册时间: 2014-04-28 14:33
系统: ubuntu12&centos6

Re: 关于Linux重定向的应用实例,请教了

#7

帖子 糊涂的小强 » 2014-06-11 17:39

YeLee 写了::em20

代码: 全选

#!/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
糊涂的小强
帖子: 71
注册时间: 2014-04-28 14:33
系统: ubuntu12&centos6

Re: 关于Linux重定向的应用实例,请教了

#8

帖子 糊涂的小强 » 2014-06-11 18:06

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这一句 为什么不能写成exec 4<&0 0<$File或者exec 4<&0 &0<$File
头像
astolia
论坛版主
帖子: 6703
注册时间: 2008-09-18 13:11

Re: 关于Linux重定向的应用实例,请教了

#9

帖子 astolia » 2014-06-12 10:33

糊涂的小强 写了:

代码: 全选

#!/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

还是看 man bash 的解释
Note that the exec builtin command can make redirections take effect in
the current shell.
所以exec 4<&0 <$File就是把当前shell的文件描述符0的内容复制到文件描述符4,作为备份;再把当前shell的文件描述符0设置为$File
糊涂的小强 写了: 其中exec 4<&0 <$File这一句 为什么不能写成exec 4<&0 0<$File或者exec 4<&0 &0<$File
可以写成exec 4<&0 0<$File,效果是一样的。而&0<$File这种写法是错误的
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
所以一楼的报错因为是0和<之间不能有空格。
糊涂的小强
帖子: 71
注册时间: 2014-04-28 14:33
系统: ubuntu12&centos6

Re: 关于Linux重定向的应用实例,请教了

#10

帖子 糊涂的小强 » 2014-06-12 17:43

astolia 写了:
糊涂的小强 写了:

代码: 全选

#!/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

还是看 man bash 的解释
Note that the exec builtin command can make redirections take effect in
the current shell.
所以exec 4<&0 <$File就是把当前shell的文件描述符0的内容复制到文件描述符4,作为备份;再把当前shell的文件描述符0设置为$File

谢谢,昨天发帖时,脑呆昏昏沉沉的,被这个重定向弄糊涂了,其实这里exec 4<&0 <$File改成exec <$File,不影响结果,我当时可能是想不明白为什么要写成exec 4<&0 <$File,因为是别人举的例子,还以为必须这样呢,我其实是想搞清楚什么情况下需要另外定义一个文件描述符。。。其实是我没有理解重定向时复制的含义。学shell有段时间了,一直不得其法 唉
回复