---
title: "shell脚本的简单使用： 八——之输入输出重定向"
author: "Janle"
date: "2017-05-25T10:26:13Z"
tags: []
categories: ["编程语言"]
slug: "shell"
draft: false
lastmod: "2017-04-12T13:06:42Z"
id: 2368266
url: "https://janle.iteye.com/blog/2368266"
---

**shell输入输出重定向**
就像是脚本运行时候的日志，你需要在某个文件中查看，我们需要将各个脚本的日志输出重定向到统一的某一个文件中，监控整个项目的执行情况。
输出重定向

一般情况下，每个 Unix/Linux 命令运行时都会打开三个文件：
标准输入文件(stdin)：stdin的文件描述符为0，Unix程序默认从stdin读取数据。
标准输出文件(stdout)：stdout 的文件描述符为1，Unix程序默认向stdout输出数据。
标准错误文件(stderr)：stderr的文件描述符为2，Unix程序会向stderr流中写入错误信息。


```java

#简单的使用
echo “少了个参数” > logs.log
echo "this is a error" >> logs.log
who >> logs.log

```

输入重定向

```java
#显示文件名
wc -l logs.log
#不会显示文件名
wc -l < logs.log
```

*
先创建要进行后续测试的文件*

```java
echo -e "预先准备的测试文件";
echo "测试例子1." > temp.txt;
echo "测试例子 2." >> temp.txt;
cat temp.txt;
echo "a1" > a1;
echo "a2" > a2;
echo "a3" > a3;
sudo chmod 000 a3;
ls -alF a*;

//ls -F
//可执行文件, 添加一个后缀*;
//文件夹, 添加一个后缀/;
//软连接, 添加一个后缀@;

```



```java
#获得shell执行后的返回值
echo -e "获得返回值"
ls + ;
echo -e "执行ls + ;后返回 $?"
ls -alF ./ ;
echo -e "执行ls -alF ./ ;后返回 $?"

#标准输出stdout的重定位。
echo -e "重定向到stdout ！"
ls + > out.txt
echo -e "执行ls + > out.txt返回值为 $?"
echo -e "out.txt中的内容为"
cat out.txt
rm out.txt
#这里由于+在ls命令时候不合法，后续的out.txt中内容为空

echo "=========================================="

#获得shell执行后的返回值
echo -e "获得返回值"
ls + ;
echo -e "执行ls + ;后返回 $?"
ls -alF ./ ;
echo -e "执行ls -alF ./ ;后返回 $?"


echo "=========================================="
#标准输出stdout的重定位
echo -e "重定向到stdout ！"
ls + > out.txt
echo -e "执行ls + > out.txt返回值为 $?"
echo -e "out.txt中的内容为"
cat out.txt
rm out.txt

echo "=========================================="


#标准错误stderr重定位
echo -e "重定向到stderr"
ls + 2> out.txt
echo -e "ls + 2> out.txt 执行后返回结果为 $?"
echo -e "查看out.txt的内容为"
cat out.txt
rm out.txt

#执行后同样是返回2但是错误信息重定向到out.txt的文件中
echo "=========================================="

#分别重定向标准stdout与标准错误stderr到指定的文件
echo -e "重定向标准stdin和stderr"
cat a* 2>stderr.txt 1>stdout.txt
echo -e "执行cat a* 2>stderr.txt 1>stdout.txt 返回值$?"
echo -e "执行后的stderr.txt内容为"
cat stderr.txt
echo -e "执行后的stdout.txt内容为"
cat stdout.txt
rm stderr.txt stdout.txt

echo "=========================================="

#使用2>&1把标准错误stderr重定向到标准输出stdout
echo -e "吧stderr重定向到标准输出stdout"
#也可以使用cat a* &>out.txt可以实现通用的效果
cat a* >out.txt 2>&1
echo -e "执行结果返回为 $?"
echo -e "out.txt内容为"
cat out.txt
rm out.txt

echo "=========================================="


#把标准错误stderr重定向到/dev/null
echo -e "将stderr重定向到/dev/null"
cat a* 2>/dev/null
echo "执行后返回的结果为:$?"
#"/dev/null" 是一个特殊的设备文件，所有输入到其中的比特流都会被丢弃


```

执行结果

```java
获得返回值
ls: cannot access +: No such file or directory
执行ls + ;后返回 2
total 40
drwxr-xr-x 2 root root 4096 Apr 12 11:18 ./
drwxr-xr-x 3 root root 4096 Apr 11 14:28 ../
-rw-r--r-- 1 root root    3 Apr 11 22:36 a1
-rw-r--r-- 1 root root    3 Apr 11 22:36 a2
---------- 1 bin  root    3 Apr 11 22:36 a3
-rwxrwxrwx 1 root root 1788 Apr 12 11:01 ioe.sh*
-rw-r--r-- 1 root root    9 Apr 11 22:44 output.txt
-rw-r--r-- 1 root root    9 Apr 12 11:31 out.txt
-rw-r--r-- 1 root root 1210 Apr 11 22:44 redirect.sh
-rw-r--r-- 1 root root   48 Apr 11 22:36 temp.txt
执行ls -alF ./ ;后返回 0
==========================================
重定向到stdout ！
ls: cannot access +: No such file or directory
执行ls + > out.txt返回值为 2
out.txt中的内容为
==========================================
重定向到stderr
ls + 2> out.txt 执行后返回结果为 2
查看out.txt的内容为
ls: cannot access +: No such file or directory
==========================================
重定向标准stdin和stderr
执行cat a* 2>stderr.txt 1>stdout.txt 返回值0
执行后的stderr.txt内容为
执行后的stdout.txt内容为
a1
a2
a3
==========================================
吧stderr重定向到标准输出stdout
执行结果返回为 0
out.txt内容为
a1
a2
a3
==========================================
将stderr重定向到/dev/null
a1
a2
a3
执行后返回的结果为:0
==========================================
```

*把标准输出 stdout 重定向到 tee 的标准输入 stdin*

```java

//"cat a*" 的标准输出 stdout 通过pipe( "|" )重定向为 tee 的标准输入；
//tee 把这些信息重定向到 out.txt 的同时，输出到 tee 的标准输出 stdout ；
//tee 的标准输出通过 pipe("|") 重定向到了 "cat -n" 的标准输入；
cat a* | tee out.txt | cat -n;
//把标准错误也重定向到stdout
cat a* 2>&1 | tee out.txt | cat -n;
//添加-a追加输出信息
执行cat a* 2>stderr.txt 1>stdout.txt 返回值0

```

以上的结果分析
1）因为pipe("|") 只处理标准输出的信息，错误信息依旧在终端直接输出，没有被重定向处理；
2）tee 从它的标准输入 stdin 只接收到 a1、a2 的内容，通过 "cat -n" 给输出信息加上行号来标识出 tee 接收到的信息；
3）再通过查看 out.txt 文件，发现 tee 命令输出到 out.txt 的信息，与 "cat -n"的信息是一致的；
 
**stdin 作为输入在使用cat时候的表现**

```java

cat --help #可以看见cat -等价于 /dev/stdin

ls -alF | cat
ls -alF | cat - #和上边输出一样，
ls -alF | cat -n #输出序号
ls -alF | cat /dev/stdin

cat < a1
cat < a2
cat < temp.txt

```

*文本块重定向为 shell 命令的标准输入 stdin *

```java
cat <<ERR>log.txt
Hello word
ERR
cat log.txt
cat <<ERR >> log.txt
Nice to meet 
EOF
cat log.txt;

```




shell文件之间的引用调用