linux shell基础1.4--文件描述符和重定向

来源:互联网 发布:淘宝网李宁女运动鞋 编辑:程序博客网 时间:2024/06/05 16:11

常见文件描述符以及系统预留标识:stdin(0)、stdout(1)、stderr(3)

重定向到文件

echo 'this is a sample to file ' > file.txt

追加到文件

echo 'this is a sample to file '  >>  file.txt

> 清空并输出

>>不清空,继续追加

重定向操作默认使用标准输出。

>等同于1>

>>等同于 1>>

将stderr转换为stdout

cmd 2>&1 output.txt

或者

cmd &> output.txt

黑洞文件/dev/null

重定向到文件并在stdin中打印

cmd | tee file1 file2 

cat -n将从stdin中接收到的每一行加上行号写入stdout

将stdin作为命令参数,只需将-作为命令行的文件名参数即可

cmd1 | cmd2 | cmd -

/dev/stdin

/dev/stdout

/dev/stderr

将文件重定向到命令

cmd < file

重定向脚本内部的文本块

写入文件

cat <<EOF>log.txt

xxxxxxxxx

xxxxxxxxxxxxxxxxx

xxxx

EOF

自定义文件描述符

0、1、2是预留描述符

文件打开模式:

只读模式

截断模式

追加模式

<操作符用于读取至stdin

>截断模式写入

>>追加模式写入

exec 3< file.txt

cat &3







原创粉丝点击