LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   small bash script copy one file to list of directories on file (https://www.linuxquestions.org/questions/linux-newbie-8/small-bash-script-copy-one-file-to-list-of-directories-on-file-4175507728/)

lahirushanaka 06-11-2014 09:56 AM

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 ?

schneidz 06-11-2014 10:08 AM

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


lahirushanaka 06-11-2014 10:34 AM

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

potato_farmer 06-11-2014 10:37 AM

You need the "do" after the for statement, not before the cp command.

lahirushanaka 06-11-2014 10:43 AM

then it is not sending mail, bcz it is coping file before check the file availability

lahirushanaka 06-12-2014 01:35 AM

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

pan64 06-12-2014 02:11 AM

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


lahirushanaka 06-12-2014 02:21 AM

Quote:

Originally Posted by pan64 (Post 5186819)
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.

lahirushanaka 06-14-2014 08:58 AM

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

schneidz 06-14-2014 09:55 AM

$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.

lahirushanaka 06-14-2014 11:00 AM

Quote:

Originally Posted by schneidz (Post 5188051)
$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


All times are GMT -5. The time now is 06:45 PM.