I've got a question. I'm doing a simple script right now and it isn't working like I want.


    cat /etc/backup.conf | egrep -v "(^#.*|^$|^--.*)" > /tmp/databases

    while read line; do
    pt-table-checksum [options] --databases $line --quiet
    done < /tmp/databases

    rm /tmp/databases

As you see I put names of databases to file /tmp/databases. Then I want to use pt-table-checksum for every database in that file line by line. This script works but only for first database name in file. Then it stops.

Any ideas?

  • This question is more bash related than mysql related. Can you show an example of /etc/backup.conf?
    – Giovanni
    May 29, 2015 at 12:43
  • It doesn't really matter what is in backup.conf because my cat command works fine. In /tmp/databases I have someone like this: database1 database2 database3 ... and I what to do pt-table-checksum command for every database line by line.
    – zieloneoko
    May 29, 2015 at 12:57
  • I know it sounds silly. Could you replace pt-table-checksum ... with a simple echo $line and run the script?
    – Giovanni
    May 29, 2015 at 13:13
  • I did like you say and script works fine. I get my databases names line by line: mysql phpMyAdmin jabber dev_intranet dev_extranet ...
    – zieloneoko
    May 29, 2015 at 13:19

1 Answer 1


I would use a 'for' loop for this, which I have found easier to use for things like this. Often, either line break, spaces, or combinations of those can cause issues with your method. Also, remove the --quiet from your command line to see the errors that pt-table-checksum may be reporting.


for db in `cat /etc/backup.conf | egrep -v "(^#.*|^$|^--.*)"`
    pt-table-checksum [options] --databases $db --quiet

(Hope this reads ok, have answered on my phone!)

  • Thanks for your answer but situation is similiar to while loop. There are no errors, script just do first loop with first database and then stops.
    – zieloneoko
    Jun 1, 2015 at 13:35
  • What you may find is that the DB list is presented as "db1 db2 db3" as a single line through the read line statement, and doesn't get split at the spaces, meaning the loop only runs once?
    – Dave Rix
    Jun 1, 2015 at 19:50
  • Is your DB list file set up with each database on a separate line, or with all databases on a single line?
    – Dave Rix
    Jun 1, 2015 at 19:52
  • When I do only cat command, DB list is presented as "db1 db2 db3" - each database on a separate line. I checked and there aren't any spaces at the beginning or at the end of each database name. It should be so simple but I have no idea why it doesn't work...
    – zieloneoko
    Jun 2, 2015 at 9:29
  • Check the file with od -c filename to see if there are any odd characters at the ends of the lines, indicating that they aren't separated with a line-feed. Can you cat the file and send a screenshot as an image perhaps?
    – Dave Rix
    Jun 2, 2015 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.