linux命令基础知识 管道流,linux基础知识-I/O重定向,管道(示例代码)
系統設定
默認輸出設備:標準輸出,STDOUT, 1
默認輸入設備:標準輸入, STDIN, 0
標準錯誤輸出:STDERR, 2
標準輸入:鍵盤
標準輸出和錯誤輸出:顯示器
I/O重定向:
Linux:
>: 覆蓋輸出
[[email?protected] ~]# ll /var/ > /tmp/var.out
[[email?protected] ~]# cat /tmp/var.out
total 76
drwxr-xr-x.? 2 root root 4096 Jun 21? 2015 account
drwxr-xr-x. 13 root root 4096 Jun 21? 2015 cache
drwxr-xr-x.? 2 root root 4096 Jun 21? 2015 crash
drwxr-xr-x.? 3 root root 4096 Jun 21? 2015 db
drwxr-xr-x.? 3 root root 4096 Jun 21? 2015 empty
drwxr-xr-x.? 2 root root 4096 Jun 28? 2011 games
#
# /etc/fstab
# Created by anaconda on Sun Jun 21 02:15:00 2015
#
# 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=57d85756-7680-4c7c-9125-6ad67dae2c45 /?????????????????????? ext4??? defaults??????? 1 1
UUID=2622a4b4-ddc9-47a3-aa2b-f06bc9bec085 /boot?????????????????? ext4??? defaults??????? 1 2
UUID=33d94759-fa01-4c4f-b4ac-bf3a1fe5e84f swap??????????????????? swap??? defaults??????? 0 0
tmpfs?????????????????? /dev/shm??????????????? tmpfs?? defaults??????? 0 0
devpts????????????????? /dev/pts??????????????? devpts? gid=5,mode=620? 0 0
sysfs?????????????????? /sys??????????????????? sysfs?? defaults??????? 0 0
proc??????????????????? /proc?????????????????? proc??? defaults??????? 0 0
>>:追加輸出
2>: 重定向錯誤輸出
[[email?protected] ~]# ls /varr > /tmp/var2.out
ls: cannot access /varr: No such file or directory
[[email?protected] ~]# ls /varr 2> /tmp/var2.out
[[email?protected] ~]# cat /tmp/var2.out
ls: cannot access /varr: No such file or directory
[[email?protected] ~]#
2>>: 追加方式
&>: 重定向標準輸出或錯誤輸出至同一個文件
[[email?protected] ~]# ls /var6 &> /tmp/var3.out
[[email?protected] ~]# cat /tmp/var3.out
ls: cannot access /var6: No such file or directory
[[email?protected] ~]# ls /var &> /tmp/var3.out
[[email?protected] ~]# cat /tmp/var3.out
account
cache
crash
db
empty
輸入重定向
[[email?protected] ~]# tr ‘a-z‘ ‘A-Z‘ < /etc/fstab
#
# /ETC/FSTAB
# CREATED BY ANACONDA ON SUN JUN 21 02:15:00 2015
#
# 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=57D85756-7680-4C7C-9125-6AD67DAE2C45 /?????????????????????? EXT4??? DEFAULTS??????? 1 1
UUID=2622A4B4-DDC9-47A3-AA2B-F06BC9BEC085 /BOOT?????????????????? EXT4??? DEFAULTS??????? 1 2
UUID=33D94759-FA01-4C4F-B4AC-BF3A1FE5E84F SWAP??????????????????? SWAP??? DEFAULTS??????? 0 0
TMPFS?????????????????? /DEV/SHM??????????????? TMPFS?? DEFAULTS??????? 0 0
DEVPTS????????????????? /DEV/PTS??????????????? DEVPTS? GID=5,MODE=620? 0 0
SYSFS?????????????????? /SYS??????????????????? SYSFS?? DEFAULTS??????? 0 0
PROC??????????????????? /PROC?????????????????? PROC??? DEFAULTS??????? 0 0
[[email?protected] ~]#
<
[[email?protected] ~]# cat << END
> the first line
> the second line
> END
the first line
the second line
[[email?protected] ~]#
從鍵盤中讀入數據,并保存在文檔中
cat >> /tmp/myfile.txt << EOF
[[email?protected] ~]# cat >> /tmp/myfile.txt << EOF
> the first line
> the second line
> EOF
[[email?protected] ~]# cat /tmp/myfile.txt
the first line
the second line
[[email?protected] ~]#
管道:前一個命令的輸出,作為后一個命令的輸入
命令1 | 命令2 | 命令3 | ...
[[email?protected] ~]# echo "hello,world" | tr ‘a-z‘ ‘A-Z‘
HELLO,WORLD
[[email?protected] ~]#
[[email?protected] ~]# echo "redhat" | passwd --stdin hive
Changing password for user hive.
passwd: all authentication tokens updated successfully.
[[email?protected] ~]#
同時輸出到屏幕和文件中
[[email?protected] ~]# echo "hello,world" | tee /tmp/hello.out
hello,world
[[email?protected] ~]# cat /tmp/hello.out
hello,world
[[email?protected] ~]#
統計文件行數
[[email?protected] ~]# wc -l /etc/passwd
32 /etc/passwd
[[email?protected] ~]# wc -l /etc/passwd | cut -d‘ ‘ -f1
32
[[email?protected] ~]#
練習:
1、統計/usr/bin/目錄下的文件個數;
# ls /usr/bin | wc -l
2、取出當前系統上所有用戶的shell,要求,每種shell只顯示一次,并且按順序進行顯示;
# cut -d: -f7 /etc/passwd | sort -u
3、思考:如何顯示/var/log目錄下每個文件的內容類型?
4、取出/etc/inittab文件的第6行;
# head -6 /etc/inittab | tail -1
5、取出/etc/passwd文件中倒數第9個用戶的用戶名和shell,顯示到屏幕上并將其保存至/tmp/users文件中;
# tail -9 /etc/passwd | head -1 | cut -d: -f1,7 | tee /tmp/users
6、顯示/etc目錄下所有以pa開頭的文件,并統計其個數;
# ls -d /etc/pa* | wc -l
7、不使用文本編輯器,將alias cls=clear一行內容添加至當前用戶的.bashrc文件中;
# echo "alias cls=clear" >> ~/.bashrc
總結
以上是生活随笔為你收集整理的linux命令基础知识 管道流,linux基础知识-I/O重定向,管道(示例代码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql将不同行数结果合并成多列_将多
- 下一篇: Mysql介绍与安装LinuxmacOS