Chinaunix

标题: 关于exec [打印本页]

作者: 秋池飞叶    时间: 2005-07-24 15:16
标题: 关于exec
如:
exec < tmp
然后用while 循环加read语句就可以依次读出 tmp里的每一行的内容。
但是我不理解为什么非要用exec?还有为什么执行完exec < tmp后 read就能依次读出该文本的内容那?
谢谢~
作者: carset    时间: 2005-07-24 16:03
标题: 关于exec
不是用非要 exec

< 重定向.
默认是 标准输入,也就是键盘. < 把标准输入重定向为了 文件 tmp

这么说可以明白么?
作者: 秋池飞叶    时间: 2005-07-24 20:27
标题: 关于exec
谢谢您~!
这个我明白,也许是我没表达清楚,以下是代码:


#!/bin/bash
# Script name: Speller
#
#
# Purpose: Check and fix spelling errors in a file
#
exec < /home/whl/testfile/tmp.log   # opens the tmp file
while read line  # read from the tmp file
do
echo $line
echo -en "Is this word correct? [Y/N] \n"
read answer < /dev/tty  # read from the terminal
    case $answer in
    [Yy]*)
        continue
            ;;
    *)
        echo "New word? "
        read word < /dev/tty
        sed "s/$line/$word/" /home/whl/testfile/tmp.log > error
        mv error tmp
        echo "$word has been changed."
            ;;
    esac
done

如果你把 exec < tmp 中的exec去掉,这个脚本就不能正常执行。也就是说read无法读出tmp文件里的内容
作者: waker    时间: 2005-07-24 20:59
标题: 关于exec
不为什么,就这么约定的
看看置顶13问与shaobing兄整理的IO重定向的精华帖
作者: 秋池飞叶    时间: 2005-07-25 11:15
标题: 关于exec
谢谢老大的提示
我看了下,后来我把exec < /home/whl/testfile/tmp.log   那行注释掉,执行的时候用$0 </home/whl/testfile/tmp.log   也可以正常运行。
可是对于exec我还了解的不是很透彻~继续研究中。。。
老大能给些这方面的好的资料看看不??




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2