LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   script to search inside list of files (https://www.linuxquestions.org/questions/programming-9/script-to-search-inside-list-of-files-630474/)

adam_blackice 03-25-2008 08:07 AM

script to search inside list of files
 
Dear all,

i want to make a script that loop throw list of the files and search inside every file and if it find a specific pattern inside any file from the list passed it will append another file to it, this list will be passed with full path name .

the file which is file.txt contains all the files names, looks like that

/home/file1
/home/file2
/home/file3

i wrote that script but it didn't succeeded

Code:

#!/bin/bash

for i in `cat /home/file.txt`

do

grep "default.tmp1" $i

if [ echo $?=0 ]

then

cat /home/shadow > /home/$i

echo $i

fi

done

any help will be appreciated

colucix 03-25-2008 09:06 AM

The line
Code:

if [ echo $?=0 ]
is wrong. You can test the value of $? like this
Code:

if [ $? -eq 0 ]
but you can also use the if statement to evaluate a command: in this case its exit code is evaluated, e.g. you can simply do
Code:

if grep -q "default.tmp1" $i
then
  cat /home/shadow > /home/$i
  echo $i
fi


Telemachos 03-25-2008 09:14 AM

For the record, if you really mean you want to "append" to the file, you want
Code:

cat /home/whatever >> /home/whatever_else
If you only use a single >, you will overwrite the contents of the file you are writing to (ie, wipe out the old data and insert the new data).

adam_blackice 03-25-2008 09:15 AM

Dear colucix

thanks for your tip it works but i had a new error after running the script i had this : -

./replace_advance.sh: line 15: /home/adam//home/adam/file4.zone: No such file or directory

iam sure of the existence of the file4 for at this path at the main file and also in it's directory so what do you think about this error thanks again .

adam_blackice 03-25-2008 09:17 AM

thanks Telemachos but i want to append all the file contents to replace the old one so i use the right operator :) but i use user the wrong word "append" thanks for your help

adam_blackice 03-25-2008 09:35 AM

sorry i know where exactly my fault the statement should be wrote like that

cat /home/adam/shadow > $i

and i want to thank you all for your last response :)


All times are GMT -5. The time now is 06:51 AM.