Idiomatic way to test if no positional params are

2019-02-24 05:43发布

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

5条回答
对你真心纯属浪费
2楼-- · 2019-02-24 05:50

For me, the classical way is:

[[ $# -eq 0 ]]
查看更多
叼着烟拽天下
3楼-- · 2019-02-24 06:00

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楼-- · 2019-02-24 06:02

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楼-- · 2019-02-24 06:09

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.

查看更多
爷、活的狠高调
6楼-- · 2019-02-24 06:11

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.

查看更多
登录 后发表回答