cmd line args (Was: Re: Converting DOS text files)

Paul Bijnens FFAAC09 at cc1.kuleuven.ac.be
Fri Oct 19 00:59:39 AEST 1990


>Here's yet another solution. This closely emulates Sun's dos2unix program.
> ...

The program is of course correct, but I have a comment about:

>               printf("\n\tUsage:  %s [infile [outfile]]\n\n", argv[0]);
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Why is it that Sun and others sometimes violate the simple rules
of program-design for use in a Unix shell environment?

One Simple Rule (i just invented it, and read it about 10 years ago :-) )
===> Do not specify output as simple file arguments!
It can be very surprising, doing something like this:

    dos2unix *.txt

If there are exactly 2 files, your second file (alfabetically) will
be overwritten.  This is counterintuitive to the general unix-usage.
If there are 3 files, the program will luckily give an error (not all
programs, behaving that way, give errors!).

There is a story, that in the early unix-days, when many programs
did not obey this rule, a simple housekeeping program, run by
cron (or something in those days) crippled all over the file systems
of Bell Labs.  It has since been avoided, but now and then it
slips back into some programs again.  On my system -- SYSV.3 --
"uniq" is also one of those bastards.  Guess how I found out...

A short list of commands with this confusing argument syntax:
    cpp (C preprocessor, designed to be used from "cc", but
        is frequently used as stand-alone)
    cprs (compress common object file)
    csplit (context split: the split args are after the file!)
    dcopy (well, I can live with this one...)
    diffmk (mark differences between files)
    ptx (permuted index)
    split (split a file into pieces)
    uniq  (report repeated lines in a file)

Would it be illogical to do:
   "split largefile1 largefile2"
         instead of "cat largefile1 largefile2 | split"
or
   "uniq *.out > single"  instead of  "cat *.out | uniq > single"
   or obscure: "cat *.out | uniq - single" (current syntax permits this)

There are several ways to avoid it:
-- write everything to standard output (and use the shell to redirect,
   only a novice makes this mistake once: "sort file > file")
-- specify the outputfile through an option: sort file -o file
   (sort even lets you put the output-option after the file args)
-- replace the current file with the output of the program (and
   do enough error-checking to avoid loss of important data)
-- give a new name to the output, which is related to the name
   of the input file ("cc -c prog.c" gives "prog.o",
   "compress *" gives the .Z-files).
-- leave a backup of the original file, and name the output
   like the input (like "perl -i.bak file")

Plenty of ways to rewrite dos2unix...
--
Polleke
ffaac09 at cc1.kuleuven.ac.be
FFAAC09 at BLEKUL11.BITNET



More information about the Comp.unix.sysv386 mailing list