LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
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.

Notices

Reply
 
Thread Tools
Old 01-11-2005, 09:36 AM   #1
rstoutmna
LQ Newbie
 
Registered: Dec 2004
Posts: 4
Thanked: 0
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, 09:54 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,058
Thanked: 1
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, 11:11 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Puppy
Posts: 2,663
Thanked: 14
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, 03:05 PM   #4
rstoutmna
LQ Newbie
 
Registered: Dec 2004
Posts: 4
Thanked: 0

Original Poster
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, 05:50 AM   #5
kees-jan
Member
 
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273
Thanked: 0
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

Bookmarks


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

BB 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 03:39 PM
logrotate with date in filename ptp Linux - Newbie 0 02-21-2004 03:49 PM
Ambiguous output redirect XFox Linux - Newbie 5 10-17-2003 08:29 PM
Ambiguous Redirect Error AMMullan Linux - General 1 10-01-2003 09:39 PM
ambiguous redirect? cuss Linux - General 4 03-06-2003 11:58 AM


All times are GMT -5. The time now is 10:08 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
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration