Idiomatic way to test if no positional params are given?

后端 未结 5 817
暖寄归人
暖寄归人 2021-01-13 10:03

What is the most idiomatic way in Bash to test if no positional parameters are given? There are so many ways to check this, I wonder if there is one preferred way.

S

相关标签:
5条回答
  • 2021-01-13 10:18

    For me, the classical way is:

    [[ $# -eq 0 ]]
    
    0 讨论(0)
  • 2021-01-13 10:33

    Use Sensible Semantics

    The key is readability and the intent of your code. Unless you have a good reason to do otherwise, you probably just want to determine the length of the parameter list.

    # Is length of parameter list equal to zero?
    [ $# -eq 0 ]
    

    However, you can certainly use any parameter expansion or comparison operator that expresses the intent of your code. There's no right way to do it, but you certainly may wish to consider whether the semantics of your test are portable.

    Food for Thought

    It isn't the conditional expression that's intrinsically important. What's important is why you want to know. For example:

    set -e
    foo="$1"
    shift
    
    # $2 is now $1 (but only if the line is reached)
    echo "$1"
    

    In this case, the length of the parameter list is never checked directly. The first parameter is simply assigned (even though it may be unset), and then the shell throws an error and exits when you try to shift the parameter list. This code says "I just expect the parameters to be there; I shouldn't have to check for them explicitly."

    The point here is that you need to determine what your code is trying to express, and match the semantics of your tests and conditionals to express that as clearly as you can. There really is no orthogonal answer.

    0 讨论(0)
  • 2021-01-13 10:35

    If you want it to be an error to have no positional parameters:

    : ${@?no positional parameters}
    

    will print "no positional parameters" to standard error (and exit a non-interactive shell) if $@ is unset.

    Otherwise, I'm not aware of any better options than one of the various methods of checking if $# is 0.

    0 讨论(0)
  • 2021-01-13 10:36

    Here's a most logical way:

    [ ! $@ ]
    

    It is based on a single rule:

    [ ] # this returns 1
    

    Well then,

    [ ! ] # this returns 0
    

    The rest is obvious: $@ is the special parameter that expands to a list of all positional parameters.

    Test: (It will work even if you throw a couple of empty strings ("" "" "") at it.)

    if [ ! $@ ]; then
        printf 'Note: No arguments are given.'
    fi
    
    0 讨论(0)
  • 2021-01-13 10:40

    I prefer using the fact that if there are no positional parameters, there is also no first parameter:

    [[ -z $1 ]]
    test -z "$1"
    [ -z "$1" ]
    

    It's just a tiny bit lighter on the reader. Of course it only works when the assumption that the first parameter can't be an empty string is true.

    0 讨论(0)
提交回复
热议问题