Bash Script Reading Directory With While And Do

Home » CentOS » Bash Script Reading Directory With While And Do
CentOS 12 Comments

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

12 thoughts on - Bash Script Reading Directory With While And Do

  • Hi you are overwriting the PATH env variable. change PATH to CLIPATH and all will work

  • Hi,

    I don’t understand the procedure so, in theory the path should be /var/www/clients/client* (I have number from 1 to X)
    I replaced the PATH in read section

    It’s correct?

  • I don’t understand, too… do you mean the script should read

    !/bin/bash

    while read line;
    do
    read NR CLIPATH NAME<<<$(IFS=" "; echo $line) cd /var/www/clients/ cp -R $CLIPATH /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 instead? /i.e./ replace **all** instances of “PATH” with “CLIPATH” ?

  • Yes!

    PATH should be a reserved name, NOT to be used as a variable in scripts.

    btw, what errors do you get running the script ?

  • 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

  • Elias Persson wrote:
    Dumb question: why not while read line;
    do
    read NR PATH NAME<<<$(IFS=" "; echo $line) cd /var/www/clients/ tar -czf /backup/www/test/`date +%F`_$NAME.tar.gz web$NR/ done < sites.txt

    Why long?

    mark

  • Actually it is correct, aside from possible whitespace issues with $NAME. It is using an outdated syntax (short options all in a group _without_ a leading “-“), but doing it correctly with the needed option argument following the set of option letters.

  • I stand corrected. Apparently having the “f” in the middle is not a problem with the old syntax.




  • With long options one can’t stick something that takes an argument in the middle of a bunch of others (which was the error I thought was present here, and would have been the case if your example had the options in the same order). It’s also more readily apparent what the options do, though that’s not much of an issue for common things like tar or rm. I for one can never recall if it’s -c or -C that’s –reuse-message for git commit.

  • Elias Persson wrote:

    Everybody I know knows the short flags.

    However, the point I made in the code snippet was, why copy it to a temp directory, then tar and compress it, then copy it to another directory, then remove the copy in the temp directory, rather than just tar and compress it directly to the target directory? I *did* give the full path the the target, preceding the final filename.

    mark

  • It’s analogous to printf(), where a string with all the format controls comes first, followed by all of the needed arguments in the needed sequence. It causes all sorts of weirdness when you get things wrong, as when unquoted whitespace causes what should have been one argument to appear as two or more.