Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Display week numbers on 'cal' calendars through 'ncal' UNIX
According to man cal, there's also an ncal command with extra options, such as displaying week numbers via the -w flag. But no ncal is installed in OS X. A little experiment reveals, however, that it's one and the same program -- it apparently behaves differently when named differently.

If you create an ncal link to the cal utility, you get a fully functional ncal program; note the week numbers below the calendar:
$ mkdir -p $HOME/bin
$ ln `which cal` $HOME/bin/ncal
$ $HOME/bin/ncal -w
    May 2008
Mo     5 12 19 26
Tu     6 13 20 27
We     7 14 21 28
Th  1  8 15 22 29
Fr  2  9 16 23 30
Sa  3 10 17 24 31
Su  4 11 18 25
   18 19 20 21 22
[robg adds: This works as described; you can also now use ncal -e or ncal -o to see the date for Easter (Western and Orthodox, respectively), -J for the Julian calendar, and -p to see the "country codes and switching days from Julian to Gregorian calendar as they are assumed by ncal." I tried creating an alias instead of a link, but that didn't work...can anyone explain why they chose to make cal and ncal work like this? Why not just give all the options to cal?]
  • Currently 2.29 / 5
  You rated: 4 / 5 (7 votes cast)

Display week numbers on 'cal' calendars through 'ncal' | 8 comments | Create New Account
Click here to return to the 'Display week numbers on 'cal' calendars through 'ncal'' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Display week numbers on 'cal' calendars through 'ncal'
Authored by: ctwise on May 07, '08 07:56:13AM

This is a common Unix practice, making a command work differently based on what the name is. This happens over time when two different commands that do almost the same thing gain widespread usage. Eventually someone decides to unify them but want old scripts to still work. They came up with a hack that lets one single program do everything but look like two entirely different programs. The poster child of this is the BusyBox utility which is used in embedded applications, e.g., a phone or router. One application acts like dozens of different utilities to save on space and memory. It's so easy and cost-free to do symbolic links in Unix that there's no real down side to the practice.

[ Reply to This | # ]
Display week numbers on 'cal' calendars through 'ncal'
Authored by: corienti on May 07, '08 09:09:52AM

ctwise is precisely correct.

In fact on Solaris, "cp" and "mv" are precisely the same binary file - not the same file on disk, but they are identical binary copies of the other. So if you happen to delete one accidentally, you can restore it by copying the other remaining one, to the name of the one you deleted.

As for why "ncal" doesn't work when you use an alias - this is because an alias is actually like a function or a script; it runs the binary for you, and the binary that gets run still has its executable/process name set as the name of the file on disk.
Alias "myaliastest" to "sleep" and run "myaliastest 60" and look in your process listing - you'll see that "sleep" is in the process listing, not "myaliastest".

And as for why using a softlink DOES work - well, a softlink DOES affect the process name. A softlink doesn't run a binary for you like an alias does... it IS the binary (well, it's a link to it, but for this purpose that's equivalent).
Make a softlink of "mylinktest" to "sleep" and do the same test - you'll see that "mylinktest" shows in the process list.

[ Reply to This | # ]
Display week numbers on 'cal' calendars through 'ncal'
Authored by: corienti on May 07, '08 09:15:40AM

Bah, sorry, small correction: in Solaris, "cp" and "mv" ARE the same binary file on disk; the directory entry for each are hard links (not soft links) to the same file on the disk.

[ Reply to This | # ]
Display week numbers on 'cal' calendars through 'ncal'
Authored by: Anonymous on May 08, '08 12:56:09PM

The major reason is for compatibility. Evidently, Apple didn't see a need for the full functionality of ncal, and only preinstalled it with its backward compatible name, "cal". The sillies.

You also see symlinks doing the same job, as delivered by the vendor. *EL4 has symlinks from /bin/egrep and /bin/fgrep to /bin/grep; in this case, the man page is the same too.

(How apt that "ln" was used in this hint -- "ln" is yet another hardlink to the inode referenced by "cp" and "mv". This is why the "source" and "destination" for "ln" seem to be the wrong way round.)

[ Reply to This | # ]
hard links to the same file, but with different names
Authored by: wireface on May 07, '08 09:23:21AM

Yes this is very common practice in unix. In fact on mac os x the saying "less is more" is in fact literally true, because both the command "less" and the command "more" are the same program.

ls -i /usr/bin/more /usr/bin/less
2555402 /usr/bin/less 2555402 /usr/bin/more

(this shows they have the same inode number and are thus both hard links to the same thing)

[ Reply to This | # ]
Is this a 10.5-only change?
Authored by: sweth on May 07, '08 11:52:03AM
It doesn't work for me on 10.4.11:
{8156}(sweth@newman)~$ lll =cal                                                           
-r-xr-xr-x    1 root   42832 Jan 13  2006 /usr/bin/cal
{8157}(sweth@newman)~$ lll =ncal                                                          
lrwxr-xr-x    1 root      12 May  7 14:43 /usr/local/bin/ncal -> /usr/bin/cal
{8158}(sweth@newman)~$ ncal -w                                                            
ncal: illegal option -- w
usage: cal [-jy] [[month] year]
{8159}(sweth@newman)~$ sw_vers | grep 'ProductVersion:' 
ProductVersion: 10.4.11

[ Reply to This | # ]
Is this a 10.5-only change?
Authored by: alextm on May 07, '08 05:27:14PM

I think so. this doesn't work on my 10.4.11 PPC but does on my 10.5.2 Intel

[ Reply to This | # ]
Is this a 10.5-only change?
Authored by: thinkyhead on May 07, '08 09:12:54PM
Have you tried a hard link? Maybe that will make a difference.
cd /usr/bin
sudo ln cal ncal

| slur was here

[ Reply to This | # ]