next up previous contents
Next: 開發工具 - make 與 Up: Shell scripts - 條件判斷與迴圈 Previous: 迴圈 (loop)   Contents

shell script 的追蹤與 debug

  • 以 bash 的相關參數來進行判斷
    1. sh 指令
      [root@linux ~]# sh [-nvx] scripts.sh
      選項:
      -n  :不要執行 script,僅查詢語法的問題;
      -v  :執行 sccript 前,先將 script 的內容輸出到螢幕上;
      -x  :將使用到的 script 內容顯示到螢幕上。
      
    2. 測試 sh16.sh 有無語法的問題?
      [root@linux ~]# sh -n sh16.sh 
      # 若語法沒有問題,則不會顯示任何資訊。
      
      [dywang@dywOffice ~]$ vi sh015.sh
        1 #!/bin/bash
        2 # Program:
        3 #       Using for .... loop to print 3 animal
        4 # History:
        5 # 2005/08/29    csie   First release
        6 PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
        7 export PATH
        8
        9 for animal in dog cat elephant
       10 do
       11         echo "There are ""$animal""s....
       12 done
      [dywang@dywOffice ~]$ sh -n sh015.sh
      sh015.sh: line 11: unexpected EOF while looking for matching `"'
      sh015.sh: line 13: syntax error: unexpected end of file
      
    3. 執行 sh15.sh 前,先將 scripts 的內容輸出到螢幕上。
      [dywang@dywOffice ~]$ sh -v sh015.sh
      #!/bin/bash
      # Program:
      #       Using for .... loop to print 3 animal
      # History:
      # 2005/08/29    csie   First release
      
      for animal in dog cat elephant
      do
              echo "There are ""$animal""s...."
      done
      There are dogs....
      There are cats....
      There are elephants....
      
    4. 將 sh15.sh 的執行過程全部列出來
      [root@linux ~]# sh -x sh15.sh 
      + PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/home/csie/bin
      + export PATH
      + for animal in dog cat elephant
      + echo 'There are dogs.... '
      There are dogs....
      + for animal in dog cat elephant
      + echo 'There are cats.... '
      There are cats....
      + for animal in dog cat elephant
      + echo 'There are elephants.... '
      There are elephants....
      
      # 最前面會加上 + 字號表示程式段落。
      

  • Linux 系統有很多啟動的 script,想要知道每個 script 的功能,可以直接以 cat, more, less 或 vi 進入該 script 查閱。 例如:查閱 script /etc/init.d/xfs:
    [root@dywHome ~]# cat /etc/init.d/xfs
    #!/bin/sh
    #
    # xfs:       Starts the X Font Server
    #
    # Version:      @(#) /etc/rc.d/init.d/xfs 1.4
    #
    # chkconfig: 2345 20 10
    # description: Starts and stops the X Font Server at boot time and shutdown.
    #
    # processname: xfs
    # config: /etc/X11/fs/config
    # hide: true
    #
    ### BEGIN INIT INFO
    # Provides: xfs
    # Required-Start: $network
    # Required-Stop: $network
    # Default-Start: 2 3 4 5
    # Short-Description: X Font Server
    # Description: X Font Server
    ### END INIT INFO
    
    # Source function library.
    . /etc/rc.d/init.d/functions
    
    RETVAL=0
    
    # See how we were called.
    case "$1" in
      start)
            gprintf "Starting X Font Server: "
            mkdir -p /tmp
            chmod a+w,+t /tmp   
    # sticky(+t):僅有該檔案/目錄建立者與 root 能刪除它。
            rm -fr /tmp/.font-unix
            daemon --check xfs xfs -port -1 -daemon -droppriv -user xfs
            touch /var/lock/subsys/xfs
            echo
            ;;
      stop)
            gprintf "Shutting down X Font Server: "
            killproc xfs
            rm -f /var/lock/subsys/xfs
            echo
            ;;
      status)
            status xfs
            RETVAL=$?
            ;;
      restart)
            gprintf "Restarting X Font Server. "
            if [ -f /var/lock/subsys/xfs -a ! -z "`pidof xfs`" ]; then
                killproc xfs -USR1
            else
                rm -fr /tmp/.font-unix
                daemon --check xfs xfs -port -1 -daemon -droppriv -user xfs
                touch /var/lock/subsys/xfs
            fi
            echo
            ;;
      *)
            gprintf "*** Usage: xfs {start|stop|status|restart}\n"
            exit 1
    esac
    
    exit $RETVAL
    
練習題
  1. 如何僅測試 shell script testsh.sh 有無語法的問題,而不要執行 script?
    Sol. sh -n testsh.sh
  2. 如何在執行 shell script testsh.sh 前,先將 script 的內容輸出到螢幕上?
    Sol. sh -v testsh.sh
  3. 如何在執行 shell script testsh.sh 時,將 script 的執行過程全部列出來?
    Sol. sh -x testsh.sh
  4. 如何查閱系統 script /etc/init.d/network 的內容?
    Sol. cat /etc/init.d/network (cat , more, less 或 vi 皆可)


2017-06-14