随笔 - 56, 文章 - 0, 评论 - 0, 引用 - 0
数据加载中……

《LInux与Unix Shell编程指南》笔记之Shell输入与输出

                                                   Shell输入与输出
                              点击打开本章《LInux与Unix Shell变成指南》pdf文档
cat命令
    作用:可以显示文件内容,创建文件和显示控制字符。
    格式:
    cat [ options ] filename1 filename2 ...
    最常用的选项为:
    -v    显示控制字符
    例如:
    如果显示名为myfile文件
    cat myfile
    可能会出现内容太多的问题,原因是cat命令不会在文件分页符处停下来,即它会显示完整个文件。
    解决方法:可以使用more命令或把cat命令的输出通过管道传递到另外一个具有分页功能的命令,如:
    cat myfile | more 或
    cat myfile | pg
    如果显示myfile1、myfile2、myfile3的内容,为:
    cat myfile1 myfile2 myfile3
    如果希望创建一个新文件bigfile,里面包含myfile1、myfile2、myfile3文件的内容,则:
    cat myfile1 myfile2 myfile3 > bigfile
    如果希望创建一个新文件myfile,通过标准输入-键盘,输入完毕后按<CTRL -D>结束输入,相当于一个简单的
编辑器。如:
    [root@localhost test]# cat > myfile
    This 
is great.
    [root@localhost test]# cat myfile
    This 
is great.
    [root@localhost test]# 
    
    t命令
    作用:把输出的一个副本输送到标准输出,另一个副本拷贝到相应的文件中。
    格式为:
    tee -a files
    -a表示追加到文件尾。
    举例:
    [root@localhost /]# who
    root     pts
/0        2011-02-20 08:22 (:0.0)
    把上面的内容存到who.out中:
    [root@localhost /]# who | tee who.out
    root     pts
/0        2011-02-20 08:22 (:0.0)
    查看结果:
    [root@localhost /]# cat who.out
    root     pts
/0        2011-02-20 08:22 (:0.0)
    追加举例:
    [root@localhost /]# ls -| tee -a who.out
    总计 
242
    drwxr
-xr-x   2 root root  4096 02-18 13:07 bin
    drwxr
-xr-x   4 root root  1024 02-14 19:24 boot
    [root@localhost 
/]# cat who.out
    root     pts
/0        2011-02-20 08:22 (:0.0)
    总计 242
    drwxr-xr-x   2 root root  4096 02-18 13:07 bin
    drwxr-xr-x   4 root root  1024 02-14 19:24 boot
    
    标准输入与输出
    当我们在shell中执行命令的时候,每个进程都和三个打开的文件相联系,并使用文件描述符来引用这些文件。
    -------------------------------
        文件                        文件描述符
    -------------------------------
    输入文件--标准输入                    0   
    输出文件--标准输出                    1
    错误输出文件--标准错误            2
    -------------------------------
    系统中实际上有12个文件描述符,0、1、2如上所示,可以任意使用文件描述符3到9。
    下面是0、1、2的解释:
    0(标准输入),指命令的输入,缺省是键盘,也可以是文件或其他命令输入。
    1(标准输出),是命令的输出,缺省是屏幕,也可以是文件。
    2(标准错误),是命令错误的输出,缺省是屏幕,也可以是文件。
   
    文件重定向
    通过文件重定向,可以指定命令的标准输入、输出和错误。在对标准错误进行重定向时,必须使用文加描述符,而在
标准输出和输入时,可以不使用文件描述符。
                            常用文件重定向命令
    ---------------------------------------                                 
    command > filename                   把标准输出重定向到一个新文件中                           
    command >> filename                 把标准输出重定向到一个文件中 ( 追加 )                             
    command 1 > fielname                把标准输出重定向到一个文件中                            
    command > filename 2>&1         把标准输出和标准错误一起重定向到一个文件中                            
    command 2 > filename                把标准错误重定向到一个文件中                                             
    command 2 >> filename              把标准输出重定向到一个文件中 ( 追加 )                             
    command >> filename 2>&1       把标准输出和标准错误一起重定向到一个文件中 (加 )                             
    command < filename >filename2  command命令以filename文件作为标准输入,以filename2文
                                                       件作为标准输出                            
    command < filename                    command 命令以 filename 文件作为标准输入                           
    command << delimiter                 从标准输入中读入,直至遇到 delimiter 分界符                             
    command <&m                           把文件描述符 m 作为标准输入                             
    command >&m                           把标准输出重定向到文件描述符 m 中                             
    command <&-                            关闭标准输入
    ----------------------------------------
    举例:
    a、重定向标准输出:
    可以不使用文件描述符。
    如:
    先查看当前目录下有什么文件:
    [root@localhost test2]# ls
    file1  file2
    把上面的输出重定向到ls.out中,并输出查看:
    [root@localhost test2]# ls > ls.out
    [root@localhost test2]# cat ls.
out
    file1
    file2
    ls.out
    上面的ls.out刚创建,所以在前面ls命令中没有这个文件。上面的事例可以等同于:
    [root@localhost test2]# ls 1>ls.out
    [root@localhost test2]# cat ls.
out
    下面是追加的命令:
    [root@localhost test2]# ls 1>>ls.out
    [root@localhost test2]# cat ls.
out
    file1
    file2
    ls.
out
    file1
    file2
    ls.
out
    b、重定向标准输入:
    例如:
    先查看file1内容:
    [root@localhost test1]# cat > file1
    you can 
do it!
    然后把file1作为mail的标准输入:
    [root@localhost test1]# mail root < file1
    现在可以查看/var/spool/mail/root的内容看是否正确。
    上面的命令等同于:
    [root@localhost test1]# mail root 0<file1
    
    command << delimiter
    功能:shell将分界符delimiter之后直至下一个同样的分界符之前的所有内容都作为输入,遇到下一个分界符,
shell就知道输入结束了。比如用FINISH:
    [root@localhost test1]# cat >> file1 <<FINISH
    
> The user name is $LOGNAME
    
> bye
    
> FINISH
    [root@localhost test1]# cat file1
    The user name 
is root
    bye
    c、重定向标准错误:
    例如:
    [root@localhost test1]# grep "ok" nofile
    grep: nofile: 没有那个文件或目录
    现在把上面的错误重定向到myfile文件中,而且在屏幕中不回显。
    [root@localhost test1]# grep "ok" nofile 2>myfile
    查看myfile内容
    [root@localhost test1]# cat myfile
    grep: nofile: 没有那个文件或目录
    上面的例子一般情况下都使用">>"符号来作为追加内容,文件内之前的内容就不会被覆盖。
    d、结合使用标准输出和标准错误:
    指将输出重定向到一个文件中,然后把标准错误重定向到另一个文件中,如:
    [root@localhost test1]# cat file1
    just 
do it!
    [root@localhost test1]# cat myfile
    [root@localhost test1]# cat file1 nofile 1>myfile 2>myfile.err
    [root@localhost test1]# cat myfile.err
    cat: nofile: 没有那个文件或目录
    [root@localhost test1]# cat myfile
    just do it!
    e、合并标准输出和标准错误:
    指把输出的内容和错误的内容都重定向到同一个文件中,如:
    [root@localhost test1]# cat file1 nofile > myfile  2>&1 
    [root@localhost test1]# cat myfile
    just 
do it!
    cat: nofile: 没有那个文件或目录 

posted on 2011-02-20 11:16 八路 阅读(533) 评论(0)  编辑 收藏 引用 所属分类: 嵌入式linux笔记


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理