我正在尝试在bash中使用比较,但却无法使其正常工作。
#!/bin/bash
str="75.00 W, 170.00 W"
function str_check {
pow_array=()
regexp='([0-9]+)\.[0-9]+[[:space:]]W,[[:space:]]([0-9]+)\.[0-9]+[[:space:]]W'
[[ $str =~ $regexp ]] && for (( i = 0; i < 3; i++ )); do
pow_array+=("${BASH_REMATCH[$i]}")
done
if [ "$1" -lt ${pow_arr[1]} ]; then
echo "Available power limit is ${pow_array[0]}"
echo "Setting up ${pow_array[1]}"
elif [ "$1" -gt "${pow_arr[2]}" ]; then
echo "Available power limit is ${pow_array[0]}"
echo "Setting up ${pow_array[2]}"
else
echo "All good, setting up $1"
fi
}
str_check "70"
str_check "100"
str_check "200"
已经尝试'[[',''((''''',qoute和unquote everething,但是会遇到各种错误或错误的结果。需要有人帮我一把。
./ t.sh:第9行:[:70:一元运算符预期
./ t.sh:第12行:[::预期的整数表达式
答案 0 :(得分:5)
发现shellcheck的时间!
Line 9:
if [ "$1" -lt ${pow_arr[1]} ]; then
^-- SC2154: pow_arr is referenced but not assigned (did you mean 'pow_array'?).
^-- SC2086: Double quote to prevent globbing and word splitting.
将pow_arr
的所有引用更改为pow_array
和it works !