Shell:輸出幫助
阿新 • • 發佈:2020-07-18
目錄
輸出幫助
日常執行指令碼的時候,時間久了不知道指令碼的作用和實行了哪些功能,需要重新看指令碼原始碼。因此,需要對指令碼做一下輸出幫助。
執行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.