Shell:輸出幫助


Blog:博客園 個人

目錄

輸出幫助

日常執行腳本的時候,時間久了不知道腳本的作用和實行了哪些功能,需要重新看腳本源碼。因此,需要對腳本做一下輸出幫助。

執行script.sh -h來顯示腳本使用幫助。

格式參考:

###
### my-script — does one thing well
###
### Usage:
###   my-script <input> <output>
###
### Options:
###   <input>   Input file to read.
###   <output>  Output file to write. Use '-' for stdout.
###   -h        Show this message.

help() {
    sed -rn 's/^### ?//;T;p' "$0"
}

if [[ $# == 0 ]] || [[ "$1" == "-h" ]]; then
    help
    exit 1
fi

sed -rn 's/^### ?//;T;p' "$0"說明:

  • $0:腳本名;
  • -rn:使用擴展元字符集,屏蔽默認輸出;
  • s/^### ?//:匹配### 開頭的行,並刪掉###
  • T:若前面替換失敗則跳轉的sed腳本最后;
  • p:輸出替換后的結果;

執行script.sh -h

[root@test ~]# ./aa.sh -h

my-script — does one thing well

Usage:
  my-script <input> <output>

Options:
  <input>   Input file to read.
  <output>  Output file to write. Use '-' for stdout.
  -h        Show this message.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM