Re: What does :$(cal) do?
From: Noel R. Nihill (nnihil01_at_Motorola.BLAH.com)
Date: 06/19/03
- Next message: David Thompson: "Re: $_ in ksh (Was: Re: What does :$(cal) do?)"
- Previous message: David Thompson: "Re: bash: coprocesses?"
- In reply to: Paul: "What does :$(cal) do?"
- Next in thread: Richard L. Hamilton: "Re: What does :$(cal) do?"
- Reply: Richard L. Hamilton: "Re: What does :$(cal) do?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: Thu, 19 Jun 2003 16:18:16 +0100
"Paul" <f1fteen@hotmail.com> wrote in message
news:7b7286ec.0306190622.27eb5926@posting.google.com...
> I have written the following script to calculate the number of days
> left until the end of the current month.
>
> #!/bin/ksh
>
> :$(cal)
> last_day=$_
> this_day=`date +%d`
> days_left=`expr $last_day - $this_day`
>
> I copied the :$(cal) bit from another script I found on the net.
>
> I was wondering if anyone can explain in general terms what this type
> of command/technique is called, and also what it is doing.
':' expands to '/bin/true' in ksh, and $(cal) is the same as `cal`,
i.e. 'replace cal with cal's stdout'. So what you have is:
/bin/true June 2003 S M Tu W Th F S 1 2 3 4 5...<snip>...30
And of course '$_' expands as the last parameter given to the last
executed command, which in this case will be the date of the last
day of the current month.
Note that if cal did not output a tab first of all, this would break with
an error like 'ksh: :June: not found'. Observe:
# :$(cat /etc/system)
ksh: :*ident: not found
> Also, is there any other common usage for this technique.
I don't know, mind you.
> I know it works OK, I am just interested in the theory.
>
> Thanks in advance.
>
> Paul.
-- Noel R. Nihill UNIX® platform development Motorola NSS I *could* be arguing in my spare time.
- Next message: David Thompson: "Re: $_ in ksh (Was: Re: What does :$(cal) do?)"
- Previous message: David Thompson: "Re: bash: coprocesses?"
- In reply to: Paul: "What does :$(cal) do?"
- Next in thread: Richard L. Hamilton: "Re: What does :$(cal) do?"
- Reply: Richard L. Hamilton: "Re: What does :$(cal) do?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|