#!/bin/zsh
# 使い方:
# ./drillgrep.sh <検索ワード>
#
# 例:
# ./drillgrep.sh TODO
if [ -z "$1" ]; then
echo "使い方: $0 <検索ワード>"
exit 1
fi
search_term="$1"
# ファイル一覧取得
files=($(grep -rl -- "$search_term" ./))
if [ ${#files[@]} -eq 0 ]; then
echo "ヒットなし"
exit 0
fi
echo "=== '${search_term}' を含むファイル一覧 ==="
for i in {1..${#files[@]}}; do
echo "$i: ${files[$i]}"
done
# 入力待ち
echo -n "番号を選んでください (qで終了): "
read choice
if [[ "$choice" == "q" ]]; then
echo "終了"
exit 0
fi
if ! [[ "$choice" =~ '^[0-9]+$' ]]; then
echo "数値を入力してください"
exit 1
fi
if (( choice < 1 || choice > ${#files[@]} )); then
echo "不正な番号です"
exit 1
fi
target_file="${files[$choice]}"
echo "=== ${target_file} を検索 ==="
grep -n -- "$search_term" "$target_file"
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme