LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   PIPE Symbol not recognized in Script (https://www.linuxquestions.org/questions/linux-newbie-8/pipe-symbol-not-recognized-in-script-896399/)

anishkumarv 08-09-2011 12:47 PM

PIPE Symbol not recognized in Script
 
Hi this is the simple backup script which i used to take backup the databases of my clients

Code:

#!/bin/bash
date=`date +%d-%m-%Y_%H.%M.%S`
echo Backup Started $date  >> /var/tmp/anishdatabackuplog/anishdatabasesbackup$date.log
###########################################################
#create directory to store mysql database backup
mkdir /var/tmp/anishdatabasesbackup$date
if [ $? -ne 0 ]
then echo "Backup failed with errors" for $date >> /var/tmp/anishdatabackuplog/anishdatabasesbackup$date.log
exit
else
echo “directory mysqldatabasebackup$date created”  >> /var/tmp/anishdatabackuplog/anishdatabasesbackup$date.log
fi
# Back up anish.com Databases
###########################################################
db = $MYSQL > /var/tmp/db
for db in $(mysql -u admin -pPASSWORD -D psa -e "select name from data_bases where dom_id=(SELECT id FROM domains where name='anish.info');" | sed '1d' > /var/tmp/db)
do
      mysqldump -u admin -pPASSWORD  $db >> /var/tmp/anishdatabasesbackup$date/$db.sql
      echo DUMPING DATABASE ""$db"" >> /var/tmp/anishdatabackuplog/anishdatabasesbackup$date.log
      echo backup copy of $db is moved to /var/tmp/anishdatabasesbackup$date >> /var/tmp/anishdatabackuplog/anishdatabasesbackup$date.log
        cd  /var/tmp/anishdatabasesbackup$date
        a=`du -sh $db.sql | cut -f1`
        echo THE SIZE OF $db.sql FILE IS $a >> /var/tmp/anishdatabackuplog/anishdatabasesbackup$date.log
done
echo "Backup completed successfully $date" >> /var/tmp/anishdatabackuplog/anishdatabasesbackup$date.log

cat /var/tmp/anishdatabackuplog/anishdatabasesbackup$date.log | mail -s "anishdatabasesbackup" 07anis@gmail.com


Now my requirement is from mysqldb i need to get some values from that 2 values(databases) that only i need to dump.

From Db to get the values i use this command in script.

Code:

mysql -u admin -pPASSWORD -D psa -e "select name from data_bases where dom_id=(SELECT id FROM domains where name='anish.info');"
Output of this command is like this but i need only values db_trace and db_morrilton

Quote:

+-----------------+
| name |
+-----------------+
| db_trace |
| db_morrilton |
+-----------------+

so i add sed in this mysql command to delete the first line alone,


Code:

mysql -u admin -pPASSWORD -D psa -e "select name from data_bases where dom_id=(SELECT id FROM domains where name='anish.info');" | sed '1d' > /var/tmp/db
It give the exact output what i expect but in script the pipe symbol | not recognized so the script is not working, Don't know what mistake exactly i am doing, kindly look this script and guide me to solve this thread.

shamgar03 08-09-2011 01:13 PM

stdout
 
Is the output of the mysql query going to stdout or stderr? If it only goes to stdout, all that is being redirected to /var/tmp/db isn't it? Maybe remove the redirect "> /var/tmp/db"

anishkumarv 08-09-2011 01:25 PM

Hi dude,

I need to store that values in file only, from that file only i need to get the values man and that value only i need to dump.

shamgar03 08-09-2011 01:44 PM

But you make no further references to /var/tmp/db or $MYSQL and each pass through the loop will overwrite /var/tmp/db. The for loop is already going to capture the output from the mysql query, there is no need to write it to a file.

anishkumarv 08-09-2011 01:57 PM

Hi man,

If i did that like means the scrip considered

Quote:

+-----------------+
| name |
+-----------------+

This also a DB man.

Tinkster 08-09-2011 02:00 PM

Quote:

Originally Posted by anishkumarv (Post 4437968)
Hi this is the simple backup script which i used to take backup the databases of my clients

Code:

#!/bin/bash
date=`date +%d-%m-%Y_%H.%M.%S`
echo Backup Started $date  >> /var/tmp/anishdatabackuplog/anishdatabasesbackup$date.log
###########################################################
#create directory to store mysql database backup
mkdir /var/tmp/anishdatabasesbackup$date
if [ $? -ne 0 ]
then echo "Backup failed with errors" for $date >> /var/tmp/anishdatabackuplog/anishdatabasesbackup$date.log
exit
else
echo “directory mysqldatabasebackup$date created”  >> /var/tmp/anishdatabackuplog/anishdatabasesbackup$date.log
fi
# Back up anish.com Databases
###########################################################
db = $MYSQL > /var/tmp/db
for db in $(mysql -u admin -pPASSWORD -D psa -e "select name from data_bases where dom_id=(SELECT id FROM domains where name='anish.info');" | sed '1d' > /var/tmp/db)
do
      mysqldump -u admin -pPASSWORD  $db >> /var/tmp/anishdatabasesbackup$date/$db.sql
      echo DUMPING DATABASE ""$db"" >> /var/tmp/anishdatabackuplog/anishdatabasesbackup$date.log
      echo backup copy of $db is moved to /var/tmp/anishdatabasesbackup$date >> /var/tmp/anishdatabackuplog/anishdatabasesbackup$date.log
        cd  /var/tmp/anishdatabasesbackup$date
        a=`du -sh $db.sql | cut -f1`
        echo THE SIZE OF $db.sql FILE IS $a >> /var/tmp/anishdatabackuplog/anishdatabasesbackup$date.log
done
echo "Backup completed successfully $date" >> /var/tmp/anishdatabackuplog/anishdatabasesbackup$date.log

cat /var/tmp/anishdatabackuplog/anishdatabasesbackup$date.log | mail -s "anishdatabasesbackup" 07anis@gmail.com


Now my requirement is from mysqldb i need to get some values from that 2 values(databases) that only i need to dump.

From Db to get the values i use this command in script.

Code:

mysql -u admin -pPASSWORD -D psa -e "select name from data_bases where dom_id=(SELECT id FROM domains where name='anish.info');"
Output of this command is like this but i need only values db_trace and db_morrilton




so i add sed in this mysql command to delete the first line alone,


Code:

mysql -u admin -pPASSWORD -D psa -e "select name from data_bases where dom_id=(SELECT id FROM domains where name='anish.info');" | sed '1d' > /var/tmp/db
It give the exact output what i expect but in script the pipe symbol | not recognized so the script is not working, Don't know what mistake exactly i am doing, kindly look this script and guide me to solve this thread.


Why use sed in a roundabout way if you can simply use
-s -r switches to MySQL to suppress lines and headers?




Cheers,
Tink

anishkumarv 08-09-2011 02:24 PM

Hi all,

Thanks a lot Guys Atlast I found the Easiest way

instead of -e i use -Bse That skips the Table header. and its working fine.

mysql -u admin -pPASSWORD -D psa -Bse "select name from data_bases where dom_id=(SELECT id FROM domains where name='anish.info');"


All times are GMT -5. The time now is 12:32 AM.