csh alias with variable number of arguments

后端 未结 3 424
孤街浪徒
孤街浪徒 2021-01-28 13:58

I would like to create a csh alias that performs one operation if invoked without arguments and a second operation if invoked with a single argument. Does anyone know how to do

3条回答
  •  盖世英雄少女心
    2021-01-28 14:58

    Aliases in tcsh are limited; for more advanced things, I've found that the best way is to source a (t)csh script, like so:

    alias my-cmd 'source ~/.tcsh/my-cmd.tcsh'
    

    And ~/.tcsh/my-cmd.tcsh would contain something like:

    if ( $1 != '' ) then
            echo "we have an argument: $1"
    else
            echo "we don't have an argument"
    endif
    

    Example output:

    % my-cmd
    we don't have an argument
    % my-cmd hello
    we have an argument: hello
    

    Now, it may also be possible to do this with just an alias, but this will be much more maintainable & cleaner in the long run, IMHO.

    (I've assumed tcsh here since almost all, or perhaps even all, c shells are tcsh these days).

提交回复
热议问题