Linux中的重定向 标准输入、标准输出、标准错误

Linux中的重定向 标准输入、标准输出、标准错误

类型 数字 英文 缩写
标准输入 0 standard input stdin
标准输出 1 standard output stdout
标准错误 2 standard error stderr

标准输出 重定向

>把标准输出重定向到新文件
等于1>1可省略
标准错误不适应,要用&>或者2>
会覆盖已有文件,追加用>>

systemctl status httpd > httpd		# 命令能执行成功,无屏显,内容都存到httpd
systemctl status dfasdfdsfas > httpd	# 命令能执行失败,屏显错误,httpd文件显示空白

>>追加符,不覆盖原有文件
等于1>>1可省略
标准错误不适应,要用&>>或者2>>

把标准输出、标准错误重定向到文件

[命令] > /dev/null 2>&1
[命令] >& /dev/null
[命令] &> /dev/null

tee命令类似>,但只从管道接收数据
tee -a(append)类似>>
tee & tee -a都会屏显,> & >>不屏显
tee可以同时重定向到多个文件

~/Desktop via 🅒 base 
➜ echo 456 > 456.txt 4567.txt
(base) 
~/Desktop via 🅒 base 
➜ more 456.txt 
456 4567.txt
(base) 
~/Desktop via 🅒 base took 2s 
➜ cat 4567.txt
cat: 4567.txt: No such file or directory
(base) 

将命令1的结果既保存到file.txt中,也传递给命令2,并屏显命令2的结果,tee只管前后两个命令

[命令1] | tee file.txt | [命令2]
~/Desktop via 🅒 base 
➜ echo 123.txt | tee 123.txt | tr a-z A-Z
123.TXT
(base) 

标准输入 重定向

<指定输入文件
<<等待用户输入,需要一个结束符,常设定为EOFEND
结合cat命令使用:

cat > file << END

tr命令

translate or delete characters

只处理字符,而非单行单列
只显示处理结果,不修改原文件
从管道接收数据(同tee),或从输入接收数据

替换字符并显示:

~/Desktop via 🅒 base 
➜ echo 1234567 | tr 123 asd
asd4567
(base) 

压缩(把多个重复的压缩到一个)

~/Desktop via 🅒 base 
➜ echo 1:2::::3::::::::::4 | tr -s :
1:2:3:4
(base) 

删除内容中的空格并打印在屏幕上

tr -d " " < file
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章