【Linux】第七章 shell腳本呈現數據(二)

nohup ./run/train_spark.py -c pnn200323 >./200323wlog.txt  2>&1 &

nohup 不掛斷的運行;& : 指在後臺運行;2:標準錯誤;1:標準輸出,運行日誌;>:重定向,將輸入放入200323wlog.txt。


1.輸入和輸出

    標準文件描述符

                 0 STDIN 標準輸入

                 1 STDOUT 標準輸出

                 2 STDERR 標準錯誤

    重定向錯誤

                  1.只重定向錯誤

ls -al badfile 2>test1         ##2>表示只重定向錯誤

                  2.重定向錯誤和數據

ls -al test test2 test3 badtest 2>test4 1>test5
### 2>表示將錯誤重定向到test4文件,1>表示將標準輸出重定向到test5文件
ls -al test test3 test4 badtest &> test7
### &>會將所有的輸出發送到同一個文件,包括錯誤和數據

在腳本中重定向輸出

1.臨時重定向

使用輸出重定向符來講輸出信息重定向到STDERR文件描述符。在重定向到文件描述符時,必須在文件描述符數字前加一個&

2. 永久重定向

使用exec命令告訴shell在腳本執行期間重定向某個特定文件描述符。

## 臨時重定向,使用&
echo "this is an error" >&2
echo "this is normal output"
 
## 永久重定向,使用exec命令
exec 2>testerror #重定向標準錯誤
echo "this is the start of the script" ##正常在屏幕顯示
echo "now redirecting all output to another location" ##同上
 
exec 1>testout ##重定向標準輸出
echo "this is a test should go to testout file" ##在testout文件中
echo "this line should go to testerror file" >&2 ##在testerror文件中

在腳本中重定向輸入

exec命令允許將STDIN重定向到Linux系統上的文件中。

exec 0<test14_2.sh ##重定向輸入
count=1
while read line
do 
echo "line #$count : $line"
count=$[ $count + 1 ]
done
## 1 創建輸出文件描述符
exec 3>testout
echo "this should be stored in the file " >&3
echo "this should be on the screen"
## 2 重定向文件描述符
exec 3>&1  ##描述符3重定向爲描述符1,即STDOUT
exec 1>testout  ##STDOUT重定向到文件
echo "this should store in the output file"
echo "along with this line"
exec 1>&3 ##STDOUT重定向到顯示器
echo "now things should be back to normal"
## 3 創建輸入文件描述符
exec 6<&0
exec 0<test14_2.sh
count=1
while read line
do
	echo "line #$count : $line"
	count=$[ $count + 1 ]
done
exec 0<&6
read -p "Are you done now? [Y/N]: " answer
case $answer in
	Y|y) echo "Goodbye" ;;
	N|n) echo "sorry,this is the end." ;;
esac
 
## 4 文件讀寫描述符,需要特別小心使用,使用<>
exec 3<>testfile
read line <&3
echo "read : $line"
echo "this is a test line." >&3
## 5 關閉文件描述符,使用&-,一旦關閉,在腳本就不能在使用
exec 3>&-

列出打開文件描述符

## 列出打開的文件描述符 lsof命令
## lsof -a -p $$ -d 0,1,2,3,4,5,6,7,8,9
exec 3>testfile1
exec 5<testfile2
exec 8<testfile
/usr/bin/lsof -a -p $$ -d0,1,2,3,5,8

阻止命令輸出

可以將STDERR重定向到一個叫做null文件的特殊文件,該文件位置爲/dev/null

ls -al >/dev/null

創建臨時文件

mktemp testing.XXXXXX

指定一個文件名模板,模板可以包含任意文本文件名,在文件名末尾加上6個X就可以了。mktemp命令會用6個字符替換這6個X,該文件在系統啓動時不會被刪除。-t選項會強制mktemp命令在系統的臨時目錄創建文件,使用這個選項,mktemp命令會返回臨時文件的全路徑,在/tmp目錄中的文件會在系統啓動時被刪除。-d選項會創建一個臨時目錄
 

##創建臨時文件
# 本地臨時文件 mktemp testing.XXXXXX,mktemp會用6個字符替換這6個X,保證文件名在目錄中是唯一的
# -t選項會強制mktemp命令在/tmp目錄中創建文件,返回文件的全路徑,該文件在系統啓動時會被刪除
# -d選項會創建一個臨時目錄
tempfile=$(mktemp test.XXXXXX)
exec 3>$tempfile
echo "This script writes to temp file $tempfile"
echo "This is the first line" >&3
echo "This is the second line" >&3
echo "This is the thrid line" >&3
exec 3>&-
echo "Done creating temp file.The contents are:"
cat $tempfile
rm -f $tempfile 2> /dev/null

記錄消息

tee命令可以將輸出同時發送到顯示器和日誌文件,命令格式: tee filename,-a選項會將數據追加到文件中

## 記錄消息
# tee命令會將輸出同時發送到顯示器和日誌文件,用法tee filename
#  date | tee -a testfile
# -a選項會將數據追加到文件中


參考:

1.https://blog.csdn.net/liouyi250/article/details/83108026

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