![スクリプトで分岐したロジックを処理し、読みやすさを維持する方法は? [閉鎖]](https://linux33.com/image/119447/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%81%A7%E5%88%86%E5%B2%90%E3%81%97%E3%81%9F%E3%83%AD%E3%82%B8%E3%83%83%E3%82%AF%E3%82%92%E5%87%A6%E7%90%86%E3%81%97%E3%80%81%E8%AA%AD%E3%81%BF%E3%82%84%E3%81%99%E3%81%95%E3%82%92%E7%B6%AD%E6%8C%81%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95%E3%81%AF%EF%BC%9F%20%5B%E9%96%89%E9%8E%96%5D.png)
パラメータに応じてスクリプトの機能を変更する最善の方法は何ですか?
私はこれができることを意味します:
if [ "$param" == "1" ]; then
# do code here
else
# do compeletely different code here
fi
しかし、コードがランダムに大きくなるとどうなりますか?
私はオブジェクト指向のアプローチを期待せずにスクリプトをきれいに保つ良い方法を期待しています。
答え1
これを「フォーク」ではなく「ブランチ」といいます。
スクリプトを関数に分割するか、基本スクリプトから呼び出される完全に独立した下付き文字を作成できます。
使用機能:
handle_param_1 () {
# do stuff for param == 1
}
handle_other_cases () {
# do other stuff
}
# the above functions could be written in separate files
# that you source to import their definitions
case "$param" in
1) handle_param_1 ;;
*) handle_other_cases ;;
esac
別のスクリプトを使用してください。
case "$param" in
1) somewhere/handle_param_1 ;;
*) somewhere/handle_other_cases ;;
esac