#!/bin/sh # Some weird script to find a word if [ $# -lt 1 ]; then echo "Usage: $0 [search_directory] [ignored_dir1] [ignored_dir2] ..." >&2 exit 1 fi WORD="$1" shift DIR="${1:-.}" shift if [ ! -d "$DIR" ]; then echo "Error: Directory '$DIR' does not exist." >&2 exit 1 fi IGNORED_DIRS="" while [ $# -gt 0 ]; do IGNORED_DIR="$1" # Construct prune conditions if [ -z "$IGNORED_DIRS" ]; then IGNORED_DIRS="-path \"$DIR/$IGNORED_DIR\" -prune" else IGNORED_DIRS="$IGNORED_DIRS -o -path \"$DIR/$IGNORED_DIR\" -prune" fi shift done if [ -n "$IGNORED_DIRS" ]; then FIND_COMMAND="find \"$DIR\" $IGNORED_DIRS -o -type f -print" else FIND_COMMAND="find \"$DIR\" -type f" fi eval "$FIND_COMMAND" | while read -r file; do if grep -q "$WORD" "$file" 2>/dev/null; then grep -n "$WORD" "$file" 2>/dev/null | while IFS=: read -r linenum line; do printf "%s:%d - %s\n" "$file" "$linenum" "$(echo "$line" | sed 's/^ *//')" done fi done