• 标准io和管道练习


         标准IO和管道实验练习

    【例1】把/etc/fstab文件内容重定向到/tmp目录下文件名为fstab.out
    写法:

    13:54:35 root@centos ~]#cat /etc/fstab > /tmp/fstab.out
    [13:55:02 root@centos ~]#cat /tmp/fstab.out
    
    #
    # /etc/fstab
    # Created by anaconda on Fri Sep 20 14:23:49 2019
    #
    # Accessible filesystems, by reference, are maintained under '/dev/disk'
    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
    #
    UUID=b7becd8b-fb18-48cf-810b-953944dcf82e /                       xfs     defaults        0 0
    UUID=a74c9411-1dd4-44ff-929d-ba505baaec2c /boot                   xfs     defaults        0 0
    UUID=cc79eddd-a461-46e9-96ab-3489b7de0db3 /data                   xfs     defaults        0 0
    UUID=23047094-ac08-4c27-bfd9-f6cb8c34b185 swap                    swap    defaults        0 0
    

    【例2】把hello world追加到/tmp/fstab.out文件尾部
    写法:

    [13:57:46 root@centos ~]#echo hello world >> /tmp/fstab.out
    [13:58:02 root@centos ~]#cat /tmp/fstab.out
    
    #
    # /etc/fstab
    # Created by anaconda on Fri Sep 20 14:23:49 2019
    #
    # Accessible filesystems, by reference, are maintained under '/dev/disk'
    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
    #
    UUID=b7becd8b-fb18-48cf-810b-953944dcf82e /                       xfs     defaults        0 0
    UUID=a74c9411-1dd4-44ff-929d-ba505baaec2c /boot                   xfs     defaults        0 0
    UUID=cc79eddd-a461-46e9-96ab-3489b7de0db3 /data                   xfs     defaults        0 0
    UUID=23047094-ac08-4c27-bfd9-f6cb8c34b185 swap                    swap    defaults        0 0
    /hello world
    hello world
    

    【例3】禁止覆盖重定向和强制重定向
    写法:

    [14:00:32 root@centos ~]#set -C
    [14:00:39 root@centos ~]#echo hello yang > /tmp/fstab.out 
    -bash: /tmp/fstab.out: cannot overwrite existing file
    [14:02:12 root@centos ~]#echo hello yang >| /tmp/fstab.out
    [14:02:26 root@centos ~]#cat /tmp/fstab.out 
    hello yang
    

    【例4】解除禁止覆盖重定向设置
    写法:

    [14:03:15 root@centos ~]#set +C
    [14:05:01 root@centos ~]#echo hello JIAK > /tmp/fstab.out
    [14:05:43 root@centos ~]#cat /tmp/fstab.out 
    hello JIAK
    

    【例5】把标准错误覆盖重定向到which.out文件
    先看下只重定向标准输出的情况:
    写法:

    [14:11:57 root@centos ~]#hosname > /which.out
    bash: hosname: command not found...
    [14:13:50 root@centos ~]#hosname 2> /which.out
    [14:13:36 root@centos ~]#cat /which.out
    bash: hosname: command not found...
    

    【例6】把标准错误和标准输出分别重覆盖定向到不同的文件里,即标准错误重定向到falt.txt文件,标准输出重 定向到correct.txt
    写法:

    [14:34:16 root@centos ~]#hosname 2> /falt.txt 
    [14:35:27 root@centos ~]#hostname > /correct.txt 
    [14:35:43 root@centos ~]#cat /falt.txt 
    bash: hosname: command not found...
    [14:35:53 root@centos ~]#cat /correct.txt 
    centos.yang.com
    

    【例7】用输入重定向的方式,把所有小写字母转换为大写
    写法:

    [15:02:54 root@centos ~]#cat /etc/issue
    S
    Kernel 
     on an m
    
    [15:07:48 root@centos ~]#cat /etc/issue | tr "a-z" "A-Z"
    S
    KERNEL R ON AN M
    
    

    【例8】把out.txt文件里的内容,写到file.txt文件里
    写法:

    15:11:47 root@centos ~]#cat >/file.txt</out.txt
    [15:11:57 root@centos ~]#cat /file.txt
    /usr/bin/cat
    

    【例9】屏幕随便输入几行内容,遇到END字样结尾后,屏幕内容自动保存在f1.txt里
    写法:

    [15:14:31 root@centos ~]#cat >f1.txt <<END
    > hello jiak
    > i am yang
    > END
    [15:17:17 root@centos ~]#cat f1.txt
    hello jiak
    i am yang  
    

    【例10】把echo输出的内容,传递给tr命令,实现小写字母转换为大写字母
    写法:

    [16:11:38 root@centos ~]#echo {a..z}
    a b c d e f g h i j k l m n o p q r s t u v w x y z
    [16:11:44 root@centos ~]#echo {a..z} | tr "a-z" "A-Z"
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
    

    【例11】一页一页的查看输入
    写法:

    [16:12:05 root@centos ~]#ls -l /etc |less   
    total 1420
    drwxr-xr-x.  3 root root      101 Sep 20 14:25 abrt
    -rw-r--r--.  1 root root       16 Sep 20 14:30 adjtime
    -rw-r--r--.  1 root root     1518 Jun  7  2013 aliases
    -rw-r--r--.  1 root root    12288 Sep 20 14:32 aliases.db
    drwxr-xr-x.  3 root root       65 Sep 20 14:26 alsa
    drwxr-xr-x.  2 root root     4096 Sep 20 14:27 alternatives
    -rw-------.  1 root root      541 Aug  9 07:07 anacrontab
    -rw-r--r--.  1 root root       55 Aug  8 19:47 asound.conf
    -rw-r--r--.  1 root root        1 Oct 31  2018 at.deny
    drwxr-x---.  3 root root       43 Sep 20 14:25 audisp
    drwxr-x---.  3 root root       83 Sep 20 14:32 audit
  • 相关阅读:
    小节 +三元表达式
    continue
    break
    flag标签
    #region #endregion
    for 循环
    do while 有例句体会循环的真正原理
    while 循环
    前缀和与差分
    递归的循环实现
  • 原文地址:https://www.cnblogs.com/www233ii/p/11603530.html
Copyright © 2020-2023  润新知