summaryrefslogtreecommitdiff
path: root/sisc
blob: 137bd4832d691b55a8ffac543f05b3258ec94130 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/sh

# Some weird script to find a word

if [ $# -lt 1 ]; then
    echo "Usage: $0 <search_word> [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