Created
July 2, 2023 03:13
-
-
Save raybird/8d825da7a3c5f3aa48f3d652edb9ff70 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# 定義 hello 函數 | |
hello() { | |
# 輸出 "hello" | |
echo "hello" | |
} | |
# 定義 hello2 函數 | |
hello2() { | |
# 輸出 "hello2" | |
echo "hello2" | |
} | |
# 定義 hi 函數,可以接收參數 | |
hi() { | |
# 輸出傳入的參數 | |
echo "hello $1" | |
} | |
# 檢查是否有提供參數 | |
if [[ $# -ge 2 ]]; then | |
# 判斷輸入的參數是不是已定義的函數名稱 | |
if [[ $(declare -f -F "$1") ]]; then | |
# 呼叫指定的函數並傳入第二個參數以後的參數 | |
"$@" | |
else | |
# 輸出所有函數的名稱與註解 | |
while IFS= read -r line; do | |
if [[ $line =~ ^# ]]; then | |
comment=$(echo "$line" | sed 's/# *//') | |
read -r next_line | |
if [[ $next_line =~ ^[a-zA-Z0-9_]+\(\) ]]; then | |
name=$(echo "$next_line" | awk '{ print $1 }' | sed 's/()//') | |
printf "%-10s\t%s\n" "$name" "$comment" | |
fi | |
fi | |
done < "$0" | sort -k2 | |
fi | |
else | |
# 輸出所有函數的名稱與註解 | |
while IFS= read -r line; do | |
if [[ $line =~ ^# ]]; then | |
comment=$(echo "$line" | sed 's/# *//') | |
read -r next_line | |
if [[ $next_line =~ ^[a-zA-Z0-9_]+\(\) ]]; then | |
name=$(echo "$next_line" | awk '{ print $1 }' | sed 's/()//') | |
printf "%-10s\t%s\n" "$name" "$comment" | |
fi | |
fi | |
done < "$0" | sort -k2 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment