shell初級-----數據呈現方式

輸入與輸出

Linux系統將每一個對象看成文件處理,這包括輸入和輸出進程。
Linux用文件描述符來標識每一個文件對象。
文件描述符是一個非負整數,能夠惟一標識會話中打開的文件。
每一個進程一次多能夠有九個文件描述符。出於特殊目的,bash shell保留了前三個文件描述符(0、1和2) node

Linux的標準文件描述符

 

 

 

這三個特殊文件描述符會處理腳本的輸入和輸出。linux

一、stdinshell

STDIN文件描述符表明shell的標準輸入
在使用輸入重定向符號(<)時,Linux會用重定向指定的文件來替換標準輸入文件描述符。 它會讀取文件並提取數據,就如同它是鍵盤上鍵入的。bash

[root@node1 ljy]# cat < one.txt 
one
two
three

二、stdoutthis

STDOUT文件描述符表明shell的標準輸出命令行

[root@node1 ljy]# date > time.txt
[root@node1 ljy]# more time.txt 
2019年 05月 16日 星期四 10:08:51 CST
[root@node1 ljy]# date >> time.txt   #>>表示追加的意思
[root@node1 ljy]# more time.txt   
2019年 05月 16日 星期四 10:08:51 CST
2019年 05月 16日 星期四 10:09:01 CST

三、stderrorm

shell經過特殊的STDERR文件描述符來處理錯誤消息。對象

默認狀況下,錯誤消息也會輸出到顯示器輸出中。blog

[root@node1 ljy]# aaaa 2> err.txt       
[root@node1 ljy]# more err.txt 
-bash: aaaa: 未找到命令

#shell會只定向錯誤消息而非普通的數據。

若是想要同時定義普通與錯誤的消息能夠用兩個重定向符的方式:three

[root@node1 ljy]# ls -al one two 2> err.txt 1> normal.txt
[root@node1 ljy]# more err.txt 
ls: 沒法訪問two: 沒有那個文件或目錄
[root@node1 ljy]# more normal.txt 
-rw-r--r-- 1 root root 0 5月  16 10:17 one

bash也提供了特殊的重定向符實現這一個效果&>。

[root@node1 ljy]# ls -al one two &> ceshi.txt            
[root@node1 ljy]# more ceshi.txt 
ls: 沒法訪問two: 沒有那個文件或目錄
-rw-r--r-- 1 root root 0 5月  16 10:17 one
#生成的全部輸出都會到同一位置,包括普通與錯誤。默認錯誤消息會處於更高的優先級,方便查看。

腳本中重定向輸出

 一、臨時重定向

但願在腳本中生成錯誤的信息的話,能夠單獨的一行輸出重定向到STDERR。重定向到文件描述時,你必須在文件描述符數字前加一個&符號。

[root@node1 ljy]# more one.sh 
#!/bin/bash
echo "this is a error message!" >&2
echo "this is a normal message!"
[root@node1 ljy]# sh one.sh 
this is a error message!
this is a normal message!
[root@node1 ljy]# sh one.sh 2> err.log
this is a normal message!
[root@node1 ljy]# more err.log 
this is a error message!

默認狀況下,Linux會將STDERR導向STDOUT,可是運行腳本時重定向了STDERR,腳本中全部導向STDERR的都會被從新導向。

二、永久重定向

若是腳本中有大量數據須要重定向,可使用exec命令告訴shell在腳本執行期間重定向某個特定文件描述符。

[root@node1 ljy]# more one.sh 
#!/bin/bash
exec 1> normal.txt
exec 2> err.txt
echo "this is a error message!" >&2
echo "this is a normal message!"
[root@node1 ljy]# 
[root@node1 ljy]# sh one.sh 
[root@node1 ljy]# ls
err.txt  normal.txt  one.sh
[root@node1 ljy]# more err.txt 
this is a error message!
[root@node1 ljy]# more normal.txt 
this is a normal message!

腳本中重定向輸入

exec命令容許你將STDIN重定向到linux文件中

[root@node1 ljy]# more test   
one 
two
three
[root@node1 ljy]# more ceshi.sh 
#!/bin/bash
exec 0< test
count=1
while read line
do  
  echo "$count:$line"
  count=$[$count + 1]
done
[root@node1 ljy]# sh ceshi.sh 
1:one
2:two
3:three

阻止命令輸出

shell輸出到null文件的任何命令都不會被保存!

在Linux系統中null的標準位置是/dev/null,你重定向到該位置的文件都會被丟掉不會顯示。

[root@node1 ljy]# ls -al > /dev/null 
[root@node1 ljy]# more /dev/null 

也能夠將/dev/null做爲輸入文件,能夠用它來快速清空現有文件的數據,而不須要刪除後從新建立。

[root@node1 ljy]# more test 
one 
two
three
[root@node1 ljy]# cat /dev/null > test 
[root@node1 ljy]# more test

建立臨時文件

系統上的任何帳戶都有權限讀寫/tmp目錄中的文件

mktemp能夠在本地目錄中建立一個臨時文件,須要制定一個文件名,末尾須要設置6個X

[ljy@node1 tmp]$ mktemp ceshi.XXXXXX
ceshi.vyBZEx

-t選項會強制mktemp命令在系統的臨時目錄建立文件。

因爲mktemp命令返回來了全路徑名,你能夠在Linux系統的任何目錄下引用該臨時文件,不須要管目錄在哪。

[ljy@node1 /]$ mktemp -t ceshi.XXXXXX
/tmp/ceshi.eSU3MD

-d選項告訴mktemp命令來建立一個臨時目錄而不是一個文件。

[ljy@node1 tmp]$ mktemp -d ceshi.XXXXXX
ceshi.9JVceD

記錄消息

tee命令至關於管道的一個T型接頭。他將從STDIN過來的數據同時發往兩處,一處是STDOUT,另外一處是tee命令行所指定的文件名。

[root@node1 ljy]# date | tee ceshi
2019年 05月 16日 星期四 13:57:40 CST
[root@node1 ljy]# more ceshi
2019年 05月 16日 星期四 13:57:40 CST

tee命令會在每次使用時覆蓋輸出內容,若是你想要追加,必須使用-a參數。

相關文章
相關標籤/搜索