On Mon, 14 Nov 2011 06:13:34 AM Philip Webb wrote:
> 111114 Neil Bothwick wrote:
> > On Mon, 14 Nov 2011 02:06:04 -0500, Philip Webb wrote:
> >> To convert a UNIX date to a human-readable version the command is :
> >>   556: ~> date -d @1321251520
> >>   Mon Nov 14 01:18:40 EST 2011
> >> 
> >> I would like to create a Bash alias or function to do this,
> >> but can't get the Bash syntax right: it keeps telling me
> >> "date: the argument `1321251520' lacks a leading `+';
> > 
> > It is difficult to say what is wrong with your alias
> > as you haven't shown it
> 
>   alias th='date -d @$1'
> 
> was the first try, then adding '+' &/or '\' to escape '+' or '@'.
> I also tried a function along similar lines.
> 
> > but my guess is that is is introducing a space
> > between @ and the timestamp, which gives exactly the error you get.
> 
> No, no spaces.


Aliases don't take argument, you need a function for that.

Turning on shell debugging shows what is happening

$ set -x
+ set -x

$ alias th='date -d @$1'
+ alias 'th=date -d @$1'

$ th 1321251520
+ date -d @ 1321251520
date: the argument `1321251520' lacks a leading `+';
when using an option to specify date(s), any non-option
argument must be a format string beginning with `+'
Try `date --help' for more information.


      Now set a value to $1 and see what happenes...


$ set -- 'Ha Ha'
+ set -- 'Ha Ha'

$ th 1321251520
+ date -d @Ha Ha 1321251520
date: extra operand `1321251520'
Try `date --help' for more information.

$ set +x


All the 'alias' process does is simple text substitution.


-- 
Reverend Paul Colquhoun, ULC.    http://andor.dropbear.id.au/~paulcol
 Before you criticize someone, you should walk a mile in their shoes.
Then, when you do, you'll be a mile away, and you'll have their shoes.


Reply via email to