When I use grep in TEST (line 18) it works perfectly. When I do the exact same command, but inside the for-loop (line 40), it does not work. Why? I’ve bent my brain on this problem too long now.
#!/bin/bash
clear
sourcelist=`ls *.c`
headerlist=`ls *.h`
FILE="headers.txt"
TEST="receive_elevator_data" #this is one typical entry in headers.tex
echo "TEST"
grep $TEST *.c -n
MODE="h"
if [ "$MODE" = "h" ]
then
echo "Searching.."
#for entry in $(cut -f 1 $FILE)
for entry in `cat $FILE`
do
echo "Looking at entry in $FILE: "
echo $entry
echo "Press any button to search, <s> for skip, <e> to exit"
read -e INPUT2
if [ "$INPUT2" = "s" ]
then
continue
fi
if [ "$INPUT2" = "n" ]
then
exit 1 #exit shell script
else
grep -n "${entry}" ${sourcelist}
fi
done
fi
Just to clarify: every line in headers.txt has strings like “test”, i.e no space in the lines. What headers.txt really contains is a list of function names extracted from all headerfiles in a C project 🙂 There was no returned error in my previous code, it was just the grep command in the loop that wouldn’t run. What I want to do, is to search through all C files for each function in headers.txt, and prompt me before each search
grepprobably fails because a filename contains spaces.dont use capital letter vars. those are better used by the environment.
never try to parse
lsoutput. use globbing instead.if you’re writting bash, then prefer
[[over[.do not
cat fileto read it. insteadreadit 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 ..)")