Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 726|回复: 0

linux过滤器及管道

[复制链接]
  • TA的每日心情
    开心
    2023-3-18 00:22
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2013-3-11 23:15:29 | 显示全部楼层 |阅读模式

    Linux中的应用工具

    Linux中的应用工具分为三种:交互工具,过滤器,编辑器

    输入:过滤器的数据来源

    输出:过滤器的数据去向

    重定向:标准输入、输出与错误输出,都可用特定符号改变数据来源或去向。


    标准输入、标准输出、标准错误输出,例:

    [root@redhat root]# ls -l /dev/std*

    lrwxrwxrwx 1 root root 17 2011-10-25 /dev/stderr -> ../proc/self/fd/2

    lrwxrwxrwx 1 root root 17 2011-10-25 /dev/stdin -> ../proc/self/fd/0

    lrwxrwxrwx 1 root root 17 2011-10-25 /dev/stdout -> ../proc/self/fd/1

    以上分别人标准输入(stdin(0))、标准输出(stdout(1))、标准错误输出(stderr(2)),所指向的文件。


    输入重定向:

    使用"<"来重定向输入源,大多数据工具都会以其后的文件名作为输入源,例:

    [root@redhat a]# cat test.txt

    HELLO WORLD !

    I AM LUOWEI !

    WELCOME TO LINUX'S WORLD !

    [root@redhat a]# tr 'A-Z' 'a-z' < test.txt     将test.txt文件中的内容中的大写改变成小写输出

    hello world !

    i am luowei !

    welcome to linux's world !

    [root@redhat a]#


    从当前文档输入:使用 << 让系统将一次键盘的全部输入,先送入虚拟的"当前文档",再一次性输入。

    需要一对字母、符号或字符串作为起始终结符。可以选择任意符号作为起始终结标识符。例:

    [root@redhat a]# cat test2.txt

    [root@redhat a]# cat > test2.txt <<!

    > this is test.

    > writted by luowei !

    > !

    [root@redhat a]# cat test2.txt

    this is test.

    writted by luowei !

    [root@redhat a]#


    输出重定向:

    例:

    [root@redhat a]# ls -l > test3.txt     将输出的内之余输出到test3.txt文件中

    [root@redhat a]# cat test3.txt

    总用量 20

    drwxrwxr-- 2 root root 4096 10月 25 00:32 a_dir

    drwxr-xr-x 2 root root 4096 10月 25 00:32 b_dir

    -rw-rw-r-- 1 root root 34 10月 25 01:14 test2.txt

    -rw-r--r-- 1 root root 0 10月 25 01:17 test3.txt

    -rw-r--r-- 1 root root 206 10月 25 01:04 test.txt

    请尝试执行'ls --help'来获取更多信息。

    [root@redhat a]# ls --help >>test3.txt

    [root@redhat a]# cat test3.txt         把输出的内容追加到test3.txt

    总用量 20

    drwxrwxr-- 2 root root 4096 10月 25 00:32 a_dir

    drwxr-xr-x 2 root root 4096 10月 25 00:32 b_dir

    -rw-rw-r-- 1 root root 34 10月 25 01:14 test2.txt

    -rw-r--r-- 1 root root 0 10月 25 01:17 test3.txt

    -rw-r--r-- 1 root root 206 10月 25 01:04 test.txt

    用法:ls [选项]... [文件]...

    列出<文件>的信息 (默认为目前的目录)。

    如果不指定 -cftuSUX 或 --sort 任何一个选项,则根据字母大小排序。


    长选项必须用的参数在使用短选项时也是必须的。

    -a, --all 不隐藏任何以 . 字符开始的项目

    -A, --almost-all 列出除了 . 及 .. 以外的任何项目

    ………….

    [root@redhat a]# ls-l 2>test.txt 将标准错误输出到文件中,在">"符号前加个"2"

    [root@redhat a]# cat test.txt

    -bash: ls-l: command not found

    [root@redhat a]# su - luowei

    [luowei@redhat luowei]$ find /etc -name passwd > stdout

    find: /etc/sysconfig/pgsql: 权限不够

    find: /etc/default: 权限不够

    find: /etc/httpd/conf/ssl.crl: 权限不够

    find: /etc/httpd/conf/ssl.crt: 权限不够

    find: /etc/httpd/conf/ssl.csr: 权限不够

    find: /etc/httpd/conf/ssl.key: 权限不够

    find: /etc/httpd/conf/ssl.prm: 权限不够

    find: /etc/cups/certs: 权限不够

    [luowei@redhat luowei]$ cat stdout

    /etc/passwd

    /etc/pam.d/passwd

    [luowei@redhat luowei]$ find /etc -name passwd 2>stderr

    /etc/passwd

    /etc/pam.d/passwd

    [luowei@redhat luowei]$ cat stderr

    find: /etc/sysconfig/pgsql: 权限不够

    find: /etc/default: 权限不够

    find: /etc/httpd/conf/ssl.crl: 权限不够

    find: /etc/httpd/conf/ssl.crt: 权限不够

    find: /etc/httpd/conf/ssl.csr: 权限不够

    find: /etc/httpd/conf/ssl.key: 权限不够

    find: /etc/httpd/conf/ssl.prm: 权限不够

    find: /etc/cups/certs: 权限不够

    [luowei@redhat luowei]$


    双重定向输入输出

    例:

    [luowei@redhat luowei]$ find /etc -name passwd 2> stderr > stderr > stdout

    [luowei@redhat luowei]$ cat stderr

    find: /etc/sysconfig/pgsql: 权限不够

    find: /etc/default: 权限不够

    find: /etc/httpd/conf/ssl.crl: 权限不够

    find: /etc/httpd/conf/ssl.crt: 权限不够

    find: /etc/httpd/conf/ssl.csr: 权限不够

    find: /etc/httpd/conf/ssl.key: 权限不够

    find: /etc/httpd/conf/ssl.prm: 权限不够

    find: /etc/cups/certs: 权限不够

    [luowei@redhat luowei]$ cat stdout

    /etc/passwd

    /etc/pam.d/passwd

    [luowei@redhat luowei]$ ls

    stderr stdout test.txt

    [luowei@redhat luowei]$


    将正确定的输出信息与错误的信息输出到同一个地方(文件)

    [luowei@redhat luowei]$ find /etc -name passwd &> allout

    [luowei@redhat luowei]$ cat allout

    find: /etc/sysconfig/pgsql: 权限不够

    /etc/passwd

    find: /etc/default: 权限不够

    /etc/pam.d/passwd

    find: /etc/httpd/conf/ssl.crl: 权限不够

    find: /etc/httpd/conf/ssl.crt: 权限不够

    find: /etc/httpd/conf/ssl.csr: 权限不够

    find: /etc/httpd/conf/ssl.key: 权限不够

    find: /etc/httpd/conf/ssl.prm: 权限不够

    find: /etc/cups/certs: 权限不够


    使用 &> 也可所有的输入都送到同一个地方

    [luowei@redhat luowei]$ find /etc -name passwd &> allout > allout 2>&1

    [luowei@redhat luowei]$


    管道

    使用 | 将前现代战争过滤器的输出直接送入后一个过滤器的输入。

    例:

    允许多重管道

    注意管道前过滤器的输出与管道后过滤器的输入数据类型匹配。

    例:

    [root@redhat root]# ls -l /etc |more 列出目录并实现翻页的效果

    [root@redhat root]# ls -l /etc |more |grep fstab         使用多重管道

    拷贝

    [root@redhat root]# echo "hello" > filea

    [root@redhat root]# cat filea

    hello

    [root@redhat root]# touch fileb

    [root@redhat root]# cat < filea > fileb        把filea里的内容拷贝到fileb

    [root@redhat root]# cat fileb

    hello

    [root@redhat root]#

    将多个文件的内容输出合并到同一个文件

    [root@redhat root]# cat filea;cat fileb         查看两个文件的内容

    hello

    hello

    [root@redhat root]# cat filea fileb > file    将filea与fileb的内容输出到file

    [root@redhat root]# cat file

    hello

    hello

    [root@redhat root]# echo "hello luowei" > fileb

    [root@redhat root]# cat file* > file            将当前目录下以file打头的文件内容输出到file

    cat: file: input file is output file

    [root@redhat root]# cat file

    hello

    hello luowei

    [root@redhat root]#




    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-23 15:45 , Processed in 0.424377 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表