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 { any help would be appreciated |
instead of trying to get find to -exec your commands, use while read
Code:
while read File;do 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 you can use the array to report the counts echo "${#MyArray[@]}" http://mywiki.wooledge.org/BashGuide/Arrays |
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. |
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:
... so, instead of find Code:
FileArray=( $( grep -lrz --include=wp-config.phip "define('DB_HOST', 'localhost')" /path/to/search ) ) |
aah cool what a good use of -l and -z.. thanks @firerat .. was a bit thinking why i didnt tried --help :P
|
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 |
:)
Arrays start at 0, Code:
for i in $( seq 0 $(( ${#MyArray[@]} - 1 )) );do |
All times are GMT -5. The time now is 03:15 PM. |