gpt4 book ai didi

bash - ${2 :-$2} what does this piece of code do

转载 作者:行者123 更新时间:2023-11-29 09:43:45 27 4
gpt4 key购买 nike

我在我的程序中忘记了一小段代码,是的,它真的很愚蠢:这是一个代码片段。

checkInputNum(){
if [[ $1 = *[^0-9]* ]]; then
echo "Error: '$1' is not valid Value." >&2
echo "Enter a value in numbers" >&2
read input
checkInputNum ${input}
elif [ -z "$1" ]; then
${2:-$2}
input=$2
checkinputNum $input
else
input=$1
echo ${input}
fi
}

最佳答案

这是一个导致无限递归的空操作。当且仅当:

[ -z "$1" ]

是真的。仅当字符串 "$1" 的长度为零时,即没有第一个参数时,这才成立。所以也没有第二个参数。现在:

${2:-$2}

如果未设置 $2,则计算为 $2!所以它默认为零长度字符串。这个空字符串然后被“执行”。这是一个空操作。然后你用这个“无参数”递归地调用你自己。然后,您所处的情况与最初让您到达那里的情况完全相同。您处于无限循环中。

关于bash - ${2 :-$2} what does this piece of code do,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23699862/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com