Created
August 4, 2025 14:50
-
-
Save s-hiiragi/7b2d4aa0d42139e4cab2ee7029cde387 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
| #!/bin/bash | |
| usage_exit() { | |
| local PROG=$(basename "$0") | |
| cat <<EOS >&2 | |
| $PROG -- find a file towards the root directory | |
| $PROG [OPTIONS] FILENAME | |
| OPTIONS | |
| -d show only a directory name | |
| -h show this help | |
| EOS | |
| exit 1 | |
| } | |
| ONLY_DIRNAME=false | |
| case "$1" in | |
| -h | --h | "") | |
| usage_exit | |
| ;; | |
| -d) | |
| ONLY_DIRNAME=true | |
| shift | |
| ;; | |
| esac | |
| FILENAME="$1" | |
| dir=$(pwd) | |
| while :; do | |
| f="$dir/$FILENAME" | |
| if [[ -f "$f" ]]; then | |
| if "$ONLY_DIRNAME"; then | |
| echo "$(dirname "$f")" | |
| else | |
| echo "$f" | |
| fi | |
| exit 0 | |
| fi | |
| if [[ "$dir" == "/" ]]; then | |
| exit 1 | |
| fi | |
| dir=$(dirname "$dir") | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment