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?