Commands with options: (was: war story: parallel(1) command)

Chris F.A. Johnson chris-E7bvbYbpR6jSUeElwK9/Pw at public.gmane.org
Sun Jul 28 21:28:28 UTC 2013


On Sun, 28 Jul 2013, phiscock-g851W1bGYuGnS0EtXVNi6w at public.gmane.org wrote:

>
>> I hate it that there are so many flags on so many commands.
>> <http://harmful.cat-v.org/cat-v/>
>
> Interesting read. Unfortunately, the concept of 'lots of small tools that
> do one job well' instead of 'zillions of options' leads to two
> alternatives:
>
> 1. Concatenate a series of small programs or shell script symbols to do
> the job you want, or
>
> 2. Hide that construction in an alias.

    Or in a function, or in a script.

> The disadvantage of 1 is that the incantation is often hard to remember.

    If you want to different things, you are going to have to remember
    different incantations.

> My (un)favourite example: the ls command has no option to display only
> directories. Yes, there are alternatives, but should you have to commit
> something like this to memory: 'ls -p |grep /' ? Surely something like ls
> -D would be easier to remember.
>
> How about ls -d */   ?

    Or: printf "%s\n" */

    The -d option to ls tells it to list the directories themselves,
    not their contents, when given as arguments on the comand line.

    I use this function:

ld()
{
     ld_dir=$1
     set -- ${1:+"$1/"}*/
     [ -d "${1:-.}" ] || return 5
     ls -ld ${ld_dir:+$ld_dir/}*/
} 2> /dev/null

   Or:

l() { lsc "$@" | less; }

$ l -d */
drwxr-xr-x  2 chris chris     4,096  28-Jul-2013 15:06:36  about/
drwxr--r-x  2 chris chris     4,096  28-Jul-2013 03:25:36  images/
drwxr--r-x  2 chris chris     4,096  28-Jul-2013 03:25:36  old/
drwxr-xr-x  2 chris chris     4,096  28-Jul-2013 03:25:36  privacy/
drwxr--r-x  2 chris chris   249,856  28-Jul-2013 03:25:36  quotations/
drwxr--r-x  2 chris chris     4,096  28-Jul-2013 03:25:36  xcf/

> That's much easier to remember, but my understanding is that only works in
> bash.

   It works in any shell except a very old Bourne shell.

> The disadvantage of 2 is that aliases are non-standard. If you re-install
> the system, you have to re-install all your aliases. If they are commands,
> they should be included in the operating system. Otherwise, no one else
> (human or machine) recognizes your aliases.

    As the bash man page says:

       For almost every purpose, aliases are superseded by shell
       functions.

> Incidentally, it is interesting to see that the man page for cat actually
> has examples, something that many man pages could benefit from.
> Unfortunately, the cat command has accumulated 10 options, only two of
> which have examples.

    That's only the GNU version of cat; the POSIX spec gives only one
    option: -u

> My solution to this is a correction of hints, which contain these
> commands. Many have been contributed by members of TLUG, thank-you. But
> that requires looking them up each time.
>
> Incidentally, for me the most useable solution for identifying directories
> has been
> ls -colour | more.
> The directories are a different colour. However, I suspect that's not
> totally portable either.

    --colour is a GNU option.

-- 
    Chris F.A. Johnson, <http://cfajohnson.com/>
    Author:
    Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
    Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list