Idiomatic way to test if no positional params are

2019-02-24 05:25发布

问题:

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.

Some ways are:

((! $# )) # check if $# is 'not true'
(($# == 0)) # $# is 0
[[ ! $@ ]] # $@ is unset or null

回答1:

For me, the classical way is:

[[ $# -eq 0 ]]


回答2:

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.



回答3:

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.



回答4:

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


回答5:

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.