shell脚本使用技巧2

时间:2023-03-09 18:58:11
shell脚本使用技巧2

0--stdin标准输入

1--stdout标准输出

2--stderr标准错误

重定向

echo "this is a good idea " > temp.txt

temp.txt内容会被首先清空后再输入“this is a good idea”

追加

echo "this is a bad idea " >> temp.txt

cat temp.txt

打印退出状态:echo $?

ls + 2>out.txt

2输入错误时候,输出到out.txt

cmd 2>stderr.txt 1>stdout.txt

正确输出到stdout.txt,错误输出到stderr.txt中;

重定向到同一个文件:

cmd 2>&1 output.txt

tee

打印stdout,并重向到一个文件中:tee

command | tee file1

cat a* | tee -a out.txt | cat -n

-a 会覆盖文件,-n给文件加数字号

将脚本内部的文本块进行重定向

#!/bin/bash

cat<<EOF>log.txt

LOG FILE HEADER

this is a test log file

Function:system staticstics

EOF