grep inside for-loop

dont use capital letter vars. those are better used by the environment.
never try to parse ls output. use globbing instead.
if you’re writting bash, then prefer [[ over [.
do not cat file to read it. instead read it in a while loop.
always quote your variables. they’re also easier found if grouped.
treat a list of files, as an array.
if you have to use `` prefer $() which can be nested, and also quote it (foo="$(cmd ..)")

#!/bin/bash
clear

sourcelist=(*.c)
headerlist=(*.h)
file="headers.txt"
test="receive_elevator_data" # this is one typical entry in headers.tex
mode="h"

echo "searching test"
grep -n "$test" "${sourcelist[@]}"

if [[ "$mode" == "h" ]]; then
echo "Searching.."
while read -r entry; do
printf "Looking at entry in %s: %sn" "$file" "$entry"
read -p "Press any button to search, <s> for skip, <e> to exit" -e answer
[[ "$answer" = "s" ]] && continue
[[ "$answer" = "e" ]] && exit 1 # exit shell script
grep -n "$entry" "${sourcelist[@]}"
done < "$file"
fi

source:
stackoverflow.com/questions/10263769/linux-shell-script-grep-not-working-inside-for-loop

Tags: , , , , , , , , , , , , , ,

Getting Started with Linux: The Complete Guide