LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have access to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

Are you new to LinuxQuestions.org? Visit the following links:
Site Howto | Site FAQ | Sitemap | Register Now

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Reply
 
Thread Tools
Old 01-11-2005, 08:36 AM   #1
rstoutmna
Newbie
 
Registered: Dec 2004
Posts: 4
bash returns ambiguous redirect with date in filename


[Log in to get rid of this advertisement]
Hi I was wondering if anyone knew why in bash when I use cron to do a mysqldump and include the date in the filename I get an error on all times between midnight and 10AM

Here is my script:

#!/bin/bash

mysqldump --all-databases > /mnt/serv/mysqldump_$(date '+%Y-%m-%d-%k-%M').sql
mysqldump --all-databases > /home/nsuser/mysqlbackup/mysqldump_$(date '+%Y-%m-%d-%k-%M').sql


The email response sent to my root account is below

/etc/cron.hourly/mysqldump:

/etc/cron.hourly/mysqldump: line 3: /mnt/serv/mysqldump_$(date '+%Y-%m-%d-%k-%M').sql: ambiguous redirect

/etc/cron.hourly/mysqldump: line 4: /home/nsuser/mysqlbackup/mysqldump_$(date '+%Y-%m-%d-%k-%M').sql: ambiguous redirect

And this works without a problem between 10AM and 11pm.
Any input is appreciated.
rstoutmna is offline     Reply With Quote
Old 01-11-2005, 08:54 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,045
Maybe you would have better luck creating the filename first, then calling that variable later in the script. Like this : filename=`date '+%m%d%y'`
Note: those are backticks ( ` ) and single quotes ( ' )

Here is an example
Code:
#!/bin/bash
filename=`date '+%m%d%y'`
/bin/tar -cvzf /mnt/backup/${filename}.tar.gz . \
> /mnt/backup/${filename}.log \
2> /mnt/backup/${filename}err.log
homey is offline     Reply With Quote
Old 01-11-2005, 10:11 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Solaris 10, FreeBSD Ubuntu Puppy,
Posts: 2,412
clue:
midnight = 00
ten = 10

1 am = 1

you are probably getting spaces for the single digits in the filename.
like
Code:
mysqldump --all-databases > /home/nsuser/mysqlbackup/mysqldump_2005-01- 1.sql
homey is right, construct the file name first.

if there are spaces you will need to get rid of them
as they'll mess up your 'sort' of course.

you can use typeset (in ksh maybe bash?) to pad them with zeroes

e.g:
Code:
>typeset -Z5 num
>num=3          
>echo $num      
00003

hint: don't hardcode filenames and multiply redirect.
redirect once.

either use

Code:
# all lines after this default to the output file
exec  2>&1 > $OUTFILE_NAME
or put them in a function and redirect the function like

Code:
# function definition
do_it()
{
   blah
   blah/blah
   echo done
}

do_it 2>&1 > $OUTFILE
never hardcode file names or such like. Use a variable that you can change in one place.
bigearsbilly is offline     Reply With Quote
Old 01-11-2005, 02:05 PM   #4
rstoutmna
Newbie
 
Registered: Dec 2004
Posts: 4
I changed my script and am going to check on it tomorrow. I am pretty sure both of you are correct. Thanks so much for your help I will post here tomorrow whether it did in fact work. --Thanks again
rstoutmna is offline     Reply With Quote
Old 01-12-2005, 04:50 AM   #5
kees-jan
Member
 
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273
If you don't want to wait till tomorrow to do small tests, you can locally modify your timezone in the TZ environment variable to obtain different times. For example:
Code:
$ env TZ=GMT+5 date '+%Y-%m-%d-%k-%M'
2005-01-12- 5-47
There is indeed a space in the name you are using.

Groetjes,

Kees-Jan
kees-jan is offline     Reply With Quote

Reply

Submit thread to Digg | Submit thread to del.icio.us | Submit to LQ Bookmarks

« Back to Top »

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
using bash in cron to get the date in filename rstoutmna Programming 1 12-29-2004 02:39 PM
logrotate with date in filename ptp Linux - Newbie 0 02-21-2004 02:49 PM
Ambiguous output redirect XFox Linux - Newbie 5 10-17-2003 07:29 PM
Ambiguous Redirect Error AMMullan Linux - General 1 10-01-2003 08:39 PM
ambiguous redirect? cuss Linux - General 4 03-06-2003 10:58 AM



Add LQ To Your Yahoo Add LQ To Your Google Add LQ To Your MSN Add LQ To Your Blog
All times are GMT -5. The time now is 09:14 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Open Source Consulting | Domain Registration