shell學習筆記(三)shell輸入與輸出

shell學習筆記(三)shell輸入與輸出目錄:linux

======================================================
echo
read
cat
管道 |
tee
標準輸入.輸出和錯誤
文件重定向
    標準輸入的重定向
    標準輸出的重定向
    標準輸出的重定向
    標準輸出和標準錯誤的重定向
    標準輸入和標準輸出的重定向
exec
管道|(pipe)與文件重定向區別
shell

=====================================================bash

echo
 
顯示文本行或變量,或者把字符串輸入到文件
 
echo [option] string
-e 解析轉義字符
-n 回車不換行,系統默認是回車換行,如:echo 空白
-轉義符
\a 發出報警聲
\c 回車不換行
\f換行但光標仍舊停留在原來的位置
\t跳格 相對於tab鍵
\n回車換行
\b 退格鍵
\r 回車鍵)
 
eg:
[root@viong_app1 soft]# cat echo
#!/bin/bash
#echo
echo -e "i an lin\c"        不換行
echo -e "i an man\n\n"    回車換行
echo "YES"
echo -e "a\tb\tc\nd\te\bf" 由於e\b 是退格 因此e被刪除
echo
echo "wo men\n"
echo -e "i an wei\f"
echo "viong">echo.txt
echo -n "yi qi"
[root@viong_app1 soft]# ./echo
i an lini an man
 
YES
a        b       c
d        f
 
wo men\n
i an wei
 
yi qi [root@viong_app1 soft]# cat echo.txt
viong
[root@viong_app1 soft]#
 
read
 
做用

從標準輸入中讀取一行。
 
語法

read [ -p ][ -r ][ -s ][ -u[ n ] ] [ VariableName?Prompt ]
[ VariableName ... ]
 
描述
read 命令從標準輸入中讀取一行,並把輸入行的每一個字段的值指定給 shell 變量,用 IFS(內部字段分隔符)變量中的字符做爲分隔符。VariableName 參數指定 shell 變量的名稱,shell 變量獲取輸入行一個字段的值。
由VariableName 參數指定的第一個 shell 變量指定給每個字段的值,由 VariableName 參數指定的第二個 shell 變量指定給第二個字段的值,以此類推,直到最後一個字段。 若是標準輸入行的字段比相應的由 VariableName 參數指定的 shell 變量的個數多,把所有餘下的字段的值賦給指定的最後的 shell 變量若是比 shell 變量的個數少,則剩餘的 shell 變量被設置爲空字符串。

注意: 若是省略了 VariableName 參數,變量 REPLY 用做缺省變量名。
由 read 命令設置的 shell 變量影響當前 shell 執行環境
 
如下根據不一樣參數進行講解
 
read命令-p(提示語句) -n(字符個數) -t(等待時間) -s(不回顯) 和「讀文件」
 
#!/bin/bash 
echo -n "Enter your name:"   # -n
阻止換行
read  name                
echo "hello $name "    
exit 0 
 
-------------------------------------------------------------------
[root@viong_app1 soft]# cat read.sh
#!/bin/bash
read -p "Enter your name:" name name1                
echo "hello $name "    
echo "hello $name1 "
exit 0
[root@viong_app1 soft]# ./read.sh
Enter your name:li ming bai
hello li
hello ming bai   #餘下的字段的值賦給指定的最後的 shell 變量
 
[root@viong_app1 soft]# ./read.sh
Enter your name:li
hello li
hello    #剩餘的 shell 變量被設置爲空字符串
 
-p選項容許在read命令行中直接指定一個提示
 
#!/bin/bash 
read –p "Enter your name:" name                  
echo "hello $name "    
exit 0 
 
 
-t選項指定read命令等待輸入的秒數。當計時滿時,read命令返回一個非零退出狀態; 

#!/bin/bash 
if read -t 5 -p "please enter your name:" name 
then 
    echo "hello $name " 
else 
    echo "sorry,time out " 
fi 
exit 0
 
 -n選項後接數值1,指示read命令只要接受到一個字符就退出,只要按下一個字符進行回答,read命令當即接受輸入並將其傳給變量,無需按回車鍵.對於read命令來講,-n選項不會檢測enter(新行)鍵
 
#!/bin/bash 
read -n1 -p "Do you want to continue [Y/N]?" answer 
case $answer in 
Y | y) 
      echo "fine ,continue";; 
N | n) 
      echo "ok,good bye";; 
*) 
     echo "error choice";; 
esac 
exit 0 
 
-s選項可以使read命令中輸入的數據不顯示在監視器上(實際上,數據是顯示的,只是read命令將文本顏色設置成與背景相同的顏色)。 

#!/bin/bash 
read  -s  -p "Enter your password:" pass 
echo "your password is $pass" 
exit 0 
-------------------------------------------------------------------------------------------------
每次調用read命令都會讀取文件中的"一行"文本。當文件沒有可讀的行時,read命令將以非零狀態退出,讀取文件的關鍵是如何將文本中的數據傳送給read命令 

最經常使用的方法是對文件使用cat命令並經過管道將結果直接傳送給包含read命令的while命令
eg:
[root@viong_app1 soft]# cat test
lin 80
hom 90
viong 100
wang 60
li 70
cheng 50
fan 40
shi 30
-----------------------------------------------------------------------------------------
[root@viong_app1 soft]# cat read.sh
count=1
cat test|while read line     #cat 命令的輸出做爲read 命令的輸入,read 讀到的值放在line
do
   echo "Line $count:$line"
   count=$[ $count + 1 ]    # 注意中括號中的空格。
done
echo "finish"
exit 0
 
也可直接
 
#!/bin/bash
count=1
while read line      #cat 命令的輸出做爲read命令的輸入,read讀到的值放在line中
do
   echo "Line $count:$line"
   count=$[ $count + 1 ]    #注意中括號中的空格。
done<test           #test爲文本文件
echo "finish"
exit 0
 
-------------------------------------------------------------------------------------------
[root@viong_app1 soft]# ./read.sh
Line 1:lin 80
Line 2:hom 90
Line 3:viong 100
Line 4:wang 60
Line 5:li 70
Line 6:cheng 50
Line 7:fan 40
Line 8:shi 30
finish
 
cat
 
說明:把檔案串鏈接後傳到基本輸出,用它來顯示文件內容.建立文件,還能夠用它來顯示控制字符
 
參數:

-n 或 --number 由 1 開始對全部輸出的行數編號
-b 或 --number-nonblank 和 -n 類似,只不過對於空白行不編號
-s 或 --squeeze-blank 當遇到有連續兩行以上的空白行,就代換爲一行的空白行
-v 或 --show-nonprinting

範例:
cat -n textfile1 > textfile2 把 textfile1 的檔案內容加上行號後輸入 textfile2 這個檔案裏
cat -b textfile1 textfile2 >> textfile3 把 textfile1 和 textfile2 的檔案內容加上行號(空白行不加)以後將內容附加到 textfile3 裏

範例:
把 textfile1 的檔案內容加上行號後輸入 textfile2 這個檔案裏
cat -n textfile1 > textfile2
把 textfile1 和 textfile2 的檔案內容加上行號(空白行不加)以後將內容附加到 textfile3 裏。
cat -b textfile1 textfile2 >> textfile3
cat /dev/null > /etc/test.txt 此爲清空/etc/test.txt檔案內容
 
管道 |
 
管道是把一個命令的輸出傳遞給另一個命令做爲輸入,管道用豎槓「|「表示
 
格式: 命令1|命令2
eg:
cat file|more 分頁顯示
ll |grep 「file」
df -k|awk '{print$1}'|grep -v "文件系統"     命令之間傳遞
eg:
[root@viong_app1 soft]# df -k
文件系統                1K-        已用      可用 已用% 掛載點
/dev/mapper/VolGroup00-LogVol00
                      18187836   1737764 15511272 11% /
/dev/sda1                101086     12753     83114 14% /boot
tmpfs                    513304         0    513304   0% /dev/shm
 
[root@viong_app1 soft]# df -k|awk '{print$1}'|grep -v "文件系統"
/dev/mapper/VolGroup00-LogVol00
18187836
/dev/sda1
tmpfs
 
tee
 
tee是把輸出的一個副本輸送到標準輸出,另一個副本拷貝到相應的文件中
 
tee –a files
若是但願在看到輸出的同時,也將其存入一個文件,加-a表明累加原有的內容,不覆蓋
 
通常用於管道以後
eg: 
[root@viong_app1 soft]# df -k|awk '{print$1}'|grep -v "文件系統" |tee disk
/dev/mapper/VolGroup00-LogVol00
18187836
/dev/sda1
tmpfs
[root@viong_app1 soft]# cat disk
/dev/mapper/VolGroup00-LogVol00
18187836
/dev/sda1
tmpfs
-------------------------------------------------------------------------------------------------
[root@viong_app1 soft]# df -k|awk '{print$1}'|grep -v "文件系統" |tee -a disk
[root@viong_app1 soft]# cat disk
/dev/mapper/VolGroup00-LogVol00
18187836
/dev/sda1
tmpfs
/dev/mapper/VolGroup00-LogVol00
18187836
/dev/sda1
tmpfs
  
標準輸入.輸出和錯誤
 
linux啓動後,會默認打開3個文件描述符,分別是:標準輸入standard input 0,正確輸出standard output 1,錯誤輸出:error output 2
 
linux 文件描述符:能夠理解爲linux跟蹤打開文件,而分配的一個數字,這個數字有點相似c語言操做文件時候的句柄,經過句柄就能夠實現文件的讀寫操做。 用戶能夠自定義文件描述符範圍是:3-num,這個最大數字,跟用戶的:ulimit –n 定義數字有關係,不能超過最大值。
關於修改linux文件描述符,請見如下連接
 
之後打開文件後。新增文件綁定描述符 能夠依次增長。 一條shell命令執行,都會繼承父進程的文件描述符。所以,全部運行的shell命令,都會有默認3個文件描述符
 
文件                               文件描述符
輸入文件—標準輸入                 0(缺省是鍵盤,也能夠是文件或其餘命令的輸出)
輸出文件---標準輸出                 1(缺省是屏幕,也能夠是文件)
錯誤輸出文件—標準錯誤             2(缺省是屏幕,也能夠是文件)
 
系統中實際上有12個文件描述符,能夠任意使用文件描述符3到9
 
文件重定向
 
所謂文件重定向就是改變程序運行的輸入來源和輸出地點
 
標準輸入的重定向
 
命令默認從鍵盤得到的輸入,改爲從文件,或者其它打開文件以及設備輸入,執行這個命令,將標準輸入0,與文件或設備綁定,將由它進行輸入.
 
command < filename              command命令以filename文件做爲標準輸入
command << delimiter(定界符)   從標準輸入中讀入,直到遇到delimiter定界符
command < &m                  把文件描述符m做爲標準輸入
eg:
[root@viong_app1 soft]# cat name
2
3
5
6
9
1
4
[root@viong_app1 soft]# sort < name
1
2
3
4
5
6
9
---------------------------------------------------------------------------
 
[root@viong_app1 soft]# cat score
beyes 10000 90 78 98
admin 10001 99 64 72
tony 10002 45 32 56
tom    10003 12 34 87
lilei 10004 48 25 70
 
[root@viong_app1 soft]# cat >> score <<exit
> viong 10005 49 30 80
> exit
[root@viong_app1 soft]# cat score
beyes 10000 90 78 98
admin 10001 99 64 72
tony 10002 45 32 56
tom    10003 12 34 87
lilei 10004 48 25 70
viong 10005 49 30 80
--------------------------------------------------------------------------
以上cat>>score<<exit 命令解釋:
cat>>score
輸入viong 10005 49 30 80
這裏按下ctrl+d或者ctrl+c離開
從標準輸入【鍵盤】得到數據,而後輸出追加給score文件
 
[root@lvs_app1 soft]# cat score
beyes 10000 90 78 98
admin 10001 99 64 72
tony 10002 45 32 56
tom    10003 12 34 87
lilei 10004 48 25 70
viong 10005 49 30 80
viong 10005 49 30 80
 
<<exit 直到遇到exit定界符,退出,至關於結束的輸入字符,不用按ctrl+d或者ctrl+c離開
-------------------------------------------------------------------------------------
[root@lvs_app1 soft]# cat score1
cao    10005 22 38 80
[root@lvs_app1 soft]# cat>>score<score1    #cat從score1得到輸入數據,而後輸出追加給score文件
[root@lvs_app1 soft]# cat score
beyes 10000 90 78 98
admin 10001 99 64 72
tony 10002 45 32 56
tom    10003 12 34 87
lilei 10004 48 25 70
viong 10005 49 30 80
viong 10005 49 30 80
cao    10005 22 38 80
 
標準輸出的重定向  
 
將一條命令執行結果(標準輸出,原本都要打印到屏幕上面的) 重定向其它輸出設備(文件,打開文件操做符,或打印機等等)
 
command > filename      把標準輸出重定向到一個新文件
command 1 > filename     同上
command >> filename     把標準輸出重定向到一個文件中(追加)
command > &m          把標準輸出重定向到文件描述符m中
command 1>&-           關閉標準輸出
eg:
 
[root@viong_app1 viong]# ll >test
[root@viong_app1 viong]# cat test
總計 12
-rw-r--r-- 1 root root     0 05-29 16:52 test
drwxr-xr-x 2 root root 4096 04-22 21:44 viong
 
[root@viong_app1 viong]# ll 1>test
[root@viong_app1 viong]# cat test
總計 12
-rw-r--r-- 1 root root     0 05-29 16:52 test
drwxr-xr-x 2 root root 4096 04-22 21:44 viong
-------------------------------------------------------------
[root@viong_app1 viong]# ll >>test
[root@viong_app1 viong]# cat test
總計 12
-rw-r--r-- 1 root root     0 05-29 16:52 test
drwxr-xr-x 2 root root 4096 04-22 21:44 viong
總計 16
-rw-r--r-- 1 root root 101 05-29 16:52 test
drwxr-xr-x 2 root root 4096 04-22 21:44 viong
 
shell遇到」>」操做符,會判斷右邊文件是否存在,若是存在就先刪除,而且建立新文件。不存在直接建立。 不管左邊命令執行是否成功。右邊文件都會變爲空
 
eg:
[root@viong_app1 soft]# >test    #test文件存在,就清空內容,不存在就建立一個空文件test
 
標準錯誤的重定向
 
command 2 > filename   把標準錯誤重定向到一個新文件
command >> filename     把標準錯誤重定向到一個文件中(追加)
eg:
 
[root@viong_app1 soft]# ll hom hom1 2>err.txt
-rw-r--r-- 1 root root 101 05-09 18:45 hom
 
[root@viong_app1 soft]# cat err.txt
ls: hom1: 沒有那個文件或目錄
 
[root@viong_app1 soft]# ll hom hom1 2>>err.txt
-rw-r--r-- 1 root root 101 05-09 18:45 hom
 
[root@viong_app1 soft]# cat err.txt
ls: hom1: 沒有那個文件或目錄
ls: hom1: 沒有那個文件或目錄
 
--------------------------------------------------------------------------------------------------
[root@viong_app1 soft]# ll hom hom1 2>/dev/null
-rw-r--r-- 1 root root 101 05-09 18:45 hom
 
[root@lvs_app1 soft]# ll hom hom1 1>/dev/null 2>/dev/null      #把標準輸出和錯誤轉發給/dev/null
 
[root@lvs_app1 soft]# ll hom hom1 &>/dev/null    #把標準輸出和錯誤轉發給/dev/null
 
-------------------------------------------------------------------------------------------------
[root@lvs_app1 soft]# ll hom hom1 2>&-          #關閉標準錯誤
-rw-r--r-- 1 root root 101 05-09 18:45 hom
 
[root@lvs_app1 soft]# ll hom hom1 1>&- 2>&-       #關閉標準輸出和錯誤
 
[root@lvs_app1 soft]# ll hom hom1 &>-           #關閉標準輸出和錯誤
 
標準輸出和標準錯誤的重定向
 
合併標準輸出和標準錯誤的時候,切記shell 是從作到右分析相應的命令
 
command > filename 2>&1 把標準輸出和標準錯誤一塊兒重定向到一個文件中
 
命令解釋:首先command命令標準輸出到filename文件,如遇到標準錯誤輸出到&1.而&1表明缺省屏幕(標準輸出),而缺省屏幕輸出到filename文件,及全部錯誤和結果輸出到filename文件中
 
command >> filename 2>&1 把標準輸出和標準錯誤一塊兒重定向到一個文件中(追加)
 
eg: 
[root@viong_app1 soft]# >err.txt                  #先清空err.txt文件內容
[root@viong_app1 soft]# ll hom hom1>err.txt 2>&1 #標準輸出和錯誤都重定向到err.txt文件中
 
[root@viong_app1 soft]# cat err.txt
ls: hom1: 沒有那個文件或目錄
-rw-r--r-- 1 root root 101 05-09 18:45 hom
 
[root@viong_app1 soft]# ll hom hom1>>err.txt 2>&1
 
[root@viong_app1 soft]# cat err.txt
ls: hom1: 沒有那個文件或目錄
-rw-r--r-- 1 root root 101 05-09 18:45 hom
ls: hom1: 沒有那個文件或目錄
-rw-r--r-- 1 root root 101 05-09 18:45 hom
-------------------------------------------------------------------------------------------------
[root@viong_app1 soft]# > err.txt
[root@viong_app1 soft]# ll hom hom1 2>err.txt 1>homnew  #標準輸出和錯誤分別重定向到不一樣文件中
[root@viong_app1 soft]# cat err.txt
ls: hom1: 沒有那個文件或目錄
[root@viong_app1 soft]# cat homnew
-rw-r--r-- 1 root root 101 05-09 18:45 hom
 
標準輸入和標準輸出的重定向
 
command < filename <filename 2    command命令以filename文件做爲標準輸入,以fi lename2文件做爲標準輸出
eg:
 
[root@viong_app1 soft]# cat name
2
3
5
6
9
1
4
[root@viong_app1 soft]# sort <name >name1相似cat name|sort>name1命令
[root@viong_app1 soft]# cat name1
1
2
3
4
5
6
9
 
exec
 
格式:
 
exec 文件描述符[n] <或> file或文件描述符或設備
 
在上面講的輸入,輸出重定向 將輸入,輸出綁定文件或設備後,只對當前那條指令是有效的,若是須要在綁定以後,接下來的全部命令都支持的話,就須要用exec命令
 
exec命令能夠用來替代當前shell,換句話說,並無啓動子shell,使用這一命令時任何現有環境變量將會被清除,並從新啓動一個shell
 
exec command
 
其中command一般是一個shell腳本
 
對文件描述符進行操做的時候, 它不會覆蓋當前的shell
eg:
 
[root@viong_app1 soft]# export viong=10
[root@viong_app1 soft]# echo $viong
10
[root@viong_app1 soft]# exec ./hello.sh
#這個時候退出當前shell,從新啓動一個shell
[root@viong_app1 ~]# echo $viong    #查看以前環境變量已被清除
 
[root@viong_app1 ~]#
--------------------------------------------------------------------------------------------------
[root@lvs_app1 soft]# exec 5<&1    #將標準輸出與文件描述符5綁定
 
 
[root@lvs_app1 soft]# ls /proc/self/fd #出現文件描述符5
0         1 2 3 5
 
[root@lvs_app1 soft]# exec 1>exec.txt #接下來全部命令標準輸出,綁定到exec.txt文件(輸出到該文件) 注意:這裏輸出的內容是追加的
 
[root@lvs_app1 soft]# ll              #執行命令後並沒有返回結果
 
[root@lvs_app1 soft]# exec 1<&5      #恢復標準輸出
[root@lvs_app1 soft]# ll              #執行命令後返回結果到屏幕上
 
總計 484
-rwxr-xr-x 1 root root    339 05-16 16:07 break.sh
-rwxr-xr-x 1 root root    269 05-16 17:01 case.sh
-rwxr-xr-x 1 root root    702 05-16 16:17 continue.sh
………………………..
[root@lvs_app1 soft]# exec 5>&-       #關閉文件描述符5
[root@lvs_app1 soft]# ls /proc/self/fd
0 1 2 3
 
--------------------------------------------------------------------------------------------------
[root@viong_app1 soft]# cat name
2
3
5
6
9
1
4
 
[root@viong_app1 soft]# cat exec.sh
#!/bin/bash
#exec_test
exec 3<&0 0<name
read line1
read line2
exec 1<&3
echo $line1
echo $line2
 
[root@viong_app1 soft]# ./exec.sh
2
3
[root@lvs_app1 soft]# cat exec.sh
#!/bin/bash
#exec_test
exec 3<>name            #打開name可讀性操做,與文件描述符3綁定
while read line<&3
do
echo $line
done
exec 3>&-
exec 3<&-
 
[root@lvs_app1 soft]# ./exec.sh
2
3
5
6
9
1
4
------------------------------------------------------------------------------------------------------
[root@lvs_app1 soft]# exec 6<&1
[root@lvs_app1 soft]# ls /proc/self/fd/
0 1 2 3 6
[root@lvs_app1 soft]# exec 6<&1-
-bash: echo: write error: 錯誤的文件描述符
-bash: echo: write error: 錯誤的文件描述符
[root@lvs_app1 soft]# exec 6<&-
-bash: echo: write error: 錯誤的文件描述符
-bash: echo: write error: 錯誤的文件描述符
 
遇到以上由於疏誤致使沒法輸入任何命令,就要利用以前重啓shell命令
[root@viong_app1 soft]# exec ./hello.sh    #重啓到另一個shell
 
[root@lvs_app1 ~]# ls /proc/self/fd/      #恢復正常狀態
0         1 2 3
 
管道|(pipe)與文件重定向區別
一、管道命令只處理前一個命令正確輸出,不處理錯誤輸出
二、管道命令右邊命令,必須可以接收標準輸入流命令才行。
接收標準輸入的命令才能夠用做管道右邊。不然傳遞過程當中數據會拋棄。 經常使用來做爲接收數據管道命令有:sed,awk,cut,head,top,less,more,wc,join,sort,split 等等,都是些文本處理命令。
相關文章
相關標籤/搜索