Paolo De Michele
2014-May-25 10:08 UTC
[CentOS] bash script reading directory with while and do
hi everyone, I'm searching to do a functionally bash script for copy many file inside in a directory the scenario is: root path= /var/www/clients/ inside of this there are many subdirectories with this name: client1 to x (depends of the customer) it happens that a customer not renew the contract and I would stopping to do a backup directory for him. this operation should be versatile and very easy so, I don't understand the error in this script. look below: !/bin/bash while read line; do read NR PATH NAME<<<$(IFS=" "; echo $line) cd /var/www/clients/ cp -R $PATH /backup/temp/www/ cd /backup/temp/www/ tar cfz `date +%F`_$NAME.tar.gz web$NR/ mv *.tar.gz /backup/www/test/ rm -rf /backup/temp/www/* done < sites.txt anyone can help me, please? thanks in advance regards
Alberto Varesio
2014-May-26 09:12 UTC
[CentOS] bash script reading directory with while and do
Hi you are overwriting the PATH env variable. change PATH to CLIPATH and all will work On Sun, May 25, 2014 at 12:08 PM, Paolo De Michele <paolo at paolodemichele.it>wrote:> hi everyone, > > I'm searching to do a functionally bash script for copy many file inside > in a directory > the scenario is: > > root path= /var/www/clients/ > inside of this there are many subdirectories with this name: client1 to > x (depends of the customer) > it happens that a customer not renew the contract and I would stopping > to do a backup directory for him. > > this operation should be versatile and very easy > so, I don't understand the error in this script. look below: > > !/bin/bash > > while read line; > do > read NR PATH NAME<<<$(IFS=" "; echo $line) > > cd /var/www/clients/ > cp -R $PATH /backup/temp/www/ > cd /backup/temp/www/ > > tar cfz `date +%F`_$NAME.tar.gz web$NR/ > mv *.tar.gz /backup/www/test/ > rm -rf /backup/temp/www/* > > done < sites.txt > > anyone can help me, please? > thanks in advance > > regards > _______________________________________________ > CentOS mailing list > CentOS at centos.org > http://lists.centos.org/mailman/listinfo/centos >-- Alberto 'JCN-9000' Varesio *IT Administrator* Mobile: +39 393 101 8844 LI: http://www.linkedin.com/in/albertovaresio
Elias Persson
2014-May-28 12:10 UTC
[CentOS] bash script reading directory with while and do
On 2014-05-25 12:08, Paolo De Michele wrote:> !/bin/bash > > while read line; > do > read NR PATH NAME<<<$(IFS=" "; echo $line) > > cd /var/www/clients/ > cp -R $PATH /backup/temp/www/ > cd /backup/temp/www/ > > tar cfz `date +%F`_$NAME.tar.gz web$NR/ > mv *.tar.gz /backup/www/test/ > rm -rf /backup/temp/www/* > > done < sites.txt A few things: 1. A shebang is two chars, "#!", not just an exclamation point. (possibly just a typo / copy error) 2. $PATH is special, use something else (e.g. $path). 3. Your `tar` stanza is wrong (if it's not obvious to you why that is, now is a good time to make a habit of using long options (e.g. `--gzip`) whenever possible). 4. Always quote variables that could possibly have whitespace in them ($NAME in this case). What is it you're trying to accomplish with the IFS and echo? Why not just while read nr path name; do : stuff goes here done