Re: What does :$(cal) do?

From: Noel R. Nihill (nnihil01_at_Motorola.BLAH.com)
Date: 06/19/03


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.


Relevant Pages

  • Re: VG mirroring
    ... Host level migration of EMC frames for HPUX. ... hpux.vgclean - Per volume group clean-up script. ... Initialize the tables by running "ksh hpux.start". ... Look at inq.*.txt to see what devices come from what frame. ...
    (comp.unix.admin)
  • Re: [Long] about ksh93 (Was: Bourne Shell Programming on Windows)
    ... :>: Are you saying that David Korn is an unitiated or ignorant ksh ... :> won't be called in a script. ... :> in every shell. ... that should be teached in every shell programming book. ...
    (comp.unix.shell)
  • Re: vi horizontal split screen
    ... I'm an old ksh hacker, but bash will do fine. ... I started on the SysV R2 Bourne shell, and used csh for a better ... Several ksh improvements were designed to make it faster and more efficient at processing scripts, and one way to do that was substitute built-ins for external commands called from the script. ...
    (comp.editors)
  • Re: A couple of scripting queries
    ... This gets you /usr/local/bin/date, which is gnu date. ... ksh and bash definitely do and probably other newer bourne shells like ... in the script, ... I don't think you actually need any arrays anyways. ...
    (comp.unix.sco.misc)
  • Re: Ksh output garbled/out-of-sync
    ... is starting to look like a problem withksh. ... affected systems and running the test script). ... It turned to be a bug in ksh. ...
    (comp.unix.solaris)