1. 程式人生 > >基礎Linux命令 --------- 輸入輸出管理

基礎Linux命令 --------- 輸入輸出管理

一、輸出管理

1.輸出重定向

   >                ##重定向正確的輸出

   2>              ##重定向錯誤的輸出

   &>              ##重定向所有的輸出

"注意":沒有重定向的情況(即預設情況)下,輸出會在/dev/pts/0中,並且重定向動作會覆蓋掉原檔案的內容"

 例子:

   (1) 在student使用者下查詢 /etc下的passwd檔案,將正確的輸出儲存到桌面的 file 檔案中,錯誤的輸出顯示在shell上

[[email protected] Desktop]$ find /etc/ -name passwd >file

       演示結果:      

              

  (2)在student使用者下查詢 /etc下的passwd檔案,將錯誤的輸出儲存到桌面的 file 檔案中,正確的輸出顯示在shell上

  [[email protected] Desktop]$  find /etc/ -name passwd 2>file

      演示結果:         

               

   (3)在student使用者下查詢 /etc下的passwd檔案,將所有的輸出儲存到桌面的 file 檔案中

   [[email protected]
Desktop]$  find /etc/ -name passwd &>file

          演示結果: 

               

2.輸出追加

  >>               ##追加正確的輸出

  2>>              ##追加錯誤的輸出

  &>>              ##追加所有的輸出

注意:追加會把定向的輸出放到原檔案最後不會覆蓋原檔案

"注意":1表示正確的,2表示錯誤的

二、輸入管理

1. echo  ---------將echo命令後輸入的內容顯示在shell中,也可以進行重定向

例子:

      

2.<<EOF

  內容

  EOF

例子:    (1)執行test.sh

#!/bin/bash 
cat > westos <<EOF
hello
world
EOF

        輸出結果如下:

[[email protected] Desktop]$ sh test.sh
hello
world

3.管道命令

   (1)|                   ##把第一條命令的"正確輸出"變成第二條命令的輸入

   (2)2>&1          ##將編號位2的錯誤輸出轉換成編號1

   (3)tee              ##將輸出複製一份到指定位置

例子:

    (1)統計/bin中有多少行檔案

   [[email protected] Desktop]$ ls /bin | wc -l  

演示結果:

            

    (2)在student使用者下查詢 /etc下的passwd檔案,將所有的輸出放在file中,並統計總行數

    [[email protected] Desktop]$ find /etc/ -name passwd 2>&1 | tee file |wc -l

        演示結果: