错误重定向的命令,重定向追加写入

文件描述符0,邮件是个文件

linux 输入/输出重定向,linux重定向

文件描述符(fd)是与某个打开的文件或数据流相关联的整数。文件描述符0,1,2是系统预留的。

0 ---- stdin(标准输入)
1 ---- stdout(标准输出)
2 ---- stderr(标准错误)   

输入重定向的命令 < ,输出重定向的命令 > ;
错误重定向的命令 2>,追加重定向的命令 >> ;

重定向到文件

mkdir /tmp/10
cd /tmp/10
echo "123" > 1.txt
echo "456" >> 1.txt
cat 1.txt
#123
#456

ls + #错误重定向
#ls: cannot access +: NO such file or directory
ls + 2> 1.txt
cat 1.txt
#ls: cannot access +: NO such file or directory

#还可以将stderr转换成stdout,使得stderr和stdout都被重定向到同一文件
#cmd > output.txt 2>&1   或者 cmd &> output.txt
ls + > output.txt 2>&1   #ls + &> output.txt
cat output.txt
#ls: cannot access +: NO such file or directory

#重定向到空设备
#/dev/null是一个空设备,向它写入的数组都会丢弃,但返回状态是成功的
ls + > /dev/null 2>&1
echo $?
#2 表示上条命令没有执行成功
#利用它的返回状态常用if判断中,如:
#检查多个主机是否存活 
for ip in 192.168.217.{1..3};do
        if ping -c 1 $ip >/dev/null;then
        #ping不通则为false
                echo "$ip ok"
        else
                echo "$ip no!"
        fi
done

redirect.sh

#!/bin/bash
cat << EOF >log.txt
Log file head
this is a test log file
function :system statistics
EOF

在cat << EOF
>log.txt与下一个EOF之间的所有文本都会被当做stdin数据。log.txt的内容如下:

sh redirect.sh
cat log.txt
#Log file head
#this is a test log file
#function :system statistics

read命令
read命令从标准输入中读取,并把读取的内容复制给变量。

#-p prompt  提示信息
read -p "please input your name:" name
#please input your name:James
echo $name
#James

#-a array   保存为数组,元素以空格分隔
read -p "please input your hobby:" -a arr
#please input your hobby:basketball pingpang running
echo ${arr[*]}
#basketball pingpang running

#read -d delimiter 持续读取直到遇到delimiter第一个字符退出
read -p "please input number of not 5:" -d 5
#please input number of not 5:4
#6
#5  遇到5返回

#-s 隐藏输入
#-t timeout 等待超时时间,秒


cat a.txt
#a b c
#1 2 3
#x y z
#while循环按行读取文件
cat a.txt |
while read line;do
echo $line
sleep 1
done

#重定向读取
while read line;do
echo $line
sleep 1
done < a.txt

#for循环读取
old_IFS=$IFS
IFS="\n"
for i in `cat a.txt`;do
echo $i
sleep 1
done
IFS=$old_IFS


#分别变量赋值
read a b c
#1 2 3
echo $a $b $c
#1 2 3

1. 标准输入输出

传统的Linux标准输入、输出设备主要是键盘和显示器。

2、输出重定向

输出的重定向:

[root@localhost ~]# ls -il /etc/ > etc.txt

[root@localhost /]# cat /proc/cpuinfo > cpu

[root@localhost /]# df -Th > df
[root@localhost /]# cat df
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/sda2      xfs        20G  3.3G   17G  17% /
devtmpfs       devtmpfs  986M     0  986M   0% /dev
tmpfs          tmpfs     994M   84K  994M   1% /dev/shm
tmpfs          tmpfs     994M  8.9M  986M   1% /run
tmpfs          tmpfs     994M     0  994M   0% /sys/fs/cgroup
/dev/sda1      xfs       197M  101M   97M  52% /boot
/dev/sr0       iso9660   3.5G  3.5G     0 100% /run/media/root/RHEL-7.0 Server.x86_64
[root@localhost /]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        20G  3.3G   17G  17% /
devtmpfs        986M     0  986M   0% /dev
tmpfs           994M   84K  994M   1% /dev/shm
tmpfs           994M  8.9M  986M   1% /run
tmpfs           994M     0  994M   0% /sys/fs/cgroup
/dev/sda1       197M  101M   97M  52% /boot
/dev/sr0        3.5G  3.5G     0 100% /run/media/root/RHEL-7.0 Server.x86_64

重定向追加写入:

[root@localhost /]# df -h > df.txt
[root@localhost /]# cat df.txt 
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        20G  3.3G   17G  17% /
devtmpfs        986M     0  986M   0% /dev
tmpfs           994M   84K  994M   1% /dev/shm
tmpfs           994M  8.9M  986M   1% /run
tmpfs           994M     0  994M   0% /sys/fs/cgroup
/dev/sda1       197M  101M   97M  52% /boot
/dev/sr0        3.5G  3.5G     0 100% /run/media/root/RHEL-7.0 Server.x86_64
[root@localhost /]# echo  aidu3ndddndd >> df.txt
[root@localhost /]# cat df.txt 
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        20G  3.3G   17G  17% /
devtmpfs        986M     0  986M   0% /dev
tmpfs           994M   84K  994M   1% /dev/shm
tmpfs           994M  8.9M  986M   1% /run
tmpfs           994M     0  994M   0% /sys/fs/cgroup
/dev/sda1       197M  101M   97M  52% /boot
/dev/sr0        3.5G  3.5G     0 100% /run/media/root/RHEL-7.0 Server.x86_64
aidu3ndddndd
[root@localhost /]# 

3. 输入重定向

这里给mycms5发一封邮件,邮件是个文件,df.txt

[root@localhost /]# mail mycms5 < df.txt
[root@localhost /]# su - mycms5
Last login: Tue Oct 17 13:03:49 CST 2017 on pts/1
[mycms5@localhost ~]$ mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/mycms5": 2 messages 1 new
    1 root                  Tue Oct 17 13:03  21/642   "bozhou in Ah"
>N  2 root                  Tue Oct 17 13:19  25/999   
& 2
Message  2:
From root@localhost.localdomain  Tue Oct 17 13:19:07 2017
Return-Path: <root@localhost.localdomain>
X-Original-To: mycms5
Delivered-To: mycms5@localhost.localdomain
Date: Tue, 17 Oct 2017 13:19:06 +0800
To: mycms5@localhost.localdomain
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root@localhost.localdomain (root)
Status: R

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        20G  3.3G   17G  17% /
devtmpfs        986M     0  986M   0% /dev
tmpfs           994M   84K  994M   1% /dev/shm
tmpfs           994M  8.9M  986M   1% /run
tmpfs           994M     0  994M   0% /sys/fs/cgroup
/dev/sda1       197M  101M   97M  52% /boot
/dev/sr0        3.5G  3.5G     0 100% /run/media/root/RHEL-7.0 Server.x86_64
aidu3ndddndd

& 
ctrl+d 退出
& Held 2 messages in /var/spool/mail/mycms5
You have mail in /var/spool/mail/mycms5
[mycms5@localhost ~]$ logout
[root@localhost /]# cat > file2017.txt << EOF #以EOF结束
> ABC
> 123456
> aidd
> adduduudd
> 293933
> da73633
> eof
> EOF
[root@localhost /]# 

4. 标准错误

[root@localhost /]# audheheh
bash: audheheh: command not found... #标准错误
[root@localhost /]# ahcbddbdd > my.txt
bash: ahcbddbdd: command not found...
[root@localhost /]# cat my.txt  
[root@localhost /]# 
[root@localhost /]# ahcbddbdd 2> my.txt #错误信息写入my.txt
[root@localhost /]# cat my.txt 
bash: ahcbddbdd: command not found...
[root@localhost /]# bozhou-test 2>> my.txt  # 2错误的信息,>> 追加
[root@localhost /]# cat my.txt 
bash: ahcbddbdd: command not found...
bash: bozhou-test: command not found...

5. 查找

find / -name file 查找根目录/下的名为file的文件

find / -user mycms5 查找属于用户mycms5的文件

[root@localhost /]# find / -user mycms5 1> yes 2> no
[root@localhost /]# cat yes
/tmp/.ICE-unix/1933
/tmp/.ICE-unix/12195
/tmp/.esd-1000
/var/spool/mail/mycms5
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/time
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/last_occurrence
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/uid
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/kernel
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/architecture
/home/mycms5/.bash_history
/home/mycms5/core.12606
/home/mycms5/.viminfo
此处省略多行.....

[root@localhost /]# cat no
find: ?.proc/3601/task/3601/fd/6?. No such file or directory
find: ?.proc/3601/task/3601/fdinfo/6?. No such file or directory
find: ?.proc/3601/fd/6?. No such file or directory
find: ?.proc/3601/fdinfo/6?. No such file or directory

[root@localhost /]# find / -user mycms5 &> all
[root@localhost /]# cat all
find: ?.proc/3621/task/3621/fd/6?. No such file or directory
find: ?.proc/3621/task/3621/fdinfo/6?. No such file or directory
find: ?.proc/3621/fd/6?. No such file or directory
find: ?.proc/3621/fdinfo/6?. No such file or directory
/tmp/.ICE-unix/1933
/tmp/.ICE-unix/12195
/tmp/.esd-1000
/var/spool/mail/mycms5
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/time
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/last_occurrence
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/uid
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/kernel
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/architecture
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/hostname
此处省略多行.....

[root@localhost /]# find / -user mycms5 > all
find: ?.proc/3646?. No such file or directory
find: ?.proc/3648/task/3648/fd/6?. No such file or directory
find: ?.proc/3648/task/3648/fdinfo/6?. No such file or directory
find: ?.proc/3648/fd/6?. No such file or directory
find: ?.proc/3648/fdinfo/6?. No such file or directory
[root@localhost /]# find / -user mycms5 > all 2>&1
[root@localhost /]# cat all
find: ?.proc/3657/task/3657/fd/6?. No such file or directory
find: ?.proc/3657/task/3657/fdinfo/6?. No such file or directory
find: ?.proc/3657/fd/6?. No such file or directory
find: ?.proc/3657/fdinfo/6?. No such file or directory
/tmp/.ICE-unix/1933
/tmp/.ICE-unix/12195
/tmp/.esd-1000
/var/spool/mail/mycms5
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/time
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/last_occurrence
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/uid
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/kernel
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/architectu