LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-27-2013, 07:36 AM   #1
sedna06
LQ Newbie
 
Registered: Jul 2012
Posts: 23

Rep: Reputation: Disabled
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
 
Old 09-27-2013, 08:22 AM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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
 
1 members found this post helpful.
Old 09-27-2013, 08:39 AM   #3
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 664

Rep: Reputation: 81
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.
 
Old 09-27-2013, 09:14 AM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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
 
Old 09-27-2013, 09:50 AM   #5
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 664

Rep: Reputation: 81
aah cool what a good use of -l and -z.. thanks @firerat .. was a bit thinking why i didnt tried --help :P
 
Old 09-27-2013, 09:53 AM   #6
sedna06
LQ Newbie
 
Registered: Jul 2012
Posts: 23

Original Poster
Rep: Reputation: Disabled
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
 
Old 09-27-2013, 10:01 AM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444


Arrays start at 0,

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help me! Script bash - Find the right device Susy Programming 4 02-06-2011 01:55 PM
[SOLVED] Using 'find' in a bash script Master_Ne0 Linux - Software 6 11-18-2010 01:00 PM
bash script to find/change nicolaus2 Programming 6 02-26-2010 09:50 AM
find a string within another with bash script jasper25 Linux - Newbie 3 02-01-2009 08:08 AM
how to find files using bash script prernasin Linux - Newbie 6 09-26-2007 08:57 AM


All times are GMT -5. The time now is 12:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration