Results 1 to 3 of 3
Here is what I have thus far.
!#/bin/sh
# scanner.sh: Script to search files ending with .c will be searched for strings containing
# printf or fprintf. If the string ...
- 12-08-2006 #1Just Joined!
- Join Date
- Dec 2006
- Posts
- 2
Script to find *.c files and insert a line
Here is what I have thus far.
!#/bin/sh
# scanner.sh: Script to search files ending with .c will be searched for strings containing
# printf or fprintf. If the string is found the script will add #include <studio.h> if it is not already
# included in the .c file.
#
#
echo "The following files were found and already have studio included:"
find ~/1311 -name "*.c"|xargs grep -Hn '#include <studio.h>' >studio.tmp;
# Find files with printf and fprintf and copy names to a temp file
find ~/1311 -name "*.c"|xargs grep -F 'printf' > printf.tmp;
find ~/1311 -name "*.c"|xargs grep -F 'fprintf'> fprintf.tmp;
# Use Tempfile to check each file for the string.
------------------
This script is supposed to find all *.c files in a folder (I chose 1311 as my folder) and check the files for printf or fprintf. If it is found, it should insert a new line at the top of the file. I am not sure how to implement what I have using if statements (I assume that is what I should do next). What do I do next?
Luis
- 12-08-2006 #2
Well, here's one thing I imagine you can do.
Once you have a list of files that already have studio.h (do you mean studio.h or stdio.h?) in, say, already_good, you could do something like this:
What does this do? First, it finds every file in the subtree starting at ~/1311 that contains either 'fprintf' or 'printf'.Code:IFS='\012' for file in $(find ~/1311 -name "*.c" -exec egrep -l 'f?printf' {} \;); do if ! grep -q '#include <studio.h>' "$file"; then echo '#include <studio.h>' > temp.$$ cat "$file" >> temp.$$ mv temp.$$ "$file" fi done
It then checks if the file has '#include <studio.h>'. If it does, we go on to the next file.
If it does not have '#include <studio.h>', we do something cool. We first print out '#include <studio.h>' to a pretty unique file ($$ is the current PID). We then append the text of the file to the unique file. We then replace the original C source with the new file.
Does that all make sense?DISTRO=Arch
Registered Linux User #388732
- 12-08-2006 #3Just Joined!
- Join Date
- Dec 2006
- Posts
- 2
Thanks! That was exactly what I was trying to figure out. I just could not figure out how to include the if and else statement.
Luis


Reply With Quote