LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-25-2008, 09:07 AM   #1
adam_blackice
Member
 
Registered: Apr 2006
Location: /*Egypt */ //cairo
Distribution: Ubuntu 7.04 , SLED 10 , Fedora , RHEL 5
Posts: 312

Rep: Reputation: 32
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
 
Old 03-25-2008, 10:06 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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
 
Old 03-25-2008, 10:14 AM   #3
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
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).
 
Old 03-25-2008, 10:15 AM   #4
adam_blackice
Member
 
Registered: Apr 2006
Location: /*Egypt */ //cairo
Distribution: Ubuntu 7.04 , SLED 10 , Fedora , RHEL 5
Posts: 312

Original Poster
Rep: Reputation: 32
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 .
 
Old 03-25-2008, 10:17 AM   #5
adam_blackice
Member
 
Registered: Apr 2006
Location: /*Egypt */ //cairo
Distribution: Ubuntu 7.04 , SLED 10 , Fedora , RHEL 5
Posts: 312

Original Poster
Rep: Reputation: 32
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
 
Old 03-25-2008, 10:35 AM   #6
adam_blackice
Member
 
Registered: Apr 2006
Location: /*Egypt */ //cairo
Distribution: Ubuntu 7.04 , SLED 10 , Fedora , RHEL 5
Posts: 312

Original Poster
Rep: Reputation: 32
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
 
  


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
Entering folders recursively to run script on files nested inside, ubuntu 7.10 aidansmoker Linux - Newbie 4 11-12-2007 12:39 AM
Script to compare numbers inside two text files bugg_deccan Programming 3 10-17-2007 10:53 PM
Getting a list of directories with certain files inside... Banacek Linux - Newbie 7 11-27-2006 06:43 PM
Search for text inside files alaios Linux - Newbie 7 03-12-2006 10:20 AM
Running FTP inside Shell Script truncates files jbhanc0125 Red Hat 3 01-16-2004 12:37 PM


All times are GMT -5. The time now is 08:39 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration