shell腳本使用技巧2
阿新 • • 發佈:2017-06-09
bsp 使用 sys this txt 狀態 錯誤輸出 內容 head
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
shell腳本使用技巧2