Shell Programming

#Shell Programming

  • Bourne Shell

    • Change to Bourne Shell (sh)
      • % echo $SHELL
      • % sh -> $
  • Executable script

    • Shebang
      • #!/bin/sh
      • #!/bin/bash
      • #!/usr/local/bin/bash
      • #!/usr/bin/env bash
      • #!/usr/bin/env python
    • Execution
      • $ sh test.sh
      • $ chmod a+x test.sh
      • $ ./test.sh
  • Shell variables

    • Assignment
    • Access
  • Quotation marks

    • Single quotes(‘xxx’):
      • 把裡面的東西當字串直接印
    • Double quotes(“xxx”):
      • 把裡面的東西當成special的 變數或其他東西印出
    • Back quotes(xxx):
      • The stdout of the command
  • Shell variable operator

    • “Bad” : var is not set or the value is null
    • “Good” : var is set and is not null

  • Predefined shell variables

    • Environment Variables
    • Other useful variables:

      • $* : all arguments are formed into a long string
      • $@ : all arguments are formed into separated strings
  • awk

    • 檔案類型有:
      • d: directories 可用/^d/來讀取
      • -: files 可用/^-/來讀取
      • l: links 可用/^l/來讀取