【发布时间】:2020-04-02 16:14:00
【问题描述】:
这是我的代码:
function get_index() {
my_array="$1"
value="$2"
for i in "${!my_array[@]}"; do
if [[ "${my_array[$i]}" = "${value}" ]]; then
echo "was found"
echo "${i}";
fi
done
return -1
}
echo "index test"
get_index "${cut_pages[@]}" 5
index=$?
echo $index
数组包括2和5
它必须返回 1,但它返回 255
有什么问题?
【问题讨论】:
-
my_array是一个字符串,而不是一个数组。 -
函数中,
$1 == ${cut_pages[0]},$2 == ${cut_pages[1]}等。 -
而
$?包含return语句中使用的值,而不是函数的输出。您需要使用index=$(get_index ...)来获取输出。