wiki:YM_Course_2009/Lab4
實作四: Bash Shell Script 實作練習

◢ <實作三> | <回課程大綱> ▲ | <實作五> ◣

1. 重新導向練習

  • 首先,讓我們來練習 I/O 重新導向表上的指令。
  • 使用 > 或 1> 把 ls 指令的 STDOUT 結果導向到 file 與 file2 這兩個檔案,結果是相似的。
  • 你可能會注意到 file 裡有出現 file,file2 裡才有出現 file2,代表檔案是先產生,然後才執行 ls 指定的。
    ubuntu@ubuntu:~$ ls > file
    ubuntu@ubuntu:~$ cat file
    file
    profile
    temp
    temp2
    ubuntu@ubuntu:~$ ls 1> file2
    ubuntu@ubuntu:~$ cat file2
    file
    file2
    profile
    temp
    temp2
    
  • 使用 2> 把 ls 指令的 STDERR 導向到 file3,你會發現 file3 的內容是空的,因為並沒有出現錯誤,而 STDOUT 的結果還是出現在畫面上。
    ubuntu@ubuntu:~$ ls 2> file3
    file  file2  file3  profile  temp  temp2
    ubuntu@ubuntu:~$ cat file3
    
  • 我們刻意下錯指令,例如:ls temp3 來產生 STDERR 輸出。
    ubuntu@ubuntu:~$ ls temp3 2> file4
    ubuntu@ubuntu:~$ cat file4
    ls: cannot access temp3: No such file or directory
    
  • 我們刻意下指令,例如:ls temp temp3 來同時產生 STDOUT 與 STDERR 輸出。
    ubuntu@ubuntu:~$ ls temp temp3
    ls: cannot access temp3: No such file or directory
    temp:
    ubuntu@ubuntu:~$ ls temp temp3 > file5 2> file6
    ubuntu@ubuntu:~$ cat file5
    temp:
    ubuntu@ubuntu:~$ cat file6
    ls: cannot access temp3: No such file or directory
    ubuntu@ubuntu:~$ ls temp temp3 > file7 2>&1
    ubuntu@ubuntu:~$ cat file7
    ls: cannot access temp3: No such file or directory
    temp:
    ubuntu@ubuntu:~$ ls temp temp3 2> file8 1>&2
    ubuntu@ubuntu:~$ cat file8
    ls: cannot access temp3: No such file or directory
    temp:
    
  • 因為 file8 存在,因此當我們用 >> 來重新導向 cat file 指令的 STDOUT 結果,會附加到 file8 檔案後面。
    ubuntu@ubuntu:~$ cat file >> file8
    ubuntu@ubuntu:~$ cat file8
    ls: cannot access temp3: No such file or directory
    temp:
    file
    profile
    temp
    temp2
    
  • 使用 2>> 把 ls temp temp3 的 STDERR 附加到 file8
    ubuntu@ubuntu:~$ ls temp temp3 2>> file8
    temp:
    ubuntu@ubuntu:~$ cat file8
    ls: cannot access temp3: No such file or directory
    temp:
    file
    profile
    temp
    temp2
    ls: cannot access temp3: No such file or directory
    
  • cat 用 < 來讀取 STDIN
    ubuntu@ubuntu:~$ cat < /etc/profile
    
  • 使用 << EOF 來一次輸入很多資料
    ubuntu@ubuntu:~$ cat > cat-test.txt << EOF
    aaa1
    bbb2
    ccc3
    
    ddd4
    eee5
    
    
    fff6
    ggg7
    
    EOF
    
  • 重新導向到 /dev/null 就不會顯示了
    ubuntu@ubuntu:~$ ls temp temp2 > /dev/null 2>&1
    

2. pipe 管路

  • 有時候需要透過好幾個指令把 STDIN 跟 STDOUT 串在一起,就可以很快找出想要的資訊。
    ubuntu@ubuntu:~$ echo "file"
    file
    ubuntu@ubuntu:~$ echo "file" | cat 
    file
    ubuntu@ubuntu:~$ cat cat-test.txt | sort -n
    
    
    
    aaa1
    bbb2
    ccc3
    ddd4
    eee5
    fff6
    ggg7
    
  • wc 指令可以拿來統計字數,這裡代表總共有 11 行,7個非空白行,總字數為 39 (包含換行符號)
    ubuntu@ubuntu:~$ cat cat-test.txt | wc
         11       7      39
    
  • nl 指令可以拿來統計行號,效果與 cat -n 一樣
    ubuntu@ubuntu:~$ cat cat-test.txt | nl
         1	aaa1
         2	bbb2
         3	ccc3
           
         4	ddd4
         5	eee5
           
           
         6	fff6
         7	ggg7
    
  • 有時候輸出太多,想要分頁慢慢看的話,可以用 more 跟 less 來看。
  • 另外,可以用 head 跟 tail 來顯示開頭或結尾特定行數的內容
    ubuntu@ubuntu:~$ head -n 2 cat-test.txt 
    aaa1
    bbb2
    ubuntu@ubuntu:~$ tail -n 3 cat-test.txt 
    fff6
    ggg7
    
    

3. test 與 Shell Script

  • 在學習寫 Shell Script 之前,可以先學著用 test 指令來確認自己寫的判斷式正不正確。在 Bash 環境中,變數 $? 代表上一次執行命令的結果,若為 0 代表成功/條件成立/真;若為 1 代表失敗/條件不成立/假。
    ubuntu@ubuntu:~$ test -e file
    ubuntu@ubuntu:~$ echo $?
    0
    ubuntu@ubuntu:~$ test -e file0
    ubuntu@ubuntu:~$ echo $?
    1
    
  • 同樣的動作也可以用 [] 或 [[]] 來完成
    ubuntu@ubuntu:~$ [ -e file ]
    ubuntu@ubuntu:~$ echo $?
    0
    ubuntu@ubuntu:~$ [[ -e file0 ]]
    ubuntu@ubuntu:~$ echo $?
    1
    
  • Bash Shell 的 if 判斷式語法為
    if [ 條件一 ]; then
      若條件一為成立(真,0)時,要做的動作
    elif [ 條件二 ]; then
      若條件二為成立(真,0)時,要做的動作
    else
      若條件一與條件二均為不成立(假,1)時,要做的動作
    fi
    
  • 範例:
    ubuntu@ubuntu:~$ if [ -e file ]; then echo "file 存在"; fi
    file 存在
    ubuntu@ubuntu:~$ if [ -e file0 ]; then echo "file0 存在"; else echo "file0 不存 在"; fi
    file0 不存在
    ubuntu@ubuntu:~$ if [ -e file ]; then echo "file 存在"; elif [ -e file0 ]; then echo "file0 存在"; else echo "file0 不存在"; fi
    file 存在
    ubuntu@ubuntu:~$ if [ -e file0 ]; then echo "file0 存在"; elif [ -e file ]; then echo "file 存在"; else echo "file, file0 均不存在"; fi
    file 存在
    ubuntu@ubuntu:~$ if [ -e file0 ]; then echo "file0 存在"; elif [ -e fileA ]; then echo "fileA 存在"; else echo "file0, fileA 均不存在"; fi
    file0, fileA 均不存在
    

4. Shell Script 範例

cat > test1.sh << EOF
#!/bin/bash
echo "Enter a filename: "
read filename
if [ ! -r "\$filename" ]
 then
   echo "File is not read-able"
 exit 1
fi
EOF
chmod a+x test1.sh
ubuntu@ubuntu:~$ ./test1.sh 
Enter a filename: 
file
ubuntu@ubuntu:~$ ./test1.sh 
Enter a filename: 
ab
File is not read-able
cat > test2.sh << EOF
#! /bin/bash

if [ \$# -lt 1 ]; then
        echo "Usage: filetest filename"
        exit 1
fi
if [[ ! -f "\$1" || ! -r "\$1" || ! -w "\$1" ]]
then
  echo "File \$1 is not accessible"
  exit 1
fi
EOF
chmod a+x test2.sh
ubuntu@ubuntu:~$ ./test2.sh 
Usage: filetest filename
ubuntu@ubuntu:~$ ./test2.sh filename
File filename is not accessible
ubuntu@ubuntu:~$ ./test2.sh file
Last modified 15 years ago Last modified on Jul 4, 2009, 4:48:23 PM

Attachments (2)

Download all attachments as: .zip