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 06-11-2014, 10:56 AM   #1
lahirushanaka
Member
 
Registered: Oct 2012
Posts: 46

Rep: Reputation: Disabled
small bash script copy one file to list of directories on file


Hi i want write script to copy robots.txt file to all of the web_root directory this should be only file if not available on root directories.

there is more than 50 web_root directories.
and need to send a mail if not available ?

#!/bin/bash
file=/home/shanaka/robots.txt
for dir in /home/deploy/*/public_html
do
cp "$file" "$dir"
done

i have write above that, it is only can copy files to all directory, but not check whether available or not, sending mail, can you help me to do this ?
 
Old 06-11-2014, 11:08 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,051

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
maybe something like this:
Code:
#!/bin/bash
file=/home/shanaka/robots.txt
for dir in /home/deploy/*/public_html
do
 cp "$file" "$dir"
 if [ ! -e $dir/robots.txt ]
 then
  mail -s "404: file not found" lahirushanaka@server.com
 fi
done

Last edited by schneidz; 06-11-2014 at 12:02 PM. Reason: fixed typo
 
Old 06-11-2014, 11:34 AM   #3
lahirushanaka
Member
 
Registered: Oct 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
thanks friend it was really help,but i want to send mail if robots.txt file not available on location, which should be with include directory location which is not available. but now all the files are copping before check the file availability, therefore mail is not working.

i have edited your script. but it is not working giving error. can you help me on this ?


#!/bin/bash
file=/home/shanaka/robots.txt

for dir in /home/shanaka/test/*/public_html
if [ ! -e $dir/robots.txt ]
then
mail -s "404: file not found " lahirushana@gmail.com
do cp "$file" "$dir"
fi
done

Last edited by lahirushanaka; 06-11-2014 at 11:38 AM.
 
Old 06-11-2014, 11:37 AM   #4
potato_farmer
Member
 
Registered: May 2014
Posts: 55

Rep: Reputation: Disabled
You need the "do" after the for statement, not before the cp command.
 
2 members found this post helpful.
Old 06-11-2014, 11:43 AM   #5
lahirushanaka
Member
 
Registered: Oct 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
then it is not sending mail, bcz it is coping file before check the file availability
 
Old 06-12-2014, 02:35 AM   #6
lahirushanaka
Member
 
Registered: Oct 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
HI friends wrote something like below, thanks for helping me alot,


Quote:
#!/bin/bash

file=/home/shanaka/robots.txt
for dir in /home/shanaka/test1/*/public_html
do
if [ ! -e $dir/robots.txt ]
then
ls -l /home/shanaka/test1/*/public_html/ | mail -s "404: file not found" lahirushanaka@gmail.com
cp "$file" "$dir"

else
result=$(diff $file $dir )
if [ ! -e 0 ]
then
echo $dir is not same
cp "$file" "$dir"
fi
fi
done
but i need help again, if the one file is difference, it is show all the files as different, how i show only that file is different or only show the different files
 
Old 06-12-2014, 03:11 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,487

Rep: Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427
I do not really understand what do you need, but here are some tips:
Code:
filename=robots.txt
file=/home/shanaka/$filename
for dir in /home/shanaka/test1/*/public_html
do
    if [ ! -e $dir/robots.txt ]
    then
        ls -l /home/shanaka/test1/*/public_html/ | mail -s "404: file not found" lahirushanaka@gmail.com
        cp "$file" "$dir"  # you may try to use link (ln) instead of copy...

    else
        result=$(diff $file $dir/$file )  # probably?
#        if [ ! -e 0 ]     ### <<<< what is this ?
        if [ "$result" -gt 0 ]          # or what?
        then
             echo $dir is not same
             cp "$file" "$dir"
        fi
    fi
done
 
1 members found this post helpful.
Old 06-12-2014, 03:21 AM   #8
lahirushanaka
Member
 
Registered: Oct 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
I do not really understand what do you need, but here are some tips:
Code:
filename=robots.txt
file=/home/shanaka/$filename
for dir in /home/shanaka/test1/*/public_html
do
    if [ ! -e $dir/robots.txt ]
    then
        ls -l /home/shanaka/test1/*/public_html/ | mail -s "404: file not found" lahirushanaka@gmail.com
        cp "$file" "$dir"  # you may try to use link (ln) instead of copy...

    else
        result=$(diff $file $dir/$file )  # probably?
#        if [ ! -e 0 ]     ### <<<< what is this ?
        if [ "$result" -gt 0 ]          # or what?
        then
             echo $dir is not same
             cp "$file" "$dir"
        fi
    fi
done
thanks yo

thanks you very much friends,
what i realy want is check robots.txt file in all web root, if it is there then, theck whether it is disallow or not. if it is not, i need to copy robots.txt file to not available web directories.

this is my result for previous code.

/home/shanaka/test1/project1/public_html is not same
/home/shanaka/test1/project2/public_html is not same
/home/shanaka/test1/project3/public_html is not same
/home/shanaka/test1/project4/public_html is not same
/home/shanaka/test1/project5/public_html is not same

so here only "/home/shanaka/test1/project1/public_html is not same directory file only differet, but it is showing all the directory as wrong,

there for i want to grep different one only. and copy file to those directories.
 
Old 06-14-2014, 09:58 AM   #9
lahirushanaka
Member
 
Registered: Oct 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
Hi,

Im getting below error please help me

./robo.sh: line 12: [: : integer expression expected
./robo.sh: line 12: [: : integer expression expected
./robo.sh: line 12: [: : integer expression expected
./robo.sh: line 12: [: : integer expression expected
 
Old 06-14-2014, 10:55 AM   #10
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,051

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
$result is probably not a number (what are you expecting it to be ?).

please change the first line of your script to #!/bin/bash -x and show us the result.

Last edited by schneidz; 06-14-2014 at 10:56 AM.
 
1 members found this post helpful.
Old 06-14-2014, 12:00 PM   #11
lahirushanaka
Member
 
Registered: Oct 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
$result is probably not a number (what are you expecting it to be ?).

please change the first line of your script to #!/bin/bash -x and show us the result.
Hi friend thanks for the help, issue is resolve, but i dont have an idea how is working, i have publish my code in below, now only error directory will be show as show in echo as i want, can you explain how it is work and is this the right way to do this if you dont mind.

#!/bin/bash
file=/home/shanaka/robots.txt
for dir in /home/shanaka/test1/*/public_html
do
if [ ! -e $dir/robots.txt ]
then
ls -l /home/shanaka/test1/*/public_html/ | mail -s "404: file not found" lahirushanaka@gmail.com
cp "$file" "$dir"

else
result=$(diff $file $dir/robots.txt > /dev/null
if [ "$?" -ne 0 ]
then
echo $dir is not same
cp "$file" "$dir"
fi
fi
done

Last edited by lahirushanaka; 06-14-2014 at 12:02 PM.
 
  


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
[bash script] What does this mean? -- FILE=${LIST##*/} thomas2004ch Linux - Software 1 03-05-2012 08:21 AM
Little bash script and file to give long (or any) directories short names WardXmodem Programming 1 11-23-2009 01:24 AM
bash script to insert id3 tags from file and directories jason7 Linux - General 15 07-20-2009 06:10 AM
How to copy one file and to multiple directories using shell script? cebuy Programming 8 03-31-2009 08:04 PM
bash script to copy a file from the internet AgentD Programming 3 01-24-2009 12:29 AM


All times are GMT -5. The time now is 08:43 AM.

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