Find the answer to your Linux question:
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 ...
  1. #1
    Just 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

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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:

    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
    What does this do? First, it finds every file in the subtree starting at ~/1311 that contains either 'fprintf' or 'printf'.

    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

  3. #3
    Just 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...