简体   繁体   English

这个shell命令中的“:?”是什么意思?

[英]what does “:?” mean in this shell command?

here is the shell script: 这是shell脚本:

#!/bin/bash

version="0.01";

fibonacci() {
    n=${1:?If you want the nth fibonacci number, you must supply n as the first parameter.}
    if [ $n -le 1 ]; then 
    echo $n
    else
    l=`fibonacci $((n-1))`
    r=`fibonacci $((n-2))`
    echo $((l + r))
    fi
}

for i in `seq 1 10`
do
  result=$(fibonacci $i)
  echo "i=$i result=$result"
done

And i come to confused about this line: 我对这一行感到困惑:

n=${1:?If you want the nth fibonacci number, you must supply n as the first parameter.}

i look for the manual of shell,but get nothing about what does the ":?" 我在寻找shell手册,但对“:?”一无所知 actually mean. 其实是说

thx 谢谢

from man bash: 来自man bash:

${parameter:?word}
          Display Error if Null or Unset.  If parameter is null or unset, the  expansion  of  word
          (or  a  message  to that effect if word is not present) is written to the standard error
          and the shell, if it is not interactive, exits.  Otherwise, the value  of  parameter  is
          substituted.

in this case the parameter being checked is $1 (the first positional parameter) 在这种情况下,要检查的参数为$ 1(第一个位置参数)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM