LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 01-11-2005, 08:36 AM   #1
rstoutmna
LQ Newbie
 
Registered: Dec 2004
Posts: 4

Rep: Reputation: 0
bash returns ambiguous redirect with date in filename


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.
 
Old 01-11-2005, 08:54 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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
 
Old 01-11-2005, 10:11 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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.
 
Old 01-11-2005, 02:05 PM   #4
rstoutmna
LQ Newbie
 
Registered: Dec 2004
Posts: 4

Original Poster
Rep: Reputation: 0
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
 
Old 01-12-2005, 04:50 AM   #5
kees-jan
Member
 
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273

Rep: Reputation: 30
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
 
Old 09-13-2011, 07:37 PM   #6
rjmx
LQ Newbie
 
Registered: Oct 2007
Location: Brockton, Massachusetts
Distribution: Debian
Posts: 4

Rep: Reputation: 0
I know it's been a long time, but the current version of GNU date (date (GNU coreutils) 8.5) allows the use of '%H' to give a two-digit hour (with leading zeroes if necessary). Replacing '%k' in the original poster's code with '%H' should fix the problem.
Don't know if %H was available back in 2005, though.
 
  


Reply



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
logrotate with date in filename ptp Linux - Newbie 1 04-14-2011 10:55 AM
using bash in cron to get the date in filename rstoutmna Programming 1 12-29-2004 02:39 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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:19 AM.

Main Menu
Advertisement
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
Twitter: @linuxquestions
Open Source Consulting | Domain Registration