Find the answer to your Linux question:
Results 1 to 6 of 6
Hello, I have defined command in script to detect Day, but it doesn't properly. Here is script Code: cd /home/username/`date | awk '{print $1}'`; /usr/bin/mysqldump --user=mysqluser --password=password --databases databasename database ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2010
    Posts
    7

    Command problem


    Hello,

    I have defined command in script to detect Day, but it doesn't properly.

    Here is script
    Code:
    cd /home/username/`date | awk '{print $1}'`;
    /usr/bin/mysqldump --user=mysqluser --password=password --databases databasename database > database.sql
    Output
    Code:
    root[~]#sh -x sql-backup-daily-new.sh
    ++ date
    ++ awk '{print $1}'
    + cd $'/home/username/Fri\r'
    : No such file or directoryne 2: cd: /home/username/Fri
    PS :: Folder /home/username/Fri is available on the system
    Please help why script is taking day as 'Fri\r' instead of 'Fri'

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,218
    Quote Originally Posted by eric85 View Post

    Please help why script is taking day as 'Fri\r' instead of 'Fri'
    The awk print command always ends its output with the output record separator (ORS). By default, this is newline. You have more control over your output if you use printf.
    "I'm just a little old lady; don't try to dazzle me with jargon!"
    www.hrussman.entadsl.com

  3. #3
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,049
    You may want to try `date +%a`

    See `man date` for more options
    Can't tell an OS by it's GUI

  4. #4
    Just Joined!
    Join Date
    Apr 2010
    Posts
    7
    Hello,

    Thank you for the reply..
    The awk print command always ends its output with the output record separator (ORS). By default, this is newline. You have more control over your output if you use printf.
    Used printf but doesn't worked.
    cat sql-backup-daily-new.sh
    cd /home/username/`date | awk '{printf $1}'`;
    /usr/bin/mysqldump --user=mysqluser --password=password --databases databasename database > database.sql

    You may want to try `date +%a`

    See `man date` for more options
    date +%a is also not worked.
    cd /home/holidayg/mysqlbackup/`date +%a`
    /usr/bin/mysqldump --user=mysqluser --password=password --databases databasename database > database.sql

  5. #5
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,049
    Quote Originally Posted by eric85
    date +%a is also not worked.
    cd /home/holidayg/mysqlbackup/`date +%a`
    /usr/bin/mysqldump --user=mysqluser --password=password --databases databasename database > database.sql
    Then what exactly didn't work?

    When I try:
    Code:
    mkdir ~/test/`date +%a`
    cd ~/test/`date +%a`
    # and even further
    echo haha>hihi
    cat ~/test/`date +%a`/hihi
    # it outputs 'haha'
    It works

    Can you change your code to:
    Code:
    if [ -d /home/holidayg/mysqlbackup/`date +%a` ] ; then
         /usr/bin/mysqldump \
              --user=mysqluser \
              --password=password \
              --databases databasename database > \
         /home/holidayg/mysqlbackup/`date +%a`/database.sql
    else
         echo "Target directory /home/holidayg/mysqlbackup/`date +%a` does not exist"
    fi
    Can't tell an OS by it's GUI

  6. #6
    Just Joined!
    Join Date
    Apr 2010
    Posts
    7
    if [ -d /home/holidayg/mysqlbackup/`date +%a` ] ; then
    /usr/bin/mysqldump \
    --user=mysqluser \
    --password=password \
    --databases databasename database > \
    /home/holidayg/mysqlbackup/`date +%a`/database.sql
    else
    echo "Target directory /home/holidayg/mysqlbackup/`date +%a` does not exist"
    fi
    Thank you for your help. it worked

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •