重定向和基础命令

网友投稿 272 2022-11-07

重定向和基础命令

echo 输出空行

交互式的输出算错误输出

[root@localhost etc]# ls /dev/stdout /dev/stdout [root@localhost etc]# ls /dev/stdin /dev/stdin >或1> 正确的内容覆盖到里面 2> 错误的内容覆盖到里面 &> 混合覆盖 >& 和 &> 的效果一样 COMMAND > /path/to/somefile 2>&1 合并正常和错误输出流 [root@localhost ~]# lls 2> a.txt # 错误的内容覆盖写入到里面 [root@localhost ~]# cat a.txt -bash: lls: command not found [root@localhost ~]# &> 不管是正确的或者错误的统统都给写入到里面,相当于xxx >xxx 2>&1 &>> 不管是正确的或者错误的统统都给追加到里面,相当于 xxx>>xxx 2>&1 Set -C 禁止IO覆盖重定向 Set +C 开启IO覆盖重定向 >| 强行使用覆盖重定向 1 标准输出 2 错误输出 0 标准输入 [root@node3 ~]# echo 01 >01.txt [root@node3 ~]# tr 01 0a <01.txt 0a [root@node3 ~]# tr 01 0a 0<01.txt (因为只能用0< 或者< 表示输入) 0a [root@node3 ~]# tr 01 0a 1<01.txt ^C #面试题:将正确输出和错误输出都重定向一个文件的三种写法 [root@node3 data]# ls /data/xxx &> /data/all.log 合并正常和错误输出流覆盖到目标文件 [root@node3 data]# ls /data/xxx 2> /data/all.log 1>&2 [root@node3 data]# ls /data/xxx > /data/all.log 2>&1 ================================================================= #错误的写法: [root@node3 data]# ls /data/xxx 2>&1 > /data/all.log ls: cannot access /data/touch..txt: No such file or directory ===================================================================== 合并正常和错误输出流覆盖到目标文件 两个等价命令都表示正确的和错误的都追加到里面: [root@localhost ~]# ll /error /boot > /data/all3.txt 2>&1 [root@localhost ~]# ll /error /boot 2> /data/all4.txt 1>&2 #错误的和正确的分开写在不同的文件里 [root@localhost data]# ll /error /boot >/data/1.txt 2>/data/2.txt #清空大文件的方法: cat /dev/null > /SOMEFILE > /SOMEFILE (利用shell特性) #多个命令结果追加到文件里面 [root@node3 data]# { ls;cal; }>/data/f.txt [root@node3 data]# (ls;cal)>/data/f.txt [root@node3 data]# man bash >bash.txt #邮件服务器: yum -y install postfix;systemctl enable --now postfix yum install mailx -y [root@node3 data]# mail -s hello wang how . EOT 按.回车以后就发出去了 或者ctrl d 也可以发送出去 #切换用户查看邮件 [root@node3 ~]# su - wang welcome here [wang@node3 ~]$ mail Heirloom Mail version 12.5 7/5/10. Type ? for help. "/var/spool/mail/wang": 1 message 1 new >N 1 root Sat Apr 3 10:54 18/575 "hello" & & 1 Message 1: From root@node3.wanghua.com Sat Apr 3 10:54:43 2021 Return-Path: X-Original-To: wang Delivered-To: wang@node3.wanghua.com Date: Sat, 03 Apr 2021 10:54:43 +0800 To: wang@node3.wanghua.com Subject: hello User-Agent: Heirloom mailx 12.5 7/5/10 Content-Type: text/plain; charset=us-ascii From: root@node3.wanghua.com (root) Status: R how & exit 退出

bc支持标准输入

[root01:38 AMcentos8 /data]#cat b.txt 2+3 3*4 [root01:38 AMcentos8 /data]#bc b.log [root01:41 AMcentos8 /data]#bc

多行重定向

[root08:00 AMcentos8 ~]#cat>file.txt< line1 > lin2 > EOF [root08:02 AMcentos8 ~]#cat file.txt line1 lin2

标准输入发送邮件

seq

[root@centos8 ~]#cat /data/fa.txt 1 2 3 4 5 [root@centos8 ~]#tac /data/fa.txt 5 4 3 2 1 [root@centos8 ~]#seq 10 |tac 10 9 8 7 6 5 4 3 2 1

tr 接受管道和标准输入

[00:58:04root@localhost ~]#tr abc ABC asd Asd abc ABC # abc ABC相互替换 [01:21:17root@localhost ~]#tr abc ABC random.out tr -dc '[alnum]' df.log [root@localhost ~]# cat df.log Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/centos-root 18307072 5559992 12747080 31% / devtmpfs 490140 0 490140 0% /dev tmpfs 500664 0 500664 0% /dev/shm tmpfs 500664 6868 493796 2% /run tmpfs 500664 0 500664 0% /sys/fs/cgroup /dev/sda1 508588 145208 363380 29% /boot tmpfs 100136 0 100136 0% /run/user/0 [root@localhost ~]# tr -s ' ' /tmp/a [root07:53 AMcentos8 ~]#cat /tmp/a ROOT TTY1 2021-05-14 07:09 ROOT PTS/0 2021-05-14 07:10 (10.0.0.1) #发送邮件给wang用户,标题为help [wang08:08 AMcentos7 ]$cat >a.txt< I am `whoami` > EOF #将/root文件下的列表显示为一行,且用空格隔开 [root08:29 AMcentos7 ]#echo `ls /root |tr -s ' '` 1 10.0. 2 23 anaconda-ks.cfg a.txt case.sh cmatrix cmatrix-v2.0-Butterscotch.tar fstab ip_list.gen libncurses.so.6 N passwd passwd.gpg tree-1.8.0 txt xxx #求和1到100 [root08:33 AMcentos7 ]#seq -s + 100 |bc 5050 [root08:33 AMcentos7 ]#echo {1..100}|tr -s ' ' + |bc 5050 # 删除windoes文件中的回车符号 [root@localhost ~]# tr -d '\r' < win.txt >win2.txt #去掉回车键,Windows的换行包括回车 [root08:38 AMcentos7 ]#cat win.txt a b c [root08:38 AMcentos7 ]#tr -d '\r' < win.txt >win2.txt [root08:39 AMcentos7 ]#cat win2.txt a b c [root08:39 AMcentos7 ]#hexdump -C win2.txt 00000000 61 0a 62 0a 63 0a |a.b.c.| 00000006 [root08:39 AMcentos7 ]#hexdump -C win.txt 00000000 61 0d 0a 62 0d 0a 63 0d 0a |a..b..c..| 00000009

tr 实现win和Linux之间格式的相互转化

第一步:先安装包 yum install man-pages -y

发现 \r 的八进制是015

[root09:08 AMcentos8 ~]#hexdump -C win.txt 00000000 61 0d 0a 62 0d 0a |a..b..| 00000006 [root09:09 AMcentos8 ~]#tr -d "\015" linux.txt #将win格式转换为Linux格式 [root09:09 AMcentos8 ~]#cat linux.txt a b [root09:09 AMcentos8 ~]#hexdump -C linux.txt 00000000 61 0a 62 0a |a.b.| 00000004

管道

#管道实现同一主机之间的进程之间的通信 autofs.fifo-misc 创建一个管道文件 [root12:21 PMcentos8 /data]#mkfifo test.fifo2 向里面写数据 [root12:22 PMcentos8 /data]#cat >test.fifo2 abc [root12:22 PMcentos8 /data]#ls test.fifo2 -l prw-r--r-- 1 root root 0 May 6 12:22 test.fifo2 [root@localhost ~]# xxx |tr 'a-z' 'A-Z' -bash: xxx: command not found #无论正确与错都传 [root@localhost ~]# xxx 2>&1 |tr 'a-z' 'A-Z' -BASH: XXX: COMMAND NOT FOUND [root@localhost ~]# xxx |& tr 'a-z' 'A-Z' -BASH: XXX: COMMAND NOT FOUND

rev 倒序

[root10:33 AMcentos8 ~]#echo ABCD |rev DCBA

搭建邮件服务器

第一步:注册邮箱,设置 获取授权码

第四:发送邮件

tee

[root@localhost ~]# tee a.txt 1 1 2 2 3 3 ^C [root@localhost ~]# cat a.txt 1 2 3 [root@localhost /]# ls |tee a.txt 屏幕上显示一份,文件里保存一份 a.txt bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var [root@localhost /]# cat a.txt a.txt bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var [root@localhost /]# whoami |tee -a a.txt (-a 追加) root [root@localhost /]# cat a.txt a.txt bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root 动态显示 且屏幕显示一份 [root@localhost /]# cat < I am `whoami` > echo $PATH > efo I am root echo /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin [root@localhost /]# cat welcome.txt I am root echo /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin #将/etc/issue 里面的小写变成大写,且保存到文件里面 [root07:48 AMcentos8 ~]#tr 'a-z' 'A-Z'

EOF 搭配 tee写邮件文件的配置

修改口令

[root10:42 AMcentos8 ~]#passwd --stdin wang Changing password for user wang. 123456 passwd: all authentication tokens updated successfully. [root11:17 AMcentos8 ~]#echo 123456 |passwd --stdin wang Changing password for user wang. passwd: all authentication tokens updated successfully. [root11:26 AMcentos8 ~]#cat passwd.txt |passwd --stdin wang Changing password for user wang. passwd: all authentication tokens updated successfully.

练习

第一题: [root11:39 AMcentos8 ~]#tr "a-z" "A-Z" I am `whoami` the system version is here ,please help me check it . > `cat /etc/redhat-release` > EOF [linux@centos8 ~]$ exit logout You have new mail in /var/spool/mail/root [root12:54 PMcentos8 ~]#mail Heirloom Mail version 12.5 7/5/10. Type ? for help. "/var/spool/mail/root": 2 messages 2 new >N 1 Mail Delivery System Thu Jun 3 08:29 81/2696 "Undelivered Mail Returned to Sender" N 2 linux@centos8.locald Thu Jun 3 12:54 19/686 "help" & 2 Message 2: From linux@centos8.localdomain Thu Jun 3 12:54:52 2021 Return-Path: X-Original-To: root Delivered-To: root@centos8.localdomain Date: Thu, 03 Jun 2021 12:54:52 +0800 To: root@centos8.localdomain Subject: help User-Agent: Heirloom mailx 12.5 7/5/10 Content-Type: text/plain; charset=us-ascii From: linux@centos8.localdomain Status: R I am linux the system version is here ,please help me check it . CentOS Linux release 8.2.2004 (Core) & ==================================================================================== 第四题; [root12:05 PMcentos8 ~]#ls /root/ |tr -s "\n" ' ' anaconda-ks.cfg dead.letter file.txt linux.txt man.txt passwd.txt sendmile test txt wang win.txt [root12:06 PMcentos8 ~]# ====================================================================================== 第五题: 方法一: [root12:08 PMcentos8 ~]#seq -s+ 100 |bc 5050 方法二 [root12:09 PMcentos8 ~]#echo {1..100}|tr -s ' ' + |bc 5050 方法三: [root12:12 PMcentos8 ~]#sum=0;for i in `seq 100`;do let sum+=$i;done;echo sum=$sum sum=5050 ============================================================================= 第六题: 方法一: [root12:17 PMcentos8 ~]#hexdump -C win.txt 00000000 61 0d 0a 62 0d 0a |a..b..| 00000006 [root12:18 PMcentos8 ~]#tr -d "\r" newlinux.txt [root12:21 PMcentos8 ~]#hexdump -C newlinux.txt 00000000 61 0a 62 0a |a.b.| 00000004 ============================================================================== 第七题: [root12:44 PMcentos8 ~]#echo 'xt.,| 1 jr#!$mn 2 c*/fe 3 uz 4' |tr -dc '[a-z]' xtjrmncfeuz[root12:44 PMcentos8 ~]# ================================================================= 第八题: [root12:25 PMcentos8 ~]#echo $PATH |tr ':' '\n' /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /root/bin ================================================================================= 第九题: [root12:27 PMcentos8 ~]#echo {0..9} >num.txt [root12:27 PMcentos8 ~]#cat num.txt 0 1 2 3 4 5 6 7 8 9 [root12:28 PMcentos8 ~]#tr '0-9' 'a-j'

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:基于ADS7846的电阻式触摸屏接口设计
下一篇:文件管理
相关文章

 发表评论

暂时没有评论,来抢沙发吧~