Bash script to search text in multiple files with context lines, enabling quick and recursive searches in specified directories using grep.
#!/bin/bash
# Script to search text in multiple files and show context
# Check that two arguments are provided: word to search and directory
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <text_to_search> <directory>"
exit 1
fi
TESTO="$1"
DIR="$2"
# Number of context lines to show before and after the match
CONTEXT=2
# Check that the directory exists
if [ ! -d "$DIR" ]; then
echo "Error: directory '$DIR' does not exist."
exit 1
fi
# Perform recursive search with grep showing context and line number
# -r: recursive
# -n: show line number
# -C $CONTEXT: show $CONTEXT lines before and after the match
# --color=auto: highlight the searched word
grep -r -n -C $CONTEXT --color=auto "$TESTO" "$DIR"
exit 0Bash: shell script executed in Linux/Unix environment.
grep: command used for text searching with options to show context.
User input: word or phrase to search and directory where to perform the search.
Configuration parameters: number of context lines to show (default 2).
Dependencies/configurations: the script requires grep to be installed and available in the PATH.
No hardware or Arduino connections required.