linux/unix 命令运行逻辑
一般unix/linux系统命令运行会打开三个文件:
- 标准输入文件(stdin):stdin的文件描述符为0,Unix程序默认从stdin读取数据
- 标准输出文件(stdout):stdout的文件描述符为1, Unix程序默认向stdout输出数据
- 标准错误文件(stderr):stderr的文件描述符为2,Unix程序会向stderr流中写入错误信息
shell 重定向输入
```bash将输入 重定向至test.txt
wc -l < test.txt
输出结果:2
<a name="EhrdF"></a>
#### shell 重定向输出
```bash
# 将结果输出重定向 到test.txt
ls > test.txt
# 将结果叠加输出重定向 到test.txt
pwd >> test.txt
cat test.txt
# basic_common.sh
# io_redirection.sh
# process_control.sh
# shell_operators_practise.sh
# shell_test.sh
# test
# test.txt
不同类型的重定向输出
# stderr 重定向到file
pwd 2>file
pwd 2>>file
# 标准输出和标准错误文件都重定向到file
pwd > file 2>&1
pwd >> file 2>&1
# 标准输入重定向到file1 、标准输出重定向到file2
pwd < file1 > file2
特殊的重定向方式
wc -l << EOF
test
www.baidu.com
EOF
cat << EOF
www.baidu.com
EOF
文件引用
# test1.sh文件
url="www.baidu.com"
# test2.sh文件
. ./test1.sh
# 或者
source ./test1.sh
chmod +x test2.sh
# 结果输出:www.baidu.com