Register
It is currently Mon Nov 24, 2014 12:49 am

Need help with for loop


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Tue Jul 08, 2008 1:30 pm   
User avatar
Hi

I have the following script:
Code:
#!/bin/bash
# Copyright 2008 by domin - http://domin.eu.org

WHERE="/home/domin/test"
WHAT=$1

cd $WHERE
touch .where .where_space .where_without_space

ls $WHERE | grep .xml > .where
cat .where | sed -ne 's/[[:space:]]/\\ /p' > .where_space
cat .where | grep -v [[:space:]] > .where_without_space
mv .where_space .where_all
cat .where_without_space >> .where_all
rm .where .where_without_space

touch .list_of_files .tmp

for fil in $(cat .where_all)
do
cat $fil | grep $WHAT > .tmp
if [[ $(cat .tmp | wc -l) -gt "0" ]]; then
echo "$WHERE/$fil" >> .list_of_files
fi
done

echo -e "Files where found phrase \"$WHAT\""
cat .list_of_files
echo -e "\n\nThe contents of files:"
for files in $(cat .list_of_files)
do
echo -e "File: $files\nContents:"
cat $files
echo -e "\n"
done

rm .list_of_files .tmp .where_all

exit


This script should find the custom phrase, which user enter (via $1) in files *.xml in directory. But I have one big problem - when filename is two-clause the scripts output error. So, I wrote a small function to rename for example '4 test.xml' to '4\ test.xml':
Code:
ls $WHERE | grep .xml > .where
cat .where | sed -ne 's/[[:space:]]/\\ /p' > .where_space
cat .where | grep -v [[:space:]] > .where_without_space
mv .where_space .where_all
cat .where_without_space >> .where_all
rm .where .where_without_space

But it isn't still working. When I manualy use the function to searching phrase in file (I use a '4\ test.xml' as file name):
Code:
cat $fil | grep $WHAT > .tmp
if [[ $(cat .tmp | wc -l) -gt "0" ]]; then
echo "$WHERE/$fil" >> .list_of_files
fi

it works, but when I try to use it in for loop it doesn't work - for example when I have a '4 test.xml' file, which is renamed to '4\ test.xml' the scripts interpret this file as two separated files: '4\' and 'test.xml'.

Anyone have idea howto repair?


Top
  
 PostPosted: Wed Jul 09, 2008 10:48 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
"for" splits input by spaces which is why you get the behaviour. As far as I know "for" is not capable of getting around this for this type of problem. In the past I have used something like...

Code:
cat .where_all | while read fil
do
# blah blah
done


which should solve the immediate problem, however using pipes in this fashion makes bash do some weird things so you cannot use "exit" within the loop. In this case it shouldn't matter.

Now I may have missed something but from what I have seen of your script you may be able to replace it with something like...
Code:
#!/bin/bash

echo -e "Files where found phrase \"$WHAT\""
grep -l "$WHAT" $WHERE/*.xml

echo -e "\n\nThe contents of files:"
grep -l "$WHAT" $WHERE/*.xml | while read ifile
do
  echo -e "File: $ifile\nContents:"
  cat $ifile
done


Top
 Profile  
 PostPosted: Wed Jul 09, 2008 12:10 pm   
User avatar
I tried yours solution, but it still doesn't work. Cat again and again output a error. I think that problem is precisely with cat (not with for loop) - it interpret '4 test.xml' as two files '4' and 'test.xml'. My script has this average, that rename '4 test.xml' - which isn't work manualy to '4\ test.xml' which manualy works (I mean manualy as manualy enter a commands).
Code:
domin@niunia ~/test $ cat 4 kopia.xml
cat: 4: Nie ma takiego pliku ani katalogu
cat: kopia.xml: Nie ma takiego pliku ani katalogu
domin@niunia ~/test $ cat 4\ kopia.xml
cztery
piec piec

Anyone has other ideas or I must use a perl or other language?


Top
  
 PostPosted: Thu Jul 10, 2008 11:04 am   

Joined: Tue Apr 01, 2008 10:19 am
Posts: 49
So let me get this straight.. You want to search all .xml files for a phrase that you pass?


Code:
ROOTDIR='/path/to/files/'
phrase="$1"

find "$ROOTDIR" -type f -name *.xml -exec grep "$1" {} \;


Top
 Profile  
 PostPosted: Thu Jul 10, 2008 1:47 pm   
User avatar
Yours solution isn't working. But even if it will be working the errors will be as before. Because when we want a cat '4 test.xml' or '4\ test.xml' file then the cat crashes. However, I'm surpsised that when I manualy enter a cat '4\ test.xml' it works, but when I put it into a script - cat gives errors on output.

I want script which do:
- search all xml files in directory for a typed phrase
- when that files are - make a list of them
- show a contents of each file from a list

I try various solutions, but I still have one problem - cat don't work with to-clause filenames (I wrote about it in first post).


Top
  
 PostPosted: Fri Jul 11, 2008 2:32 am   
User avatar
Yeah, I reparied it and now it works!

Code:
#!/bin/bash
# Copyright 2008 by domin - http://domin.eu.org

WHERE="/home/shells/domin/test"
WHAT=$1

cd $WHERE
touch .where .where_space .where_without_space .tmp .list_of_files .list_of_files_space .list_of_files_without_space .list_of_files_with_space

ls $WHERE | grep .xml > .where
cat .where | sed -ne 's/[[:space:]]/\\ /p' > .where_space
cat .where | grep -v [[:space:]] > .where_without_space

touch .where_all
cat .where_space > .where_all
cat .where_without_space >> .where_all

cat .where_all | while read fil
do
cat "${fil}" | grep $WHAT > .tmp
if [[ $(cat .tmp | wc -l) -gt "0" ]]; then
echo "$WHERE/${fil}" >> .list_of_files
fi
done

cat .list_of_files | sed -ne 's/[[:space:]]/\\ /p' > .list_of_files_space
cat .list_of_files | grep -v [[:space:]] > .list_of_files_without_space
cat .list_of_files | grep [[:space:]] > .list_of_files_with_space
echo "" > .list_of_files
cat .list_of_files_with_space > .list_of_files
cat .list_of_files_without_space >> .list_of_files

echo -e "Files where found phase \"$WHAT\""
cat .list_of_files
echo -e "\n\nThe contents of files:"

cat .list_of_files | while read files
do
echo -e "File: $files\nContents:"
cat "$files"
echo -e "\n"
done

rm .list_of_files .tmp .where_without_space .list_of_files_space .where .where_space .list_of_files_with_space .where_all .list_of_files_without_space

exit


And sample output from script:
Code:
domin@niunia ~ $ ./znajdz.sh piec
Files where found phase "piec"
/home/shells/domin/test/1.xml
/home/shells/domin/test/2.xml
/home/shells/domin/test/3.xml
/home/shells/domin/test/4.xml
/home/shells/domin/test/pliczek.xml
/home/shells/domin/test/4 kopia.xml
/home/shells/domin/test/testowy pliczek.xml


The contents of files:
File: /home/shells/domin/test/1.xml
Contents:
jeden
piec

File: /home/shells/domin/test/2.xml
Contents:
dwa
piec

File: /home/shells/domin/test/3.xml
Contents:
trzy
piec

File: /home/shells/domin/test/4.xml
Contents:
cztery
piec piec

File: /home/shells/domin/test/pliczek.xml
Contents:
piec

File: /home/shells/domin/test/4 kopia.xml
Contents:
cztery
piec piec

File: /home/shells/domin/test/testowy pliczek.xml
Contents:
piec


Top
  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP