LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash Script Help with Find (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-help-with-find-4175478744/)

sedna06 09-27-2013 07:36 AM

Bash Script Help with Find
 
Hello LQ,

I am working on a bash script that uses find and sed to find a list of files and perform a replacement

their are about 150 files and I am trying to combine an iF statement with the find and sed to show completed when all files are updated but it is not working.

as a side not how would I add a count function to show file 1 change and increment it to file 2 file 3 and so on?


Code:

function wpdatabase {
        clear
        echo "command to change wpdatabase"
        if(find . -type f -name "wp-config.phip" -exec sed -i "s/define('DB_HOST', 'localhost')/define('DB_HOST', '10.0.0.1')/g" {} \;);then
        echo "done"
        else
        echo "not"
        fi
}


any help would be appreciated

Firerat 09-27-2013 08:22 AM

instead of trying to get find to -exec your commands, use while read

Code:

while read File;do
  sed -i '....' "$File"
done <<< $(find .... -print0)

your counter is a little more complicated,. sed doesn't tell you if it actually did something ( at least I'm not aware of it )

couple of options,
add the backup suffix

sed -i.backup
test for the backup before, ( remove it )
test for backup after, add to your counter

or, see if grep gets a match to your pattern, when it does run sed, if sed doesn't fail ( exit with non-zero ) add to counter

a simple counter

Code:

c=5
for i in {1..10};do
    c=$(($c+1))
    echo $c
done

alternative, add the filenames that are modified to an array,. you can then 'report' after your loop

you can use the array to report the counts
echo "${#MyArray[@]}"
http://mywiki.wooledge.org/BashGuide/Arrays

SAbhi 09-27-2013 08:39 AM

Thats a good logic @firerat...

even you can test for the exit status a non-zero means failure and zero would be success so counter +1.

there could be many ways apart from find.. one that used in above, another ***just for an example:

for i in $(ls | grep -i "file_pattern" | xargs echo)
do
do.something....
done

you can use "-c " with grep to get a count.

Firerat 09-27-2013 09:14 AM

ls is seldom a good idea for things like this

it is actually touch upon in the arrays link
http://mywiki.wooledge.org/BashGuide/Arrays

but you have me thinking

grep --help
Code:

...
  -r, --recursive          like --directories=recurse
  -R, --dereference-recursive  likewise, but follow all symlinks
      --include=FILE_PATTERN  search only files that match FILE_PATTERN
      --exclude=FILE_PATTERN  skip files and directories matching FILE_PATTERN
      --exclude-from=FILE  skip files matching any file pattern from FILE
      --exclude-dir=PATTERN  directories that match PATTERN will be skipped.
  -L, --files-without-match  print only names of FILEs containing no match
  -l, --files-with-matches  print only names of FILEs containing matches
  -c, --count              print only a count of matching lines per FILE
  -T, --initial-tab        make tabs line up (if needed)
  -Z, --null                print 0 byte after FILE name
...


so, instead of find

Code:

FileArray=( $( grep -lrz --include=wp-config.phip "define('DB_HOST', 'localhost')"  /path/to/search ) )
for File in "${FileArray[@]}";do
    sed -i "s/define('DB_HOST', 'localhost')/define('DB_HOST', '10.0.0.1')/" "${File}" || SedFail+=("${File}")
done
echo "${#FileArray[@]}"
echo "${FileArray[@]}"
echo "${#SedFail[@]}"
echo "${SedFail[@]}"

fill out the report ;)

SAbhi 09-27-2013 09:50 AM

aah cool what a good use of -l and -z.. thanks @firerat .. was a bit thinking why i didnt tried --help :P

sedna06 09-27-2013 09:53 AM

Many Thanks Guys and Firerat

@Firerat just a quick one how would get the filearray to echo its results on a new line so at the moment I have

echo -e "file changed name \n${FileArray[@]}\n"

and it prints the files like file1, file2, file3 I have added \n but it dont work

how do I get it to

file1
file2
file3

many thanks

Joe

Firerat 09-27-2013 10:01 AM

:)

Arrays start at 0,

Code:

for i in $( seq 0 $(( ${#MyArray[@]} - 1 )) );do
    echo "${MyArray[$i]}"
done



All times are GMT -5. The time now is 03:15 PM.